awk和sed实现匹配行内容和行号打印

 

  sed打印所有匹配行行号  sed -n  '/liu/=' aa

  sed打印匹配内容  sed -n  '/liu/p' aa

  sed打印匹配内容和匹配行号  sed -n  -e '/liu/='  -e '/liu/p' aa

  sed打印最后一个匹配内容的一行  sed -n  -e '/liu/='  -e '/liu/p' aa | tail -n1

 重点语法:

 sed中p 打印匹配行

        = 打印匹配行号

        -n  打印模式匹配的行

        -e 命令行进行编辑

 

以下为实现的是匹配行到文件末尾的内容打印,有不对的命令请指正一下

匹配行到文件末尾:
num=`sed -n '/匹配内容/=' file | tail -n1` #得到匹配最后一行的行号,再使用以下命令打印    sed -n $num',$p' file 
tac file | sed '/ccc/q' | tac
sed -n '/ccc/h;//!H;${g;p}' file
awk 'NR==FNR{if(/ccc/)p=FNR ;next} FNR>=p' file file
awk -vRS="" -vFS="ccc" '{print FS$NF}' urfile
awk '{s=s"\t"$0}END{gsub(".*ccc","ccc",s);gsub("\t","\n",s);print s}' aa 
sed -z 's/.*\nccc\n/ccc\n/'

sed ':1;$q;N;/ccc/,$D;b1' file
不包含匹配行ccc
sed -n ':1;N;$!b1;s#.*\nccc#ccc#p' file
包含匹配行ccc
awk -vRS='ccc'  'END{print RS,$0}' urfile

 

sed -i "s/plumber/streamsets-datacollector-basic-lib/g" */*
grep -E "abc.*def|xyz"
tail -f nohup.out |awk '/sum|/' 
tail /etc/services | awk '/warn/{a[$1]++}END{for(v in a)print a[v],v}'
tail -f ./nohup.out | awk '/warn/{print substr($4,1,2);total=total+$1;}'
tail -f ./nohup.out | awk '/warn/{a+=substr($4,1,2);b+=$a;print a}'
tail -f nohup.out | grep -v "md5"
tail -f nohup.out | grep -vE '(error|critical|warning)'
反向搜索:grep -n 'something' file | tail -n 1

文件中每一行前加时间:cat all.txt | while read line; do echo "[$(date "+%Y-%m-%d %H:%M:%S")] $line"; done

 

 


 

  

  

 

 

 

 

  • 2
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值