Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with grep on script 1

Status
Not open for further replies.

dadms

Technical User
Mar 8, 2003
67
0
0
US
I am trying to count the number records that contain specific words that are not group together by grepping on 2 keywords in my script but having no success.

John Doe Married
Jan Doe Married
Don Doe Single

(i know the syntax is not right)
example....
#how many Doe's are Married
for file in 'blahblahblah'
do
grep -n -c "Doe"&&"Married" $file
done
 
There may be other ways, but try this:

grep Doe $file|grep -c Married

 
This...
Code:
awk '{print $3,$2}' $FILE | sort | uniq -c
...will give you...
Code:
   2 Married Doe
   1 Single Doe

Hope this helps.
 
ok, just one more thing, what do I do if the fields are comma delimited. I do I tell it that the comma separates the fields?


John,Doe,Married
Jan,Doe,Married
Don,Doe,Single
 
If you stick with "grep", it couldn't care less. It is simply a pattern matching utility. It doesn't care about what order the matching paterns are in either. You may want to explore the "awk" solution SamBones provided. You'll have much more control.
 
with just one grep:

grep -c "Doe.*Married" $file

. = any character
.* = any number of occurences of any character

There are other ways of specifying REs (Regular Expressions) so see man page for grep and ed - ed is where the REs in grep originated. In fact the name grep comes from: g RE p (global i.e. whole file; RE; p -print)


HTH,

p5wizard
 
I have figured out that -F, separates the fields however I get told that there are too many fields for the awk command. There are over 108.

HMMM...back to the grep..
 
thanks for all the help. I used the following:

grep -c "Doe.*Married" $file


It didn't initially work because I had a space between the . and *
 
thanks for all the help. I used the following:

grep -c "Doe.*Married" $file


It didn't initially work because I had a space between the . and *
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top