PDA

View Full Version : Line search in Unix




dukebound85
Sep 14, 2006, 11:35 PM
How would one go about searching many lines of a file for a word, or more specifically, searching for a line that doesnt contain some desired word.

thanks



balamw
Sep 14, 2006, 11:47 PM
How would one go about searching many lines of a file for a word, or more specifically, searching for a line that doesnt contain some desired word.

thanks
grep is your friend.

man grep will give you the basic usage.

For what you want:
grep [word] [filename]

finds the lines that match word

grep -v [word] [filename]

finds the lines that don't match word

You can mix and match too with pipes

e.g. grep [word1] [filename] | grep -v [word2]

will match the lines that have word1 but not word2.

I won't even touch regular expressions here...

B

dukebound85
Sep 15, 2006, 12:02 AM
Thanks, that is exactly what I was looking for!

dukebound85
Sep 15, 2006, 12:22 AM
what about copying certain line numbers from one file and exporting to another?

I am guessing sed may be an option

sed -c "1,5" file > new

this will copy lines 1-5 of file and export to new right?


thanks for all the help so far



EDIT: I figured it out sed -n '18,22pp' file will get me lines 18-22

balamw
Sep 15, 2006, 12:30 AM
what about copying certain line numbers from one file and exporting to another?

I am guessing sed may be an option

sed -c "1,5" file > new

this will copy lines 1-5 of file and export to new right?


thanks for all the help so far

For just getting the first or last lines in a file, head and tail are far more straightforward.

e.g. head -5 [filename] | grep [words]

You can play all kinds of lovely games with grep, sed and awk. So much so they wrote a nice O'Reilly book about them.

e.g. grep can output X many lines before and Y many lines after the match with -A and -B so you can look for strings in lines adjacent to matches etc...

B

stcanard
Sep 15, 2006, 01:23 AM
You can play all kinds of lovely games with grep, sed and awk. So much so they wrote a nice O'Reilly book about them.

grep, sed, and awk are probably the 3 most underused commands in unix (in that people may use them, but rarely truly exercise what they can do).

Although find and xargs are pretty good candidates too.

Master those 5 commands, and it is amazing what you can do in a single line.

with -A and -B so you can look for strings in lines adjacent to matches etc...

I run my own mailserver, and let any address come in, so when I give emails I can make them up on the fly (company name) so I can track spam. I run this periodically on my Spam folder:

grep -a -C15 "personal@myemail.com" Spam | grep "Subject: " | sort | uniq -c | egrep "^[ ]+1"

To check for any personal emails that got caught. It shows me any unique subjects (so it filters the 100 vi@gra spams I have) that came to my personal address so I can quickly scan for somebody that sent me an message that got binned wrongly. Cuts out an incredible amount of chaff.

balamw
Sep 15, 2006, 08:19 AM
grep, sed, and awk are probably the 3 most underused commands in unix (in that people may use them, but rarely truly exercise what they can do).

Although find and xargs are pretty good candidates too.

Master those 5 commands, and it is amazing what you can do in a single line.

I only recently started using xargs (to moitor and clean up the Spam Maildirs on our mail gateway), so I know exactly what you mean!

Is it even possible to really exercise grep, awk and sed. Or perl for that matter.

B