LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   sed inside awk or awk inside awk (https://www.linuxquestions.org/questions/linux-newbie-8/sed-inside-awk-or-awk-inside-awk-4175583364/)

maddyfreaks 06-29-2016 11:53 AM

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

rknichols 06-29-2016 12:22 PM

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.

maddyfreaks 06-29-2016 12:27 PM

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

maddyfreaks 06-29-2016 12:30 PM

Perfect i got it thanks.

grail 06-29-2016 01:10 PM

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


All times are GMT -5. The time now is 07:56 AM.