LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 10-02-2010, 02: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, 03: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, 03:09 AM   #3
sem007
Member
 
Registered: Nov 2006
Distribution: RHEL, CentOS, Debian Lenny, Ubuntu
Posts: 638

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

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

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
And have a look here as to why parsing ls is not such a great idea.
 
Old 10-02-2010, 04: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, 05:09 AM   #6
sem007
Member
 
Registered: Nov 2006
Distribution: RHEL, CentOS, Debian Lenny, Ubuntu
Posts: 638

Rep: Reputation: 113Reputation: 113
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, 05: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, 05:34 AM   #8
sem007
Member
 
Registered: Nov 2006
Distribution: RHEL, CentOS, Debian Lenny, Ubuntu
Posts: 638

Rep: Reputation: 113Reputation: 113
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, 11:22 AM   #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, 11:45 AM   #10
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
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, 07:07 PM   #11
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

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



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

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

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