You are not logged in.

#1 2011-01-04 04:24:28

Gumper
Member
From: U.S.A.
Registered: 2009-10-26
Posts: 132

Help extracting data from a grep command

I'm using grep to pull some data from a log file but I can't seem to be able to get exactly what I need. What I have so far is the following:

[22:00:16]   /usr/sbin/chroot                                [ Warning ]

What I need to do is to somehow only select the "/usr/sbin/chroot" part of what grep gives me and remove the "[22:00:16]" and the "[Warning]".
I've tried using "cut" and "sed" but I can't seem to figure it out.

Could someone point me in the right direction on what to use to be able to do this?

Thanks!


Ready yourselves, ready yourselves
Let us shine the light of Jesus in the darkest night
Ready yourselves, ready yourselves
May the powers of darkness tremble as our praises rise .... Casting Crowns-Until The Whole World Hears.

Offline

#2 2011-01-04 04:56:21

lagagnon
Member
From: an Island in the Pacific...
Registered: 2009-12-10
Posts: 1,087
Website

Re: Help extracting data from a grep command

"awk" is more suited to extracting information from columnar data.
Something like:

cat mylogfile | awk '{print $1}'


Philosophy is looking for a black cat in a dark room. Metaphysics is looking for a black cat in a dark room that isn't there. Religion is looking for a black cat in a dark room that isn't there and shouting "I found it!". Science is looking for a black cat in a dark room with a flashlight.

Offline

#3 2011-01-04 04:56:34

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: Help extracting data from a grep command

This should be safe enough:

sed 's/^\[[^]]\+\][[:blank:]]\+//;s/[[:blank:]]\+\[[^]]\+\]$//'
lagagnon wrote:

"awk" is more suited to extracting information from columnar data.
Something like:

cat mylogfile | awk '{print $1}'

True, but this isn't quite columnar. Also, your cat hates you right now.

Last edited by falconindy (2011-01-04 04:58:09)

Offline

#4 2011-01-04 09:37:00

carlocci
Member
From: Padova - Italy
Registered: 2008-02-12
Posts: 368

Re: Help extracting data from a grep command

falconindy wrote:

This should be safe enough:

sed 's/^\[[^]]\+\][[:blank:]]\+//;s/[[:blank:]]\+\[[^]]\+\]$//'

I detect a terrible case of seditis regexposa

awk --re-interval -F'[ \n\t]{2,}' '{print $2}' $LOGFILE

as usual, the more a regexp is general, the uglier it gets.

I think a simple

awk '{print $1}' $LOGFILE

is better.

Offline

#5 2011-01-04 13:12:05

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: Help extracting data from a grep command

@carlocci
I think you meant

awk '{print $2}' $LOGFILE

Offline

#6 2011-01-04 13:50:56

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: Help extracting data from a grep command

carlocci wrote:
falconindy wrote:

This should be safe enough:

sed 's/^\[[^]]\+\][[:blank:]]\+//;s/[[:blank:]]\+\[[^]]\+\]$//'

I detect a terrible case of seditis regexposa

awk --re-interval -F'[ \n\t]{2,}' '{print $2}' $LOGFILE

as usual, the more a regexp is general, the uglier it gets.

I think a simple

awk '{print $1}' $LOGFILE

is better.

I'm comfortable in my seditis. Your --re-interval looks extremely gawkish, but that's definitely a handy flag to know about. Thanks.

Last edited by falconindy (2011-01-04 13:51:09)

Offline

#7 2011-01-04 22:56:21

Gumper
Member
From: U.S.A.
Registered: 2009-10-26
Posts: 132

Re: Help extracting data from a grep command

Thanks for the help everyone.


Ready yourselves, ready yourselves
Let us shine the light of Jesus in the darkest night
Ready yourselves, ready yourselves
May the powers of darkness tremble as our praises rise .... Casting Crowns-Until The Whole World Hears.

Offline

Board footer

Powered by FluxBB