Mac Line search in Unix


balamw

Moderator
Staff member
Aug 16, 2005
19,095
963
New England
dukebound85 said:
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

macrumors P6
Original poster
Jul 17, 2005
18,113
1,269
5045 feet above sea level
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

Moderator
Staff member
Aug 16, 2005
19,095
963
New England
dukebound85 said:
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

macrumors 65816
Oct 19, 2003
1,490
0
Vancouver
balamw said:
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

Moderator
Staff member
Aug 16, 2005
19,095
963
New England
stcanard said:
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