Solved

Get last column value with AWK for a variable

Posted on 2013-01-16
15
571 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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Like to learn Linux, need advice for a good book and setup. 3 55
AWS ELB 5 95
AWS Central Authentication 1 85
PHP error function not working on AWS 10 66
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…
Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
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.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

791 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