2014-05-07 39 views
2

我有以下的輸出:如何基於行的子串線排序

aaa=12 
bbb=124 
cccc=1 
dddd=15 

我想基礎上value.s上述線路所以輸出應該是這樣的排序:

$ cat file | awk_or_sed_or_any_command 
cccc=1 
aaa=12 
dddd=15 
bbb=124 

UPDATE

我嘗試以下命令:

$ cat file | awk -F '=' '{print $2"="$1}' | sort | awk -F '=' '{print $2"="$1}' 

但它太長了。

有沒有比上面更好的建議?

注:從busybox的只有支持下列選項我的Linux使用排序:

$ sort --help 
BusyBox v1.19.4 (2014-04-04 18:50:39 CEST) multi-call binary. 

Usage: sort [-nru] [FILE]... 

Sort lines of text 

     -n  Sort numbers 
     -r  Reverse sort order 
     -u  Suppress duplicate lines 

回答

6

使用以下命令

sort -n -t = -k 2 your_file 

給我

[email protected] ~ $ ash 
$ cat <<EOF | sort -n -t = -k 2 
> aaa=12 
> bbb=124 
> cccc=1 
> dddd=15 
> EOF 
cccc=1 
aaa=12 
dddd=15 
bbb=124 
$ which sort 
/usr/bin/sort 
$ LANG=C sort --version 
sort (GNU coreutils) 8.21 
Copyright (C) 2013 Free Software Foundation, Inc. 
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>. 
This is free software: you are free to change and redistribute it. 
There is NO WARRANTY, to the extent permitted by law. 

Written by Mike Haertel and Paul Eggert. 

檢查sort manpage爲其他sort選項

+0

不用了,'<' –

+0

輸出沒有以正確的方式進行排序。你能分享一下你的命令輸出嗎?這裏後我得到:'aaa = 12 bbb = 124 cccc = 1 dddd = 15 ' – MOHAMED

+0

它適用於bash而不適用於灰。我使用ash標記了我的問題 – MOHAMED

0
sed -e 's/=\(.*\)$/=000000\1_\1/;s/=0*\([0-9]\{7\}\)_/=\1_/' YourFile | sort | sed -e 's/=[0-9]_/=/' 

準備基本sort不是數字,也沒有服用列比放回原來的形式