LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 08-30-2009, 04:39 PM   #1
devnulljp
LQ Newbie
 
Registered: Jan 2006
Posts: 1

Rep: Reputation: 0
Using cut to extract only last item in line?


Anyone know how to use cut to select the last, or second to last, or a range from the end of the line using a delimiter?
cut -d: -f 1 will pull out the first, cut -d: -f 1-3 will get 1 to 3, but only from the beginning. Is it possible to chomp away from the end?

I have lines of different lengths and all I want is the last item (so it's not possible to grab a specified field number, as sometimes the last one is field3 others it's 5 or 8 or more.

Ex.
Code:
item1:item2:item3:item4 
item1:item2
item1:item2:item3:item4:item5:item6:item7:item8
so I want line1, item4; line2,item2, line3,item8 etc.

Right now I'm munging it with a python script, but I figure cut must be able to do this more simply?

Any ideas?
 
Old 08-30-2009, 05:33 PM   #2
lwasserm
Member
 
Registered: Mar 2008
Location: Baltimore Md
Distribution: ubuntu
Posts: 184

Rep: Reputation: 41
Well, this is perhaps not very pretty but here is one way:
Code:
$ echo one two three four five six seven|rev|cut -d' ' -f1|rev
seven
$

Last edited by lwasserm; 08-30-2009 at 05:34 PM.
 
Old 08-30-2009, 05:58 PM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Another method using awk:
Code:
awk -F: '{print $NF}' file
 
  


Reply



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
Cut and paste from command line dulli1 Linux - Newbie 9 10-12-2010 09:31 PM
How do I extract characters from several words on a line? MheAd Linux - Newbie 22 06-23-2008 10:22 PM
Cut(command) line by line leventis Programming 4 09-24-2006 06:51 PM
mozilla xpm kmenu item desktop item cjae Linux - Newbie 3 04-06-2005 07:11 AM
Command line extract Daunted Linux - Software 2 09-30-2004 05:37 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

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

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
Open Source Consulting | Domain Registration