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
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
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!
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?
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
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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!
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
Sorry if this is not what you want. If you want the entire line, you can remove -o option.
$ grep 'K1DB' t5 | grep -o '16.01.2013:16:30:00'
16.01.2013:16:30:00
Sorry if this is not what you want. If you want the entire line, you can remove -o option.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
"-F" of awk accepts regular expressions.
awk -F"LAST=| TYPE=" '/'$DBNAME'/ {OUT=$2} END {print OUT}' inputfile
awk -F"LAST=| TYPE=" '/'$DBNAME'/ {OUT=$2} END {print OUT}' inputfile
ASKER
Thanks guys! I will test it tomorrow and award the points afterwards!
Have a nice evening!
Have a nice evening!
awk -F'[ =]+' '$2=="'$DBNAME'"{L=$4}END{ print L}' inputfile
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
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.
This would do it too, so will many other ways
Open in new window