LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-29-2016, 11:53 AM   #1
maddyfreaks
Member
 
Registered: May 2011
Posts: 85

Rep: Reputation: 0
sed inside awk or awk inside awk


Hi Team

I would like to extract last feild from a line with is already parsed to awk.

I want to print last feild with sed inside awk or awk inside awk.

Code:
$ cat file.log
LOG_LOC:/opt/install/logs/12.2

$ cat file.log|awk -F':' '{print "Variable "$1 " has Location " $2 " version of "}'
Variable LOG_LOC has Location /opt/install/logs/12.2 version of

***The output i want is
$ cat file.log|awk -F':' '{print "Variable "$1 " has Location " $2 " version of "}'
Variable LOG_LOC has Location /opt/install/logs/12.2 version of 12.2
not sure how to use sed inside awk after column extract
 
Old 06-29-2016, 12:22 PM   #2
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 4,298

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
awk can do all of the string maniuplations that sed can do, so just do it all in awk.
Code:
awk -F':' '{print "Variable "$1 " has Location " $2 " version of " gensub(".*/", "", 1, $2)}' file.log
That also avoids the "useless use of cat".

Yes, gensub() is a GNU extension. If you're using some other version of awk, please specify.

Last edited by rknichols; 06-29-2016 at 12:25 PM. Reason: gnu extension mentioned
 
Old 06-29-2016, 12:27 PM   #3
maddyfreaks
Member
 
Registered: May 2011
Posts: 85

Original Poster
Rep: Reputation: 0
i have tried this it worked

$ cat file.log | awk -F':' '{print "Variable "$1 " has Location " $2 " version of " substr($2,19)}'
Variable LOG_LOC has Location /opt/install/logs/12.2 version of 12.2


but path gets changed due size
 
Old 06-29-2016, 12:30 PM   #4
maddyfreaks
Member
 
Registered: May 2011
Posts: 85

Original Poster
Rep: Reputation: 0
Perfect i got it thanks.
 
Old 06-29-2016, 01:10 PM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,719

Rep: Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034
Just remember that your substr command requires you to know the data length ahead of time but the gensub requires only the knowledge that the string after the last '/' is required
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Awk inside Awk ? acc_Wk Linux - Newbie 13 05-24-2012 06:15 PM
[SOLVED] awk: how can I assign value to a shell variable inside awk? quanba Programming 6 03-23-2010 02:18 AM
Sed inside awk ZAMO Linux - General 1 02-26-2009 04:13 AM
shell command using awk fields inside awk one71 Programming 6 06-26-2008 04:11 PM
awk inside a makefile linux.fob Programming 2 10-12-2005 04:57 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:43 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration