Welcome to the most active Linux Forum on the web.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 10-11-2007, 02:27 AM   #1
LQ Newbie
Registered: Dec 2005
Posts: 29

Rep: Reputation: 15
Cut command Problem

Hi All
I am using ll(long listing) and the output produced by the ll i want to cut the last field by using cut command. But as u know that there is not any separator
and the spaces between each column are not fixed. How to solve it.

Anirban Adhikary.
Old 10-11-2007, 02:42 AM   #2
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
Use awk instead, as in
ls -l | awk '{print $NF}'
this will print the last field of each line, despite the number of blank spaces between fields. However it does not work if filenames contain blank spaces. Please note that the output of the command above is exactly the same as
ls -1
with no problems for filenames!
Old 10-11-2007, 02:44 AM   #3
LQ Veteran
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405

I'm not sure if you want the last field printed or remove it from the rest of the line.

colucix shows how to print the last field.

If you want all but the last field:

sed 's/\(.*\) .*$/\1/' infile

Hope this helps.
Old 10-11-2007, 03:02 AM   #4
LQ Guru
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 681Reputation: 681Reputation: 681Reputation: 681Reputation: 681Reputation: 681
Another way to do it is to compress the extra spaces and use a space as the delimiter:

 ls -l | tr -s ' ' | cut -d' ' -f 8
I'm not saying this is better than the awk method presented earlier.

However, why do you want to use a long listing and only keep the name. Might as well use a short listing "ls -1" or use find, "find ./ -maxdepth 1 -type f"


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
the 'cut' command and HTML metalx1000 Programming 1 10-27-2006 08:50 PM
finger and cut command Craig Bowes Linux - Newbie 3 10-09-2004 12:49 PM
Cut Command Help!! fooforon Programming 2 02-05-2004 10:09 AM
cut / awk command?? Sammy2ooo Linux - Newbie 1 05-27-2003 05:46 PM
Easy: Help with Cut command Krash_io Programming 4 07-17-2002 02:29 AM > Forums > Linux Forums > Linux - Newbie

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

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