tags:

views:

72

answers:

5

Hey,

Using only grep and sed, is there a way I can tranform the output of ls -l * into this :

-rw-r--r-- agenda.txt

-rw-r--r-- annuaire.txt

Thanks!

A: 

Like this?

ls -l | sed 's/ [0-9].*:.[0-9] / /' | less

Transforms

-rw-r--r--  1 tomislav tomislav     609 2009-11-26 10:32 Test.class
-rw-r--r--  1 tomislav tomislav      46 2009-12-14 12:16 test.groovy

into

-rw-r--r-- Test.class
-rw-r--r-- test.groovy
Tomislav Nakic-Alfirevic
are you sure it works?
ghostdog74
The `g` is unnecessary.
Dennis Williamson
This will fail for files where `ls` prints the year instead of hh:mm.
hlovdal
I've provided some test input, the command and the output I get: I am sure it works that way because that is a copy/paste of what I got.Hlovdal, the year doesn't show up for any of my files instead of hh:mm.Dennis, always glad to see someone reduce/improve: you are right, the 'g' is not needed.
Tomislav Nakic-Alfirevic
Your command works fine on my Ubuntu Tomislav :)
Samantha
From the core-utils info manual: "By default, file timestamps are listed in abbreviated form. Mostlocales use a timestamp like `2002-03-30 23:45'. However, the defaultPOSIX locale uses a date like `Mar 30 2002' for non-recent timestamps,and a date-without-year and time like `Mar 30 23:45' for recenttimestamps. A timestamp is considered to be "recent" if it is less than sixmonths old"
hlovdal
I have files modified in 2008 and they still show yyyy-MM-dd hh:mm <filename>. Judging by Samantha's comment, it's just ubuntu's default behaviour.
Tomislav Nakic-Alfirevic
+2  A: 

seeing that you have already got your "answer", here's one of the simpler solution

 ls -l | tr -s " "| cut -d" " -f1,8-

@OP, sed is "powerful", but sometimes, simplicity is more powerful.

Side note: Don't parse file names like that.

ghostdog74
I agree with you ghostdog, but I just needed to learn a couple tricks with sed... Thanks :)
Samantha
Of all the given answers, I consider this to be the best one from a robustness point of view. All of the sed answers will fail in case of filenames with an unfortunate combination of colon, space and/or numbers in them. This solution will not fail that way.
hlovdal
+1  A: 

Here is a working command. The slightly tricky thing is that ls -l will print the year for files that are older than some time (6 months) and hh:mm for newer files.

ls -l  | sed 's/ .*[0-9]* .*[A-Z][a-z][a-z] [ 0-9][0-9] \{1,2\}[0-9][0-9]:*[0-9][0-9] / /'

For the following example

drwxr-xr-x 39 root root 1024 Feb 19 08:58 /

the starting .* will match 39 root root 1024 and then the rest of the regular expression matches month name (so you might restrict a-z to fewer characters) followed by year or hh:mm.

hlovdal
that's doesn't work
ghostdog74
What does not work? I have tested and verified the command on a solaris 10 machine.
hlovdal
+2  A: 
 ls -l | sed 's/[ ]+//g' | sed 's/ [0-9].*:.[0-9]/ /g'
muruga
Hey, tested your command, and noticed that the first part | sed 's/[ ]+//g' doesn't really do much :) it works just fine whitout it... did you add it for a particular purpose?
Samantha
I assume the first sed command is intended to be the same as `tr -s " "` as in ghostdog74's answer. The correct syntax for that is `sed 's/[ ]\+/ /g'`.
hlovdal
+1  A: 
ls -l | sed 's/^\([^\t ]\+\)\(.*:.[^ \t]\+\)\(.\+\)/\1 \3/'
waw! works for me, thank you :)
Samantha