Link to home
Start Free TrialLog in
Avatar of amahajan1981
amahajan1981

asked on

grep between 3rd and 4rth comma

Hi

Text files look like this :

689:2006/07/12 01:08:06,5,espresso,1492197335,star_schema_log,0.198698,0.394908,0.00448759090909091,88,1.46666666666667
689:2006/07/12 01:08:06,5,espresso,1492197336,star_schema_log,0.198698,0.394908,0.00448759090909091,88,1.46666666666667
689:2006/07/12 01:08:06,5,espresso,1492197337,star_schema_log,0.198698,0.394908,0.00448759090909091,88,1.46666666666667

I wanted to grep only word star_schema_log between 3rd and 4rth comma ..... how can i do this!

Regards
Anand
Avatar of zephyr_hex (Megan)
zephyr_hex (Megan)
Flag of United States of America image

use grep to get the lines you need (if you need to select only certain lines from a file), and then use awk to parse the lines.
or, use can use "cut" along with grep... grep will select the lines you need and cut can parse the lines
Avatar of amahajan1981
amahajan1981

ASKER

i dont want to select lines all lines have "," as delimeter...

i just wanted to get a word from 3rd and 4rth ","

I am a beginner in unix...just started to read abt awk and sed

regards
Anand  
ASKER CERTIFIED SOLUTION
Avatar of grsteed
grsteed

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of rockiroads
rockiroads
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
just 90%? ha, only kidding tacticalvehicle. Im so just to piping output from cat, I forget u can do the way u did

thanks for the recommendation though, appreciated!
Print all lines where the 4th field matches a pattern:

awk -F"," '/ $4 == "star_schema_log" {print $0}' /pathto/file.log
The simplest solution is as someone above suggested: cut does it simply and correctly. You tell it that your delimiter is the comma, and that you want field number 5:
   cut -d, -f5 file.txt

In fact, cut is really clever, in that you can pick out only the fields you want, e.g. to grab fields 1, 5, 6 and 7, you can do:
   cut -d, -f1,5-7 file.txt
# quick&dirty
awk -F,  '{print $4}' /path/to/file.log|grep pattern

# the awk way
awk -F,  '/'pattern/'{print}' /path/to/file.log
Thnks guys for ur help!!

regards
Anand