常用的shell命令(cut sed sort awk nl head …)
- cut (既可以编辑文本,也可以通过管道符号|进行操作)
vim test.txt
hello:mother:fucker:with:my:name
hello:mother:fucker:with:my:name
hello:mother:fucker:with:my:name
hello:mother:fucker:with:my:name
cut -b 2,3 test.txt
el
el
el
el
cut -c 8 test.txt
cut -nb 1- test.txt
cut -b -1 test.txt
cut -d: -f 2 test.txt
mother
mother
mother
mother
vim test.txt
sohu 100 4500
baidu 100 5000
google 110 5000
guge 50 3000
sort -n test.txt
sort -rk2 test.txt
sort -rk3 test.txt
head -n 10 test.txt
- awk(默认按照TAB或者空格将文本中的每行进行分割,并且可以打印和计算)
ps -ef | awk '{print $2}'
-F:修改默认分隔符
-v:设置变量
-f:执行awk的脚本
head -n 1 test.txt | awk 'BEGIN{FS=":"} {print$1}'
或者 head -n 1 test.txt | awk -F: '{print$1,$2}'
- sed(可以针对文本执行插入、修改、删除、打印的操作,针对行的操作)
nl sed.txt
>1 LINUX
sed 1a\motherfucker sed.txt
>1 LINUX
>motherfucker
nl sed.txt | sed '1a fuck you'
>fuck you
>1 LINUX
nl sed.txt | sed '1i fuck you'
nl sed.txt | sed '2,5d'
nl sed.txt | sed '1,5c HELLO WORLD'
nl sed.txt | sed '2,5p'
nl sed.txt | sed -n '2p'
nl sed.txt | sed -n '/hello/p'
(a:添加,i:插入,c:替换,d:删除,p:打印)