LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 05-20-2011, 01:48 AM   #1
vinaytp
Member
 
Registered: Apr 2009
Location: Bengaluru, India
Distribution: RHEL 5.4, 6.0, Ubuntu 10.04
Posts: 704

Rep: Reputation: 55
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,
 
Old 05-20-2011, 01:58 AM   #2
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295
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

Last edited by EricTRA; 05-20-2011 at 01:59 AM.
 
1 members found this post helpful.
Old 05-20-2011, 02:08 AM   #3
vinaytp
Member
 
Registered: Apr 2009
Location: Bengaluru, India
Distribution: RHEL 5.4, 6.0, Ubuntu 10.04
Posts: 704

Original Poster
Rep: Reputation: 55
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 !!

Last edited by vinaytp; 05-20-2011 at 02:09 AM.
 
Old 05-20-2011, 02:09 AM   #4
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295
Hi,

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

Kind regards,

Eric
 
1 members found this post helpful.
Old 05-20-2011, 04:27 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,516

Rep: Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893
As long as we realise that awk is in no way needed for this task??
Code:
echo ${var##*/}
 
1 members found this post helpful.
  


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] Perl extract information for a particular column kdelover Programming 7 10-26-2010 03:24 AM
awk multiple column into single column ilukacevic Programming 49 07-19-2010 07:23 PM
Column statistic by awk ? cs24 Programming 7 01-15-2010 05:41 AM
Change column value with AWK RyudoBlaze Programming 2 04-25-2009 03:05 AM
awk column printing schneidz Programming 7 09-29-2005 06:14 AM

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

All times are GMT -5. The time now is 09:52 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