Solved

Get last column value with AWK for a variable

Posted on 2013-01-16
15
561 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
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.
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.

744 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