linux - 如何根据行中的子字符串对行进行排序

标签 linux shell awk sed ash

我有以下输出:

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

更新

我尝试了以下命令:

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

但是太长了。

还有比上述更好的建议吗?

注意:我的 linux 使用仅支持以下选项的 busybox 排序:

$ 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

最佳答案

使用以下命令

sort -n -t = -k 2 your_file

给我

alex@rhyme ~ $ 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 选项

关于linux - 如何根据行中的子字符串对行进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23516245/

相关文章:

linux - 如何为 OpenSSL 提供自定义编译器/链接器标志?

linux - 如何使用expect(TCL)脚本将十进制转换为十六进制并发送十六进制

python - 使用python获取Linux中所有已挂载的文件系统的列表

linux - 根据第一列的开头合并 txt 中的几行

bash - 如何使用 sh 迭代 awk 的每个输出?

linux - 从文件列表中选择文件

javascript - 使用javascript在浏览器中解析grep的结果

java - 使用脚本启动 nohup java 命令并获取进程 ID RHEL

linux - 有没有办法改变vim的默认模式

awk - 如何在 awk 中打印记录的行号?