Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Get last column value with AWK for a variable

Posted on 2013-01-16
15
Medium Priority
?
594 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
  • 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
Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

 
LVL 31

Assisted Solution

by:farzanj
farzanj earned 400 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 400 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 85

Accepted Solution

by:
ozo earned 1400 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 85

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 200 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 85

Assisted Solution

by:ozo
ozo earned 1400 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

Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Question has a verified solution.

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

Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
Utilizing an array to gracefully append to a list of EmailAddresses
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Suggested Courses
Course of the Month9 days, 15 hours left to enroll

927 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