Link to home
Start Free TrialLog in
Avatar of skahlert2010
skahlert2010

asked on

Get last column value with AWK for a variable

Dear experts,

I have the following log file with input such as:

DBNAME=K1DB LAST=16.01.2012:23:30:00 TYPE=C
DBNAME=K2DB LAST=16.01.2012:11:30:00 TYPE=C
DBNAME=K3DB LAST=16.01.2012:12:30:00 TYPE=C
DBNAME=K1DB LAST=16.01.2013:14:30:00 TYPE=O
DBNAME=K1DB LAST=16.01.2013:16:30:00 TYPE=C

I have a ksh variable DBNAME that has the value K1DB for instance.

With awk I need to extract the last date (16.01.2013:16:30:00) within that logfile for that specific variable i.e. K1DB.

Can you please show me how to achive that? I have tried different things throughout the day with no success. A solution would be tremedously welcome.

Thanks,

skahlert2010
Avatar of farzanj
farzanj
Flag of Canada image

Specifically in AWK?

This would do it too, so will many other ways
grep -P 'K1DB' filename | grep -Po '\d{2}\.\d{2}\.\d{4}\S+'

Open in new window

Avatar of skahlert2010
skahlert2010

ASKER

Well, many ways are welcome, as long as the output yields the correct result.
I was focussing on awk due to its abilities. However I am not fond of them and having a hard time.

Did you test your example? I don't have the possibilty right now.

Thanks a lot!
Yes, I did.

Is this what you want?
$ grep -P 'K1DB' t5 | grep -Po '\d{2}\.\d{2}\.\d{4}\S+'
16.01.2012:23:30:00
16.01.2013:14:30:00
16.01.2013:16:30:00

Open in new window

SOLUTION
Avatar of farzanj
farzanj
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Wow Farzanj, you amaze me!

Great!

The output looks promising but I need only the latest LAST VALUE ==> 16.01.2013:16:30:00
Depending on a calculation with that value I will start another action.

Using your suggestions is there a way to filter the latest date value?

So far you did a great job!

Thank you!
Oh, looks like you just want to grep

$ grep 'K1DB' t5 | grep -o '16.01.2013:16:30:00'
16.01.2013:16:30:00

Open in new window


Sorry if this is not what you want.  If you want the entire line, you can remove -o option.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
"-F" of awk accepts regular expressions.

awk -F"LAST=| TYPE=" '/'$DBNAME'/ {OUT=$2} END {print OUT}' inputfile
Thanks guys! I will test it tomorrow and award the points afterwards!

Have a nice evening!
awk -F'[ =]+' '$2=="'$DBNAME'"{L=$4}END{print L}' inputfile
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for helping! I tested all of your suggestions! Some of them wor brilliantly, others go into the right direction but do not cut off everything but the time.<br /><br />@woolmilkpork: I really liked your example but unfortunately the output of <br /><br />awk -F"LAST=+ |+TYPE=" '/'"$DBNAME"'/ {OUT = $2} END {print OUT}' $BKP_LOG_DIR/$BKP_LOG <br /><br />is   AST=17.01.2013:12:13:00 BKPTYPE=O<br /><br />It doesn't cut off the string before and after the date value. <br /><br />Thanks to all for your excellent help!
If my version didn't work the you must have run it against data different to those you posted.

For example, your inputfile seems to contain "BKPTYPE" instead of "TYPE".

Further, you have a space after "LAST=+" which is wrong.

Thanks for the points.