格式化列表工具column和sort使用一则

来源:互联网 发布:意德法家 知乎 编辑:程序博客网 时间:2024/05/17 05:52

2012-10-20 wcdj

在对表格形式的输出数据进行处理时,column和sort工具大多可以派上用场。

看一个例子:

#/bin/sh# 2012-10-20 wcdjfile=test.txtcat << gerry_here_doc=================================usage:# m: the number of fields from onecat file | sort +(m-1) -m +(m-1) -m# useful for pretty-printing displays# -s option is specifing a set of characters to be used to delimit columns for the -t optioncat file | column -t=================================gerry_here_doctext="4  20121001-20121018  14523670   1    20121001-20121018    637368     3   20121001-20121018  308371     2         20121001-20121018         14832041   5  20121001-20121018   1.5469409E71  20121001-20121017  615608     3         20121001-20121017      305929     5  20121001-20121017  1.5271584E74     20121001-20121017   14350047   2  20121001-20121017      14655976   "echo "$text" > "$file"echo ""echo "export file named test.txt to current dir."echo "the contents of test.txt are: "cat "$file"echo "you could find that this file showing looks some confused !"echo "so, use column command to display it prettily"echo ""cat "$file" | column -techo ""echo "then, I wanna sort this file by the second field firstly and the second field next, like this: "echo ""cat "$file" | column -t | sort +1 -2 +0 -1echo ""echo "test is over"

输出内容如下:

=================================usage:# m: the number of fields from onecat file | sort +(m-1) -m +(m-1) -m# useful for pretty-printing displays# -s option is specifing a set of characters to be used to delimit columns for the -t optioncat file | column -t=================================export file named test.txt to current dir.the contents of test.txt are: 4  20121001-20121018  14523670   1    20121001-20121018    637368     3   20121001-20121018  308371     2         20121001-20121018         14832041   5  20121001-20121018   1.5469409E71  20121001-20121017  615608     3         20121001-20121017      305929     5  20121001-20121017  1.5271584E74     20121001-20121017   14350047   2  20121001-20121017      14655976   you could find that this file showing looks some confused !so, use column command to display it prettily4  20121001-20121018  145236701  20121001-20121018  6373683  20121001-20121018  3083712  20121001-20121018  148320415  20121001-20121018  1.5469409E71  20121001-20121017  6156083  20121001-20121017  3059295  20121001-20121017  1.5271584E74  20121001-20121017  143500472  20121001-20121017  14655976then, I wanna sort this file by the second field firstly and the second field next, like this: 1  20121001-20121017  6156082  20121001-20121017  146559763  20121001-20121017  3059294  20121001-20121017  143500475  20121001-20121017  1.5271584E71  20121001-20121018  6373682  20121001-20121018  148320413  20121001-20121018  3083714  20121001-20121018  145236705  20121001-20121018  1.5469409E7test is over

参考:

[1]  column http://linux.about.com/library/cmd/blcmdl1_column.htm

[2] sort http://linux.about.com/library/cmd/blcmdl1_sort.htm


原创粉丝点击