常用的shell命令(cut sed sort awk nl head …)

常用的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 #查看占用第2个字符的文本内容,逐行显示
el
el
el
el
cut -c 8 test.txt #查看占用第8个字节的文本,适用于中文,英文,数字
cut -nb 1- test.txt
cut -b -1 test.txt

cut -d: -f 2 test.txt #将文本内容通过:进行分割,取第2列
mother
mother
mother
mother
  • sort(通过文本内容进行排序)
#准备文件
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(取出文本中的头几行)
head -n 10 test.txt #取出文本中的头10行
  • 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    #在第一行后面添加motherfucker
>1 LINUX
>motherfucker 

nl sed.txt | sed '1a fuck you' #在第一行后面重启一行添加fuck you
>fuck you 
>1 LINUX

nl sed.txt | sed '1i fuck you' #将第一行之前插入fuck you

nl sed.txt | sed '2,5d'        #将2~5行的文本删除并展示

nl sed.txt | sed '1,5c HELLO WORLD' #将1~5行文本内容替换为HELLOWORLD

nl sed.txt | sed '2,5p'        #只打印2~5行,但是其他附加行也会显示

nl sed.txt | sed -n '2p'       #只显示2-5行

nl sed.txt | sed -n '/hello/p' #只检索带有hello的行,并打印


(a:添加,i:插入,c:替换,d:删除,p:打印)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值