Linux Blog

Remove lines that are in another file

Filed under: Shell Script Sundays — TheLinuxBlog.com at 6:52 pm on Sunday, February 7, 2010

Remove lines from a file that exist in another fileI had an issue this week where I needed to remove lines from one file if they existed in another file. Looking back it was frustrating as such a task should be simple.

I tried all sorts of things. Differencing the two files and using grep to grab the lines I wanted. Whatever I tried just did not produce the expected results. Thanks to a buddy I found the solution which ended up being to sort the two files before using diff.

Example:
Assuming two files exist, File_1 and File_2. File_1 containing lines with a, b, c and. File_2 containing b and d. If we want to remove b and d from File_1 because they exist in File_2 you could use something like the this:

owen@linuxblog:~$ cat File_1.txt
a
b
c
d
owen@linuxblog:~$ cat File_2.txt
b
d

owen@linuxblog:~$ diff File_1.txt File_2.txt | grep \< | cut -d \  -f 2
a
c

That’s all fine and dandy until File_2.txt contains the same lines in a different order. Running the same command produces different results. See Below:

owen@linuxblog:~$ cat File_2.txt
d
b

owen@linuxblog:~$ diff File_1.txt File_2.txt | grep \< | cut -d \  -f 2
a
b
c

The solution as noted above is to use sort before hand and then difference them:

owen@linuxblog:~$ sort File_1.txt >> File_1-sorted; sort File_2.txt >> File_2-sorted;
owen@linuxblog:~$ diff File_1-sorted File_2-sorted | grep \< | cut -d \  -f 2
a
c

Obviously the example has been simplified, when dealing with thousands of lines the sort could take a while. With that said I’m sure there are more efficient ways to achieve the same results. I wouldn’t doubt there being a command better suited to do this. Have at it in the comments.

Ubuntu Unleashed 2010 Edition Review

Filed under: General Linux — TheLinuxBlog.com at 11:36 am on Monday, January 25, 2010

Irritated with my Desktop after an upgrade gone bad and an incident with the nvidia noveau driver that left me x less, I decided it was time to re-install. I turned to my bookshelf to find Ubuntu Unleashed 2010 Edition. Normally by the time a book hits my shelf the material is outdated, not necessarily useless, just not the most up to date. This is an exception. The Ubuntu Unleashed 2010 Edition was updated with an Ubuntu 9.10 DVD and a “Free Upgrade to Ubuntu 10.04″ which I found out that if you buy the book before the end of 2010 you can get an upgrade kit in the mail.

So, I pop the DVD in the drive and start the installation. Nothing new here for anyone that has installed Linux or Ubuntu recently; for those that haven’t, it was a pleasant surprise to see that it actually detected my high resolution monitor and used it to its advantage. It really is strange to not have to squint at an installer. The first chapter covers the step by step installation in more detail which is relatively short and easy to follow. Most people should not need to read this if they are familiar with installing an operating system but it I think it is good to have it there. Just don’t let this first chapter prevent you from looking further into this book. After putting the DVD in and getting it started, I found myself reading the book through the entire installation; which for some reason got from 0-90% quickly, then took the majority of the time in the 90% range, but I’m not complaining.

The Authors really did a good job of writing in an understandable language and organizing the book in a logical format. I’ve found myself flipping through and finding many golden nuggets of information. I personally would not have picked this book up because of the title, since I’m not a big Ubuntu user. But Ubuntu Unleashed 2010 edition is packed full of information, 32 chapters and a hefty appendix to be exact. It is not all Ubuntu – specific either, meaning most of the content should work on just about distribution. This book would not be rendered useless if you don’t decide to go the Ubuntu route. I recommend taking a look at the contents and buying this book, as I’m sure you’ll be pleasantly surprised at the topics it covers. I think it would be a great book for someone that is interested in Linux in general, it reads well but can also be used as a quick reference. I wish I had a book like this when I was getting started, it would have saved me a whole lot of time and effort. I have set aside some of the more advanced chapters and made a note to read later.

Other reviews I’ve read have said that it has too much terminal use in it, which is something Ubuntu is trying to eliminate. While this may be true, if you want the most out of your Linux distribution, the fact is you will at some point use a terminal. Commands are less likely to change as much as graphical interfaces. Although some things may be slightly outdated I don’t think that this book should be re-written, as it is in the nature of open source and technology to change. If you keep this in mind I don’t think you’ll be disappointed with it.



Goals For The New Year

Filed under: The Linux Blog News — TheLinuxBlog.com at 9:34 pm on Wednesday, January 6, 2010

As the new year has turned, I thought it would be a good time to go back and look at my goals for 2009. To be honest they were pretty modest and I’d pretty much forgotten what they were, despite this fact I still managed to get 4 out of the 6 done. Getting certified never happened, but I feel like I became more qualified, which is all that matters right? Hah. I blame the CD ripping on streaming media services like Pandora, Grooveshark and last.fm which pretty much eliminated my need to rip the CD’s. So all in all I guess it was a productive year.

I’m still working on my technical related goals for this coming year, I don’t really know what to put on the list yet, since I’ve found that goals will change as time goes on. For example, I set up my PBX in November of 09 which was about 11 months after I set the goal that I had forgotten about. Maybe I’ll roll the two I did not accomplish in 2009 over, add a the goals of getting more organized and reducing the amount of equipment I have. Yea, those seem reasonable for now.

Happy new year!

Syncing your Delicious Bookmarks

Filed under: General Linux — TheLinuxBlog.com at 11:24 am on Friday, December 11, 2009

I’ve written about syncing your bookmarks on Linux before.

I’ve been using the method with GMarks and the Google Toolbar plugin for some time and it works just fine. I also have a Delicious account that I use for work, that I like to access and save book marks to from any computer., this is where SimpleDelicious comes into play.

Simple and seamless delicious bookmarks management from your FireFox browser menu. Unlike others it’s aimed to be simple to use and not bloated with invasive and unnecessary features.
features:

-Add and delete delicious tags/bookmarks from the browser menu, as simple as that

And it really it is, it’s a quick install then enter your account information and you’re good to go. I find the sorting on it is a little strange as it organizes by tags rather than folders, but it works. If you remember where you put something or have a good organizing system (unlike my assortment of random tags that seemed like a good idea at the time) you’ll always be able to find what you’re looking for. You can always use the web front end to search and organize your bookmarks if you have to, but this firefox plugin makes syncing and adding to your delicious bookmarks easy!

delicious_thumb

Best iPhone Error Message Ever

Filed under: The Linux Blog News — TheLinuxBlog.com at 11:45 am on Wednesday, December 9, 2009
Best iPhone Error Message Ever

Best iPhone Error Message Ever

Not Linux related at all but I thought it was funny.  So, yea. Ignore the giant DISMISS_ALERT button, that’s an error caused by me being stupid, but I thought the actual message was pretty funny.

After dialing a “special” number that I strategically obfuscated using gimp and then may have failed to upload the correct one. I get an:

“Error performing request, No Error”.

Although interesting, I now need to find some one without a jail broken phone to see if they get the same message just for the lols. Anyway this sort of stuff can be found all over the place, I just think its funny to find it on such a trendy hipster user oriented device.

When not to script it

Filed under: General Linux, Linux Software — TheLinuxBlog.com at 10:37 am on Wednesday, December 2, 2009

I’ve come across a number of programs in the past and thought to myself,

“Why didn’t they script this, it would be so much easier.”

After thinking about it for a while it all makes sense now. It seems that I had the shell script bug, everything must be done in the shell! In reality while the shell is great and all, it is not without its flaws. There are somethings the shell is great at and should be used for, then there others it shouldn’t.

Don’t get me wrong, I like to script as much as the next guy, but sometimes I think people get lost in its simplicity. Some people use it for everything without fail. Perhaps this is because they don’t know otherwise, or maybe because they too have the shell script bug.

There are many reasons to use the shell and I’m not against using it by any means. This is simply a list of when it may not be suitable to whip out the shell and start scripting. If there is any I missed, please add them in the comments and they may eventually make it into a revised post. I’ll do the same if I come up with any more. Onto the list!

Distribution / Portability – Ever try to distribute a script? Well, most of the time its not a problem, but sometimes you’ll get bit in the ass. Not all systems are created equal. Some have missing packages, or something doesn’t work the EXACT same way.

Simplicity – Sometimes bash is elegant. Well, it depends on your definition I guess. Some argue that scripting is simple, where as others that have to support or maintain it will be ready to cut you. I know I’m sharpening my shanks right now for some revenge. Seriously though, choose a language that makes it easy to K.I.S.S (keep it simple, stupid.)

Performance – Lets kick in the after burners. This is one I don’t have much experience in although I should probably look into it. The shell is sort of like a glider, it gets the jobs done, but I wouldn’t want to race it against a jet or anything. If you’re considering scripting, just time the process to make sure it meets your performance needs. Most of the time, if it is for you, you’ll be fine right? I mean you’ve got all of the time in the world and can wait overnight if you have to. I’d rather not, but have been known to wait on a slow script out of laziness.

Maintenance – Nightmare code. Dependency Hell. Some may have seen the t-shirt or heard the phrase,

“Programming is like sex; one mistake, and you’ll support it forever”

Yea scripting this is sort of like that, except you can always abandon and upgrade your scripts :)

Security – Keep it secure, there are many issues with security in shell scripts. We wont get into them now but if you wear a tinfoil hat when using a computer, you may not want to rely on shell scripts to do your top secret stuff.

Bash Tips for more efficient terminal usage

Filed under: Linux Software, Linux for Newb's — TheLinuxBlog.com at 5:36 pm on Monday, November 23, 2009

Here are some tips for working in the shell that I use on a daily basis. These may be known to most veterans, please contribute your favorite shortcuts to the comments. But for the most part this is not for the uber leet Linux geek, this is for those new to the terminal.

TAB
- Gives you suggestions / completes stuff for you. Type a, Press it twice, it will make your life a lot easier.

ctrl+w
- Remove word behind cursor.

ctrl+u
- Undo / Erase everything from cursor to beginning of the line

!!
- Type out whatever you typed out last, can be combined with tools like sudo

alt+f
- Go forward to the end of the previous word

alt+b
- Move cursor back to the beginning of the previous word

ctrl+d
- You’re done? Press ctrl+d to logout. If you’re in a virtual terminal, it may also close the window.

ctrl+z
- Stop the current process. Say you run gvim, and then you want to spawn another process. Press ctrl+z, then type bg. Once you’re done with the “other” process, you may type fg to bring it back into the foreground.

Yum Messed Up

Filed under: General Linux, Linux Software — TheLinuxBlog.com at 10:05 am on Monday, November 16, 2009

So, this morning, when I got to work and booted up, I noticed something was wrong. My machine was not running as usual. It turns out, packagekit was sitting there being a general pain. I just wanted to look busy, so I killed the process. Turns out that wasn’t such a good idea, since it didn’t finish doing whatever it was doing and caused an ugly error like this:

rpmdb: Thread/process 28373/3077981888 failed: Thread died in Berkeley DB library
error: db4 error(-30975) from dbenv->failchk: DB_RUNRECOVERY: Fatal error, run database recovery
error: cannot open Packages index using db3 – (-30975)
error: cannot open Packages database in /var/lib/rpm
CRITICAL:yum.main:

Error: rpmdb open failed

rpmdb: Thread/process 28373/3077981888 failed: Thread died in Berkeley DB library
error: db4 error(-30975) from dbenv->failchk: DB_RUNRECOVERY: Fatal error, run database recovery
error: cannot open Packages index using db3 – (-30975)

Naturally, I tried using rpm, do try and fix things, since it indicated something to do with a database I tried:

[owen@linuxblog ~]$ sudo rpm –justdb
rpm: –justdb may only be specified during package installation and erasure
[owen@linuxblog ~]$ sudo rpm –rebuilddb
rpmdb: Thread/process 28373/3077981888 failed: Thread died in Berkeley DB library
error: db4 error(-30975) from dbenv->failchk: DB_RUNRECOVERY: Fatal error, run database recovery
error: cannot open Packages index using db3 – (-30975)

Well, it turns out that those options don’t work, and the rebuilddb gave an error about the db4 being corrupt. Well, I decided to go into /var/lib/rpm as the other error had that directory and found the db4 files. Running db45_recover sounded like it might work, so I gave it a shot.

[owen@linuxblog rpm]$ db45_recover -h /var/lib/rpm/
db45_recover: unlink: /var/lib/rpm/__db.003: Permission denied
db45_recover: unlink: /var/lib/rpm/__db.004: Permission denied
db45_recover: unlink: /var/lib/rpm/__db.000: Permission denied
db45_recover: unlink: /var/lib/rpm/__db.002: Permission denied
db45_recover: unlink: /var/lib/rpm/__db.001: Permission denied
db45_recover: /var/lib/rpm/log.0000000001: log file unreadable: Permission denied
db45_recover: PANIC: Permission denied
db45_recover: DB_ENV->log_newfh: 1: DB_RUNRECOVERY: Fatal error, run database recovery
db45_recover: dbenv->close: DB_RUNRECOVERY: Fatal error, run database recovery

That’s right,you need to be root for this bad boy:

[owen@linuxblog rpm]$ sudo db45_recover -h /var/lib/rpm/

The above seemed to fix it for now. I guess next time I’ll think about it more before I start slaying processes on a Monday morning.

Backup your DVDs on Linux

Filed under: General Linux — TheLinuxBlog.com at 12:01 am on Wednesday, November 4, 2009

At an average of around $20 per DVD* most can’t afford to have any of their DVDs lost, stolen or broken. Backing them up is a touchy subject depending on who you talk to, but here is how to do it anyway.

Backing up a DVD on Linux used to be much harder than it is with K9Copy. No more flaky DVD Shrink crashes under wine. K9Copy takes the hassles out of creating backups of your DVDs under Linux. As the name implies it is KDE software but works well under gnome provided you have the needed libraries installed.

It has many options to backup and really is pretty comparable in functionality to the infamous DVD Shrink for Windows. Take a look at the screenshots and give it a try yourself:

* 2007 US Entertainment Industry Report (www.mpaa.org/USEntertainmentIndustryMarketStats.pdf)

BackTrack Persistent “Press Enter Fix”

Filed under: General Linux, Shell Script Sundays — TheLinuxBlog.com at 5:37 pm on Sunday, November 1, 2009

One of the things that has irritated me about the persistent USB thumb drive installs is the “Press Enter” to continue prompt on shutdown. Luckily, if you have persistent working correctly, the fix for this is easy.

Open up /etc/rc0.d/S89casper
Search for ENTER and the shutdown string “Please remove the disc and close the tray (if any) then press ENTER: ” > /dev/console”
For me, this was on line 89. Comment out, that line and the lines down to the ending bracket for the do_stop() routine.

Reboot, and see if you have that annoying message, if not you’re good to go.

Halloween Pumpkin

Filed under: General Linux — TheLinuxBlog.com at 12:15 am on Friday, October 30, 2009

For Halloween this year every department at work was asked to carve or decorate a pumpkin. Since I’m in IT I thought I’d take the task of decorating our pumpkin with something IT related that would scare the crap out of everyone. Most of our end users are actually Windows users, so this pumpkin should be especially scary to them.

I introduce the B.S.O.Dkin, yes it’s a B.S.O.D pumpkin

B.S.O.D Pumpkin.

B.S.O.D Pumpkin.

I’d like to say it looks better in person, but it really doesn’t. I don’t really have an artsy knack for carving or painting pumpkins or I could have done something a lot better.

I guess my “Halloween Costume” is a blue Mexican wrestlers mask, blue t-shirt, and jeans to complete the B.S.O.D theme. With as much hate as I’ve been giving Microsoft recently I’m going to have to keep my fingers crossed for no kernel panics. Karma might bite me.

Next Page »