shantanuo
shantanuo

Reputation: 32336

Find the first or third column

The following command is working as expected. What I need to find is the thread id that is available in the first or third column.

# tail -1000 general.log | grep Connect | egrep -v "(abc|slave_user)"
                2856057 Connect root@localhost on 
111116  5:14:01 2856094 Connect root@localhost on 

If the line starts with the date, select the third column i.e. 2856094 or the first column i.e. 2856057

Expected output:
2856057
2856094

Upvotes: 1

Views: 2926

Answers (5)

jaypal singh
jaypal singh

Reputation: 77145

Use the awk inbuilt variable NF to capture the number of fields. If they equal to 6 then print 3 column else print 1st column.

awk 'NF==6{ print $3;next } { print $1 }' INPUT_FILE

Upvotes: 1

potong
potong

Reputation: 58473

This might work for you:

 tail -1000 general.log | sed -e '/abc\|slave_user/d;/ Connect.*/!d;s///;s/.* //'

Upvotes: 1

coelmay
coelmay

Reputation: 61

Without knowing the format of the file, maybe try:

$ tail -1000 general.log | grep Connect | egrep -v "(abc|slave_user)" | awk '{if ($3 == "root@localhost"){print $1;}else{print $3}}'

Or maybe this would work which is simpler:

$ awk '/Connect/ {if ($3 == "root@localhost"){print $1;}else{print $3}}' general.log

I tried. If I'm wrong, or there is a better way, I to will learn it in time. :)

Maybe this using int() ??????

$ awk '/Connect/ {if (!int($3)){print $1;}else{print $3}}' general.log

Upvotes: 0

Michael J. Barber
Michael J. Barber

Reputation: 25052

Another way to look at it is that you always take the fourth column when counting from the right:

awk '{ print $(NF-3) }'

Otherwise, if the date is really the only reliable indicator, try this:

awk -v Date=$(date "+%y%m%d") '$1 == Date { print $3; next } { print $1 }'

Upvotes: 5

mu is too short
mu is too short

Reputation: 434775

If your data really is that regular (i.e. all the columns are fixed width), then you could use cut:

tail -1000 general.log | grep Connect | egrep -v "(abc|slave_user)" | cut -c17-23

Upvotes: 1

Related Questions