linux shell之 sort命令的使用

sort -t: file # 以默认的方式排序,:表示域的分隔符,若不指定-t,那么默认分隔符为空格
sort -t: -k3 file 根据第3域进行对file文件排序
sort -t: -k3n file 根据第3域的数字大小进行对file文件排序
sort -t: -k3nr file 根据第3域的数字大小进行对file文件逆向排序
sort -t: -u file 对file 文件进行排序,并去掉重复的记录
sort -t: -k3n -o file1 file 根据第3域的数字大小进行对file文件排序,将排序结果输出到文件file1
sort -t: -c file 测试file文件是否按默认方式排序
sort -t: -k3n -c file 测试file文件是否按第三域进行排序
sort -t: -m file1 file2,将排序好的第一个文件和第二个文件合并

cat PROFESSOR.db
J Luo
Southeast University
Nanjing,China


Y Zhang
Victory University
Melbourne, Australia


D Hou
Beijing University
Beijing, China


B Liu
Shanghai Jiaotong University
Shanghai, China

C Lin
University of Toronato
Toronto, Cannada
cat PROFESSOR.db | awk -v RS="" '{gsub("\n", "@");print}'
J Luo@Southeast University@Nanjing,China
Y Zhang@Victory University@Melbourne, Australia
D Hou@Beijing University@Beijing, China
B Liu@Shanghai Jiaotong University@Shanghai, China
C Lin@University of Toronato@Toronto, Cannada
cat PROFESSOR.db | awk -v RS="" '{gsub("\n", "@");print}' | sort
B Liu@Shanghai Jiaotong University@Shanghai, China
C Lin@University of Toronato@Toronto, Cannada
D Hou@Beijing University@Beijing, China
J Luo@Southeast University@Nanjing,China
Y Zhang@Victory University@Melbourne, Australia

cat PROFESSOR.db | awk -v RS="" '{gsub("\n", "@");print}' | sort | awk -v ORS="\n\n" '{gsub("@", "\n");print}'
B Liu
Shanghai Jiaotong University
Shanghai, China

C Lin
University of Toronato
Toronto, Cannada

D Hou
Beijing University
Beijing, China

J Luo
Southeast University
Nanjing,China

Y Zhang
Victory University
Melbourne, Australia

上一篇:Truncated incorrect DOUBLE value: ‘是’


下一篇:MySQL