Solved

Get last column value with AWK for a variable

Posted on 2013-01-16
15
580 Views
Last Modified: 2013-01-17
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
0
Comment
Question by:skahlert2010
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 3
  • +1
15 Comments
 
LVL 31

Expert Comment

by:farzanj
ID: 38783824
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

0
 

Author Comment

by:skahlert2010
ID: 38783840
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!
0
 
LVL 31

Expert Comment

by:farzanj
ID: 38783853
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

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 31

Assisted Solution

by:farzanj
farzanj earned 100 total points
ID: 38783863
This works too:
grep 'K1DB' t5 | sed 's/.*LAST=//;s/ .*//'
16.01.2012:23:30:00
16.01.2013:14:30:00
16.01.2013:16:30:00

Open in new window

0
 
LVL 31

Assisted Solution

by:farzanj
farzanj earned 100 total points
ID: 38783869
Another one:

grep 'K1DB' t5 | awk -F= '{print $3}' | awk '{print $1}'
16.01.2012:23:30:00
16.01.2013:14:30:00
16.01.2013:16:30:00

Open in new window


t5 is the filename.
0
 

Author Comment

by:skahlert2010
ID: 38783990
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!
0
 
LVL 31

Expert Comment

by:farzanj
ID: 38784036
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.
0
 
LVL 84

Accepted Solution

by:
ozo earned 350 total points
ID: 38784042
awk '$1=="'DBNAME=$DBNAME'"{L=$2}END{print substr(L,6)}' t5
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 38784061
"-F" of awk accepts regular expressions.

awk -F"LAST=| TYPE=" '/'$DBNAME'/ {OUT=$2} END {print OUT}' inputfile
0
 

Author Comment

by:skahlert2010
ID: 38784124
Thanks guys! I will test it tomorrow and award the points afterwards!

Have a nice evening!
0
 
LVL 84

Expert Comment

by:ozo
ID: 38784233
awk -F'[ =]+' '$2=="'$DBNAME'"{L=$4}END{print L}' inputfile
0
 
LVL 68

Assisted Solution

by:woolmilkporc
woolmilkporc earned 50 total points
ID: 38784356
Yes, ozo, I know it will work that way, but I always like to make it a bit more "unique", thus more "robust".

My version will tolerate spaces e.g between date and time or, with a tiny modification, also spaces in the DB name. Adding columns before the "LAST=" column will not cause problems either.

But yes, your version will tolerate multiple consecutive equal signs and spaces, so ...

awk -F"LAST=+| +TYPE=" '/'"$DBNAME"'/ {OUT = $2} END {print OUT}' inputfile
0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 350 total points
ID: 38785008
perl -lne '($l)=/LAST=(\S+)/ if/DBNAME='$DBNAME'\b/;END{print $l}'  inputfile
0
 

Author Closing Comment

by:skahlert2010
ID: 38786098
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!
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 38786429
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.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question