Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I skip line with awk

Tags:

bash

awk

I have a command like this:

tac $log | awk -v pattern="test" '$9 ~ pattern {print; exit}'

It shows me the last line in which $9 contains test text.

Like this:

Thu Mar 26 20:21:38 2015 1 10.8.0.22 94 /home/xxxyyy/zzz_test_223123.txt b _ o r spy ftp 0 * c
Thu Mar 26 20:21:39 2015 1 10.8.0.22 94 /home/SAVED/zzz_test_123123.txt b _ o r spy ftp 0 * c
Thu Mar 26 20:21:40 2015 1 10.8.0.22 94 /home/xxxyyy/zzz_test_123123.txt b _ o r spy ftp 0 * c
Thu Mar 26 20:21:41 2015 1 10.8.0.22 94 /home/SAVED/zzz_test_123124.txt b _ o r spy ftp 0 * c

-- >

Thu Mar 26 20:21:41 2015 1 10.8.0.22 94 /home/SAVED/zzz_test_123124.txt b _ o r spy ftp 0 * c

This command shows me the last line. But I need to pass if line have SAVED. So I need to show like this:

Thu Mar 26 20:21:40 2015 1 10.8.0.22 94 /home/xxxyyy/zzz_test_123123.txt b _ o r spy ftp 0 * c

How can I do this?

like image 671
onur Avatar asked Apr 24 '15 08:04

onur


2 Answers

To skip a line, you can match it, and use the next command.

$9 ~ /SAVED/ { next }
$9 ~ /\.txt$/ { print; exit }
like image 164
Markus Jarderot Avatar answered Oct 08 '22 17:10

Markus Jarderot


You can add another condition !~ to prevent this second pattern to be matched (I use pattern2 to make it more generic, of course you can hardcode SAVED there):

$9 ~ pattern && $9 !~ pattern2

All together:

$ awk -v pattern="test" -v pattern2="SAVED" '$9 ~ pattern && $9 !~ pattern2 {print; exit}'
Thu Mar 26 20:21:40 2015 1 10.8.0.22 94 /home/xxxyyy/zzz_test_123123.txt b _ o r spy ftp 0 * c
like image 26
fedorqui 'SO stop harming' Avatar answered Oct 08 '22 18:10

fedorqui 'SO stop harming'