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

 

 


 

  

  

 

 

 

 

發佈了40 篇原創文章 · 獲贊 23 · 訪問量 17萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章