You are not logged in.

#1 2013-07-13 14:47:28

graysky
Member
From: /run/user/1000
Registered: 2008-12-01
Posts: 8,423
Website

Parsing logs, need to keep lines matching two conditions [SOLVED]

I wrote a script that generates a 12 columns in a text file based on parsing my apache logs.  I need to remove any line that match the following two conditions:

1)     <TAB>200<TAB>
2)     <TAB>206<TAB>

Where <TAB> is a literal tab as my other stuff reads tab deliminated files.  The following grep works to match simply '200' or '206' but I can't get the grep syntax correct to match with the trailing and leading tabs.

grep -w '200\|206' parsed.log

Any help is appreciated.

EDIT: Finally!

grep -E $'\t200\t\|\t206\t' parsed.log

EDIT2: Rrrr... the above works under zsh but not under bash... what's my problem?

Last edited by graysky (2013-07-13 16:16:08)


CPU-optimized Linux-ck packages @ Repo-ck  • AUR packagesZsh and other configs

Offline

#2 2013-07-13 15:18:38

thisoldman
Member
From: Pittsburgh
Registered: 2009-04-25
Posts: 1,172

Re: Parsing logs, need to keep lines matching two conditions [SOLVED]

Does this work:

grep -E '(\t200\t)|(\t206\t)' parsed.log

Edit: Wait, that matches a literal 't', doesn't it?

I think you need this:

grep -P '[\t]200[\t]|[\t]206[\t]' parsed.log

Last edited by thisoldman (2013-07-13 15:33:54)

Offline

#3 2013-07-13 15:19:50

tavianator
Member
From: Waterloo, ON, Canada
Registered: 2007-08-21
Posts: 857
Website

Re: Parsing logs, need to keep lines matching two conditions [SOLVED]

It fails under bash because bash treats "\|" as a literal "\|", i.e. a backslash and then a pipe.  zsh will turn it into just "|".  Anyway you don't have to escape the |, so "grep -E $'\t200\t|\t206\t' parsed.log" should work.

Offline

#4 2013-07-13 15:26:53

graysky
Member
From: /run/user/1000
Registered: 2008-12-01
Posts: 8,423
Website

Re: Parsing logs, need to keep lines matching two conditions [SOLVED]

Thanks for the assistance.  Will mark as solved.

...actually, my original thought was flawed.  I actually need to preform this grep query within column #9 in the text file.  Simply doing it on a line-by-line basis does not catch the errors.  I need to some how mate this up with awk.

EDIT: Here is a sample of the file: http://repo-ck.com/PKG_source/test.txt

Last edited by graysky (2013-07-13 15:58:23)


CPU-optimized Linux-ck packages @ Repo-ck  • AUR packagesZsh and other configs

Offline

#5 2013-07-13 15:33:03

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

Re: Parsing logs, need to keep lines matching two conditions [SOLVED]

graysky, do both conditions have to be met at the same time? Is it 1) AND 2) or 1) OR 2)?

Offline

#6 2013-07-13 15:33:58

graysky
Member
From: /run/user/1000
Registered: 2008-12-01
Posts: 8,423
Website

Re: Parsing logs, need to keep lines matching two conditions [SOLVED]

karol wrote:

graysky, do both conditions have to be met at the same time? Is it 1) AND 2) or 1) OR 2)?

Is it an or: either 200 or 206 (it will never be both).  I edited post #4 with a sample file.

Last edited by graysky (2013-07-13 15:48:25)


CPU-optimized Linux-ck packages @ Repo-ck  • AUR packagesZsh and other configs

Offline

#7 2013-07-13 16:03:17

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

Re: Parsing logs, need to keep lines matching two conditions [SOLVED]

graysky wrote:

Thanks for the assistance.  Will mark as solved.

...actually, my original thought was flawed.  I actually need to preform this grep query within column #9 in the text file.  Simply doing it on a line-by-line basis does not catch the errors.  I need to some how mate this up with awk.

EDIT: Here is a sample of the file: http://repo-ck.com/PKG_source/test.txt

Try

awk '$9 ~ /20[0,6]/' test.txt

The sample has only 200 in the 9th column, but I edited it and it worked with 206 too.

Offline

#8 2013-07-13 16:08:21

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

Re: Parsing logs, need to keep lines matching two conditions [SOLVED]

Oooops.

You're using tab-deliminated files, so try

awk -F '\t' '$9 ~ /20[0,6]/' test.txt

instead.

Sorry for bumping.

Offline

#9 2013-07-13 16:15:39

graysky
Member
From: /run/user/1000
Registered: 2008-12-01
Posts: 8,423
Website

Re: Parsing logs, need to keep lines matching two conditions [SOLVED]

@karol - Thank you.  That solved it.


CPU-optimized Linux-ck packages @ Repo-ck  • AUR packagesZsh and other configs

Offline

#10 2013-07-15 13:14:56

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

Re: Parsing logs, need to keep lines matching two conditions [SOLVED]

Except that '/20[0,6]/' will also match "20," ... character ranges are literal.

Offline

Board footer

Powered by FluxBB