Locations of visitors to this page

Monday, October 12, 2009

sort file by length of lines

sort file by length of lines
按每行长度对文件内容排序

比如文件包含一些人名, 想按名称长度进行排序
cat >myfile <<EOF
Andre Anichanov
Andrew Mogrelia
Antonio Vivaldi
Antoni Wit
Capella Istropolitana
Christian Kohn
Cologne Chamber Orchestra
Daniel Rothert
Daniel Taylor
Einar Steen-Nokleberg
Elena Papandreou
Eteri Andjaparidze
EOF

awk得到每行的长度, 按长度排序后显示输出
awk '{print length,$0}' myfile|sort -rn|cut -d" " -f2-
或者将管道写在print后
awk '{print length,$0|"sort -rn|cut -d\" \" -f2-"}' myfile

运行结果
Cologne Chamber Orchestra
Einar Steen-Nokleberg
Capella Istropolitana
Eteri Andjaparidze
Elena Papandreou
Antonio Vivaldi
Andrew Mogrelia
Andre Anichanov
Daniel Rothert
Christian Kohn
Daniel Taylor
Antoni Wit



-fin-

No comments:

Website Analytics

Followers