tags:

views:

119

answers:

2

so I'm trying to display only columns at a time

first ls -l gives me this

drwxr-xr-x 11 stuff stuff      4096 2009-08-22 06:45 lyx-1.6.4
-rw-r--r--  1 stuff stuff  14403778 2009-10-26 02:37 lyx.tar.gz

I'm using this:

ls -l |cut -d " " -f 1

to get this

drwxr-xr-x 
-rw-r--r--

and it displays my first column just fine. Then I want to see on the second column

ls -l |cut -d " " -f 2

I only get this

11

Shouldn't I get

11
1

?
Why is it doing this?

if I try

   ls -l |cut -d " " -f 2-3

I get

11 stuff

There's gotta be an easier way to display columns right?

+4  A: 

This should show the second column:

ls -l | awk '{print $2}'

Chris J
This will work for all cases except the filename itself when the filename has embedded whitespace. For that, you may want to use either "awk '{print substr($0, 45)}'" or "ls -l | cut -c45-".
Shannon Nelson
+1  A: 

cut considers two sequential delimiters to have an empty field in between. So the second line:

-rw-r--r--  1 stuff stuff

has fields:

1: -rw-r--r--
2: --empty field--
3: 1
etc.

You can use use column fields in cut:

ls -l | cut -c13-14

Or you can use awk to separate fields (unlink, cut awk will treat sequential delimiters as a single delimiter).

R Samuel Klatchko