博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
linux sort命令学习
阅读量:7033 次
发布时间:2019-06-28

本文共 2089 字,大约阅读时间需要 6 分钟。

linux sort命令以行为单位对文本文件进行排序。

接下来我们会以/tmp/sort_test.txt这个文本文件为例对sort命令的用法进行说明。
sh-# cat /tmp/sort_test.txt
10 my name is xulin
2 I like programming
3 what about you?
sh-#

1. 因为sort命令是按照字符比较的方式进行排序,所以便出现以下的结果,

sh-# cat /tmp/sort_test.txt | sort
10 my name is xulin
2 I like programming
3 what about you?
sh-#

2. 指定-n选项,这样就会以字符串数值大小进行排序,

sh-# cat /tmp/sort_test.txt | sort -n
2 I like programming
3 what about you?
10 my name is xulin
sh-#

3. 如果要反向排序,那就要指定-r选项了,

sh-# cat /tmp/sort_test.txt | sort -nr
10 my name is xulin
3 what about you?
2 I like programming
sh-#

4. 有时候用户希望能够按字段进行排序,其中-t选项用来指定域分隔符,-k用来指定位置,

sh-# cat /tmp/sort_test.txt | sort -t' ' -k 1,2
10 my name is xulin
2 I like programming
3 what about you?
sh-#
sh-# cat /tmp/sort_test.txt | sort -t' ' -k 2,3
2 I like programming
10 my name is xulin
3 what about you?
sh-#

sh-# ifconfig lo

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:9 errors:0 dropped:0 overruns:0 frame:0
          TX packets:9 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:456 (456.0 b)  TX bytes:456 (456.0 b)

sh-#

sh-# ifconfig lo | sort

          RX bytes:456 (456.0 b)  TX bytes:456 (456.0 b)

          RX packets:9 errors:0 dropped:0 overruns:0 frame:0
          TX packets:9 errors:0 dropped:0 overruns:0 carrier:0
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          collisions:0 txqueuelen:0
          inet addr:127.0.0.1  Mask:255.0.0.0
lo        Link encap:Local Loopback
sh-#

sh-# ifconfig lo | sort -t: -k 2 -n

          collisions:0 txqueuelen:0

lo        Link encap:Local Loopback
          RX packets:9 errors:0 dropped:0 overruns:0 frame:0
          TX packets:9 errors:0 dropped:0 overruns:0 carrier:0
          inet addr:127.0.0.1  Mask:255.0.0.0
          RX bytes:456 (456.0 b)  TX bytes:456 (456.0 b)
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
sh-#

sh-# ifconfig lo | sort -t: -k 2 -nr

          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX bytes:456 (456.0 b)  TX bytes:456 (456.0 b)
          inet addr:127.0.0.1  Mask:255.0.0.0
          TX packets:9 errors:0 dropped:0 overruns:0 carrier:0
          RX packets:9 errors:0 dropped:0 overruns:0 frame:0
lo        Link encap:Local Loopback
          collisions:0 txqueuelen:0

sh-#

是不是很方便呢?

 

转载地址:http://lsyal.baihongyu.com/

你可能感兴趣的文章
PHP 连接 MySQL
查看>>
314安装与网络配置预习+笔记
查看>>
python项目实战:免费下载kugou任意付费音乐
查看>>
消息中间件-activeMQ
查看>>
SQL SERVER2008数据库常识
查看>>
mac outlook 自动回复
查看>>
横屏竖屏
查看>>
Zabbix监控MySQL
查看>>
OSChina 周三乱弹 ——祖传的程序员?????
查看>>
OSChina 周五乱弹 —— 埃塞俄比亚的远房大表姐
查看>>
大华设备扫描工具
查看>>
twisted的异步库汇总-- mysql,redis,mongo,zmq,sockjs等
查看>>
Python3 基于asyncio的新闻爬虫思路
查看>>
af3.0学习使用和理解
查看>>
Linux vmstat命令实战详解
查看>>
输入字符串,输出字符串所有组合
查看>>
Python中 字典排序、列表排序
查看>>
ubuntu12.04 安装vnc
查看>>
我的友情链接
查看>>
前嗅ForeSpider脚本教程:基础对象(三)
查看>>