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]