7、过滤文件内容

head

View the first n lines of data.

tail

View the last n lines of data.

sort

Organise the data into order.

nl

Print line numbers before data.

wc

Print a count of lines, words and characters.

cut

Cut the data into fields and only display the specified fields.

sed

Do a search and replace on the data.

uniq

Remove duplicate lines.

tac

Print the data in reverse order.

一、打印文件指定行数据

head [-number of lines to print] [path]

从头开始数,打印数据

dingyi@martin:~$ head -4 sample 
Fred apples 20
Susy oranges 5
Mark watermellons 12
Robert pears 4

head [-number of lines to print] [path]

从尾开始打印数据

dingyi@martin:~$ tail -3 sample 
Greg pineapples 3
Oliver rockmellons 2
Betty limes 14

二、整理

sort [-options] [path]

整理数据,默认为字母排序

dingyi@martin:~$ sort sample 
Anne mangoes 7
Betty limes 14
Fred apples 20
Greg pineapples 3
Lisa peaches 7
Mark grapes 39
Mark watermellons 12
Oliver rockmellons 2
Robert pears 4
Susy oranges 12
Susy oranges 5
Terry oranges 9

三、打印行号

nl [-options] [path]

dingyi@martin:~$ nl sample 
     1	Fred apples 20
     2	Susy oranges 5
     3	Mark watermellons 12
     4	Robert pears 4
     5	Terry oranges 9
     6	Lisa peaches 7
     7	Susy oranges 12
     8	Mark grapes 39
     9	Anne mangoes 7
    10	Greg pineapples 3
    11	Oliver rockmellons 2
    12	Betty limes 14

四、计算word count

wc [-options] [path]

dingyi@martin:~$ wc sample 
 12  36 197 sample

五、只打印几列

cut [-options] [path]

dingyi@martin:~$ cut -f 1,2 -d ' ' sample 
Fred apples
Susy oranges
Mark watermellons
Robert pears
Terry oranges
Lisa peaches
Susy oranges
Mark grapes
Anne mangoes
Greg pineapples
Oliver rockmellons
Betty limes

注意:

  • -f 后面代表打印的列
  • -d后面代表用什么符号分隔列,这里是空格,cut默认是tab

六、查找和替换

sed [path]

dingyi@martin:~$ sed 's/oranges/es/g' sample 
Fred apples 20
Susy es 5
Mark watermellons 12
Robert pears 4
Terry es 9
Lisa peaches 7
Susy es 12
Mark grapes 39
Anne mangoes 7
Greg pineapples 3
Oliver rockmellons 2
Betty limes 14

s代表替换,g代表全局。前面的字符是搜寻的单词,后面的是替换的单词。

七、删除重复行

uniq [options] [path]

八、倒着打印

tac [path]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值