Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

balamw

Moderator emeritus
Aug 16, 2005
19,365
979
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 Core
Original poster
Jul 17, 2005
19,160
4,152
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 emeritus
Aug 16, 2005
19,365
979
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,485
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 emeritus
Aug 16, 2005
19,365
979
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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.