LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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 06-11-2011, 01:10 AM   #1
frenchn00b
Senior Member
 
Registered: Jun 2007
Location: E.U., Mountains :-)
Distribution: Debian, Etch, the greatest
Posts: 2,561

Rep: Reputation: 57
`ls -ltra | cut -f3` How would you cut?


Hello,

Cut is not working for ls or ps aux or other where tab are not clearly defined. Another solutions are perl, awk, python, ... other ...

However I would consider that cut could work no, somehow. How would you cut it actually or what would be your universally working preference?
 
Old 06-11-2011, 01:14 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
Sorry, what's the point of this thread? Just use awk.

not programming. Moved to Linux - Newbie.
 
Old 06-11-2011, 01:27 AM   #3
frenchn00b
Senior Member
 
Registered: Jun 2007
Location: E.U., Mountains :-)
Distribution: Debian, Etch, the greatest
Posts: 2,561

Original Poster
Rep: Reputation: 57
Quote:
Originally Posted by acid_kewpie View Post
Sorry, what's the point of this thread? Just use awk.

not programming. Moved to Linux - Newbie.
It's philosophy about cut.

Awk is not a wise solution if for instance you code an application over sh and give it to a friend. If he has not awk, what you do? however all alternative to cut are done with some packages that may not be installed...
 
Old 06-11-2011, 01:36 AM   #4
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
I don't think you're exactly the best judge of what is a wise solution...
 
Old 06-11-2011, 01:44 AM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Besides acid_kewpie's comments: The cut command _does_ work! The problem is that the output of ls is space(s) separated and cut uses a tab as delimiter....

Using awk is the best/easiest solution in this case. I haven't come across a linux/unix distro that does not come with awk, if the actions done with awk are simple it doesn't matter if nawk, gawk, mawk or Xawk is used.

Hope this helps.
 
Old 06-12-2011, 11:17 PM   #6
Valery Reznic
ELF Statifier author
 
Registered: Oct 2007
Posts: 676

Rep: Reputation: 137Reputation: 137
cut use as default field separator tab. But it can be changed with -d flag
 
Old 06-12-2011, 11:57 PM   #7
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
Hi,

this is slightly off topic from your initial post, but your second post you indicate that your motivation for the question is portability.

If you want portability, I'd suggest not parsing the output of ls. The output of 'ls -ltra' can differ significantly on different systems. Since you mention 'cut -f3' I guess you are trying to get a list of the file owners (at least that is the 3rd field on my system).

Code:
stat --format='%U' *
HTH,

Evo2.
 
Old 06-13-2011, 02:11 AM   #8
ssrameez
Member
 
Registered: Oct 2006
Location: bangalore
Distribution: Fedora, Ubuntu, Debian, Redhat
Posts: 82

Rep: Reputation: 6
Quote:
Originally Posted by frenchn00b View Post
Hello,

ls -lart|cut -f3
You are missing the field separator.

ls -lart|cut -d ' ' -f3

This is working fine for me.
 
Old 06-13-2011, 02:14 AM   #9
salasi
Senior Member
 
Registered: Jul 2007
Location: Directly above centre of the earth, UK
Distribution: SuSE, plus some hopping
Posts: 4,070

Rep: Reputation: 897Reputation: 897Reputation: 897Reputation: 897Reputation: 897Reputation: 897Reputation: 897
And, of course, read this.
 
Old 06-13-2011, 02:30 AM   #10
frenchn00b
Senior Member
 
Registered: Jun 2007
Location: E.U., Mountains :-)
Distribution: Debian, Etch, the greatest
Posts: 2,561

Original Poster
Rep: Reputation: 57
Quote:
Originally Posted by ssrameez View Post
You are missing the field separator.

ls -lart|cut -d ' ' -f3

This is working fine for me.
For ps aux, it is the same story, it is not working also for portability:

Code:
ps aux | cut -f4
Solution is surely awk that is likely installed but if you take a 300MB just installed debian for instance (server install with the minimum) you will not have awk at all, nor perl. However "cut" is there, but it cannot easily crop the columns
 
Old 06-13-2011, 02:50 AM   #11
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
Hi,

this is similar to my post suggesting you use stat.

Quote:
Originally Posted by frenchn00b View Post
For ps aux, it is the same story, it is not working also for portability
I'd suggest that the solution with ps, is not to parse its output (with cut, awk, perl or whatever), but to give the correct flags/options to ps so that it only ouputs the information that you are interested in.

Evo2.
 
Old 06-13-2011, 02:54 AM   #12
ssrameez
Member
 
Registered: Oct 2006
Location: bangalore
Distribution: Fedora, Ubuntu, Debian, Redhat
Posts: 82

Rep: Reputation: 6
I am not sure why are you missing the delimiter..

ps aux|cut -d' ' -f1

You can modify the field f1 or f4 accordingly.
 
  


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
[SOLVED] using cut puth Programming 4 03-11-2010 02:55 PM
help with cut command using find. Cut last 8 characters leaving the rest ncsuapex Programming 4 09-16-2009 08:55 PM
How to use command grep,cut,awk to cut a data from a file? hocheetiong Linux - Newbie 7 09-11-2008 07:16 PM
Cut Help metallica1973 Programming 67 12-20-2007 04:36 PM

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

All times are GMT -5. The time now is 09:44 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
Open Source Consulting | Domain Registration