开发者

select fields with sed

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!


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.


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


ls -altrh| sed -E 's/ +.+ / / g'

Or you can go with ssed which supports Perl Regular Expressions.

I solved your problem using the ssed program you can install it in any Posix system, ssed stands for super sed. so i did a ls -latrh in my home directory.

telsa:~ mahmoh$ ls -altrh

total 136
drwxr-xr-x   5 root    admin   170B Jun 24 00:27 ../
drwx------+  4 mahmoh  staff   136B Jun 24 00:27 Pictures/
drwx------+  3 mahmoh  staff   102B Jun 24 00:27 Music/
drwx------+  3 mahmoh  staff   102B Jun 24 00:27 Movies/
drwx------+  3 mahmoh  staff   102B Jun 24 00:27 Desktop/
-rw-------   1 mahmoh  staff     3B Jun 24 00:27 .CFUserTextEncoding
drwxr-xr-x+  5 mahmoh  staff   170B Jun 24 00:27 Public/
drwx------+  5 mahmoh  staff   170B Jun 24 02:19 Documents/
-rw-r--r--@  1 mahmoh  staff    15K Jun 24 02:19 .DS_Store
drwx------@ 36 mahmoh  staff   1.2K Jun 24 14:48 Library/
-rw-r--r--   1 mahmoh  staff   279B Jun 24 15:27 .profile~
-rw-r--r--@  1 mahmoh  staff    14K Jun 24 15:29 .vimrc
-rw-r--r--   1 mahmoh  staff   279B Jun 24 15:30 .profile
drwx------   2 mahmoh  staff    68B Jun 24 15:46 .Trash/
drwxr-xr-x   3 mahmoh  staff   102B Jun 24 20:26 .mplayer/
-rw-------   1 mahmoh  staff   3.5K Jun 24 22:11 .bash_history
-rw-------   1 mahmoh  staff    42B Jun 24 23:25 .lesshst
-rw-r--r--   1 mahmoh  staff   3.6K Jun 24 23:39 temp
-rw-r--r--   1 mahmoh  staff   3.3K Jun 24 23:43 rtorrent.rc~
drwxr-xr-x   5 mahmoh  staff   170B Jun 24 23:52 torrents/
-rw-r--r--   1 mahmoh  staff   3.3K Jun 24 23:56 .rtorrent.rc~
-rw-------   1 mahmoh  staff   3.7K Jun 24 23:56 .viminfo
-rw-r--r--   1 mahmoh  staff   3.3K Jun 24 23:56 .rtorrent.rc
drwxr-xr-x+ 25 mahmoh  staff   850B Jun 24 23:56 ./
drwx------+ 10 mahmoh  staff   340B Jun 24 23:58 Downloads/

Now watch.

telsa:~ mahmoh$ ls -altrh| ssed -R -e 's/ +.+ /    / g'
total 136
drwxr-xr-x    ../
drwx------+    Pictures/
drwx------+    Music/
drwx------+    Movies/
drwx------+    Desktop/
-rw-------    .CFUserTextEncoding
drwxr-xr-x+    Public/
drwx------+    Documents/
-rw-r--r--@    .DS_Store
drwx------@    Library/
-rw-r--r--    .profile~
-rw-r--r--@    .vimrc
-rw-r--r--    .profile
drwx------    .Trash/
drwxr-xr-x    .mplayer/
-rw-------    .bash_history
-rw-------    .lesshst
-rw-r--r--    temp
-rw-r--r--    rtorrent.rc~
drwxr-xr-x    torrents/
-rw-r--r--    .rtorrent.rc~
-rw-------    .viminfo
-rw-r--r--    .rtorrent.rc
drwxr-xr-x+    ./
drwx------+    Downloads/


ls -l | sed 's/^\([^\t ]\+\)\(.*:.[^ \t]\+\)\(.\+\)/\1 \3/'


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.


why not use awk instead of sed? awk is built for stuff like this.

see this manual page for more about fields in awk.


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜