Solved

Get last column value with AWK for a variable

Posted on 2013-01-16
15
564 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
 
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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SSH commands for Nas4free 21 381
Wrap Oraccle SQL*Plus executable Command 4 69
How to install Ubuntu 16 in DELL venue 8 pro 20 53
VMware Workstation 12 Player 16 42
Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
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.

895 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now