LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Not able to extract last column from awk (https://www.linuxquestions.org/questions/linux-newbie-8/not-able-to-extract-last-column-from-awk-881736/)

vinaytp 05-20-2011 01:48 AM

Not able to extract last column from awk
 
Hi All,

I have a basic question of awk.

Code:

var=/test/build/create/sls
echo $var | awk '{ FS = ":" ; print $NF }'
/test/build/create/sls

I am trying to extract last column from the above awk one liner. I am not getting where am I getting wrong !!

Thanks,

EricTRA 05-20-2011 01:58 AM

Hello,

If you're trying to get sls as a value, at least that's what I understand from the above, then you need to set your field separator to / instead of : and also if I'm not mistaking take the FS declaration outside the {} like this:
Code:

echo $var | awk -F'/' '{ print $5 }'
if you want to get sls as value. If the value of $var is different and you always want the last column, then substitute $5 for $NF as in your first post.

Kind regards,

Eric

vinaytp 05-20-2011 02:08 AM

Many Thanks sir,

Actually while posting I made that mistake of putting ":" instead of '/' . I did not have a chance to copy/paste from terminal.

Thanks again !!

EricTRA 05-20-2011 02:09 AM

Hi,

You're welcome. If you consider your problem solved then please mark the thread as such. Have fun with Linux.

Kind regards,

Eric

grail 05-20-2011 04:27 AM

As long as we realise that awk is in no way needed for this task??
Code:

echo ${var##*/}


All times are GMT -5. The time now is 05:19 PM.