Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get a part of a line after grep

Tags:

file

grep

unix

I have a huge file on my unix server from which I need to extract certain parts

The format of the line is

aNumber timestamp commandInformation

I use the command

grep LATENCY file.log | grep CMDTYPE=NEW

to filter out certain lines that I want. I only want the part timestamp and the last 9 characters from the line to be returned, not the complete line. How can I do that?

like image 714
randomThought Avatar asked Sep 24 '09 12:09

randomThought


2 Answers

You can do it all with grep using -o which outputs only the match and not the whole line.

Assuming you can create a regex for the timestamp and the rest of the line, you could simply add:

... | grep -o regex  

[Added answer for anyone else who lands here trying to extract part of a line using grep where the regex is the part they want to extract.]

like image 152
Ian Mercer Avatar answered Oct 12 '22 00:10

Ian Mercer


cut must do the job

grep something somewhere | grep againsomething | cut -f2 -d' '
like image 22
Balakrishnan Avatar answered Oct 11 '22 23:10

Balakrishnan