unix 常用命令 perl 实现 sed awk tr nl

sed

task sed perl
Replace 12 with twelve sed 's/12/twelve/g' perl -pe 's/12/twelve/g'
Replace the word sh with Bourne Shell sed -e 's/ sh / Bourne Shell /g' [1] perl -pe 's//bsh/b/Bourne Shell/g' [2]
Remove lines 2 to 4 from stream sed '2,4d' perl -nle 'print if $.<2 || $.>4'

edit awk

task awk perl
Print second field (whitespace-separated) awk '{print $2}' perl -lane 'print $F[1]'
Count lines starting with X awk '/^X/ {++x} END {print x}' perl -nle '++$x if /^X/; print $x if eof'
Add numbers in second column and print sum awk '{sum+=$2} END {print sum}' perl -lane '$sum+=$F[1]; print $sum if eof'

edit tr

task tr perl
ROT13 tr 'A-Za-z' 'N-ZA-Mn-za-m' perl -pe 'y/A-Za-z/N-ZA-Mn-za-m/'
Remove carriage return from DOS files [3] tr -d '/r' perl -pe 'tr//r//d'

edit grep

task grep perl
Print only lines containing 12 grep '12' perl -nle 'print if /12/'
Print only lines not containing 12 grep -v '12' perl -nle 'print if !/12/'

edit nl

task nl perl
Insert line numbers (lined up) nl -ba perl -nle 'printf "%6s  %s/n", $., $_'

edit Footnotes

  1. Won't match words at start/end of line
  2. Will match any perl word-boundary which consists of A-Za-z_ followed by a non A-Za-z_
  3. This method will remove all carriage return characters, not only those at end of line
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值