Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

grep between 3rd and 4rth comma

Posted on 2006-07-14
11
Medium Priority
?
533 Views
Last Modified: 2013-12-26
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
0
Comment
Question by:amahajan1981
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +5
11 Comments
 
LVL 44

Expert Comment

by:zephyr_hex (Megan)
ID: 17112131
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.
0
 
LVL 44

Expert Comment

by:zephyr_hex (Megan)
ID: 17112141
or, use can use "cut" along with grep... grep will select the lines you need and cut can parse the lines
0
 

Author Comment

by:amahajan1981
ID: 17112536
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  
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 11

Accepted Solution

by:
grsteed earned 750 total points
ID: 17112944
Not sure exactly what you're looking for. You say "I wanted to grep only word star_schema_log between 3rd and 4rth comma"  

The word "star_schema_log" shows up between the 4th and 5th comma.

If you want the value between the 3rd and 4th comma (4th field) , how about this,

cat file |cut -d "," -f  4

This will give you a list of the values in the 4th field. If you want to look for something specific, add "|grep whatever" to that command.


Cheers,

Gary
0
 
LVL 65

Assisted Solution

by:rockiroads
rockiroads earned 600 total points
ID: 17114854
You could try AWK


cat yourfile | awk -F','  '{print $4}'


I think its $4 to get between 3rd and 4th, if not, try $3 or $5


passing in -F is what delimiter to use, in this case its a comma

it then puts the values into argument variables
$1 refers to the value up to the 1st comma
$2 refers to the value between 1st and 2nd comma
and so on

0
 
LVL 1

Assisted Solution

by:tacticalvehicle
tacticalvehicle earned 150 total points
ID: 17115246
as commented rockiroads,

AWK tool is best for you

awk -F,  ' { print $4 }' /pathto/file.log

# this is only for for better perfomance cat with PIPE isnt nescessary in this case becouse AWK is able read log file directly
# ( I also forgot this feature :)

if you want more fields...

awk -F,  ' { print $3"   "$4 }' /pathto/file.log  
# output is will be
#
#espresso   1492197335
#espresso   1492197336
#espresso   1492197337


also with AWK you cas count this fileds or sort or else
I hope now is for you clear

90% points this solution is for rockiroads ;)
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 17115537
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!
0
 
LVL 21

Expert Comment

by:tfewster
ID: 17117338
Print all lines where the 4th field matches a pattern:

awk -F"," '/ $4 == "star_schema_log" {print $0}' /pathto/file.log
0
 
LVL 15

Expert Comment

by:bpmurray
ID: 17118400
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
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 17121632
# quick&dirty
awk -F,  '{print $4}' /path/to/file.log|grep pattern

# the awk way
awk -F,  '/'pattern/'{print}' /path/to/file.log
0
 

Author Comment

by:amahajan1981
ID: 17156350
Thnks guys for ur help!!

regards
Anand
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question