LinuxQuestions.org
Register a domain and help support LQ
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 10-02-2010, 03:56 AM   #1
culin
Member
 
Registered: Sep 2006
Distribution: Fedora Core 10
Posts: 254

Rep: Reputation: 32
formatting ls or get one column from ls output


Hello pals,
i have a problem.. what i need to do is to extract one complete column(file size) from the output of ls -lS but while doing so in some rows i have a single space but in some other rows i have 2 or 3 spaces like some file sizes are different with 30 bytes 400 bytes and some 4000 bytes.. so when i extract the output of ls using | cut -d ' ' -f5 i get the value which has only one space i.e. i get 4000 as output because 400 has 2 spaces seperated and 30 is 3 spaces seperated..
so how to get the file size column from the ls out put ???????

Thanks and regards... .
 
Old 10-02-2010, 04:01 AM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
use the stat command
Code:
 stat -c "%s:%n" *
 
Old 10-02-2010, 04:09 AM   #3
sem007
Member
 
Registered: Nov 2006
Distribution: RHEL, CentOS, Debian Lenny, Ubuntu
Posts: 638

Rep: Reputation: 111Reputation: 111
You can also use awk commnad.

Code:
ls -lS | awk {'print$5'}
Regards,
 
Old 10-02-2010, 05:15 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,243

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
And have a look here as to why parsing ls is not such a great idea.
 
Old 10-02-2010, 05:41 AM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by sem007 View Post
You can also use awk commnad.

Code:
ls -lS | awk {'print$5'}
Regards,
that's also wrong. Did you test your code?
 
Old 10-02-2010, 06:09 AM   #6
sem007
Member
 
Registered: Nov 2006
Distribution: RHEL, CentOS, Debian Lenny, Ubuntu
Posts: 638

Rep: Reputation: 111Reputation: 111
Quote:
Originally Posted by ghostdog74 View Post
that's also wrong. Did you test your code?
Yes i test this and it disply file size columns.

ls -l output

Code:
[root@CentOS-5 dir1]# ls -l
total 24
drwxr-xr-x 2 root root 4096 Sep 29 21:15 1
drwxr-xr-x 2 root root 4096 Sep 29 21:17 2
-rw-r--r-- 1 root root    8 Oct  2 15:42 abc
-rw-r--r-- 1 root root 9703 Oct  2 15:42 xyz
with awk command

Code:
[root@CentOS-5 dir1]# ls -l | awk {'print$5'}

4096
4096
8
9703
what is wrong with this?

Regards,
 
Old 10-02-2010, 06:39 AM   #7
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by sem007 View Post
what is wrong with this?
the correct way to write is
Code:
ls -l | awk '{print $5}'
 
Old 10-03-2010, 06:34 AM   #8
sem007
Member
 
Registered: Nov 2006
Distribution: RHEL, CentOS, Debian Lenny, Ubuntu
Posts: 638

Rep: Reputation: 111Reputation: 111
Quote:
Originally Posted by ghostdog74 View Post
the correct way to write is
Code:
ls -l | awk '{print $5}'
errr...

Yes you are right. some time small mistake create big problem. i don't know how i made this mistake.

Thanks for pointing.

Regards,
 
Old 10-19-2010, 12:22 PM   #9
culin
Member
 
Registered: Sep 2006
Distribution: Fedora Core 10
Posts: 254

Original Poster
Rep: Reputation: 32
thanks a lot to all for replies..
@ghostdog74 why is awk {'print$5'} wrong way ? and why is awk '{print $5}' this right way ???
 
Old 10-19-2010, 12:45 PM   #10
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,063

Rep: Reputation: 381Reputation: 381Reputation: 381Reputation: 381
Just use "stat" as suggested above. It's just silly to parse the ls output when there's a tool that will give you the exact bit of info you are needing.
 
1 members found this post helpful.
Old 10-19-2010, 08:07 PM   #11
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,243

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
Quote:
@ghostdog74 why is awk {'print$5'} wrong way ? and why is awk '{print $5}' this right way ???
Try it on your command line and you will quickly see why.
 
  


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
Average of a column in console output santius Linux - General 4 09-04-2010 12:50 AM
(Formatting)new Column adding pinga123 Linux - Newbie 1 04-28-2010 02:17 AM
Multiple Column output for LS Scratch59 Linux - Newbie 4 12-09-2009 06:28 PM
Loop column output handband2 Programming 8 11-05-2008 04:40 PM
Sorting of multi-column output rytrom Linux - Newbie 11 09-15-2003 12:31 PM


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