========不定期更新该页面========

1、只保留匹配的内容

日志片段:

"duration":0.03,"scheme":"https","sessionId":"19B0F3DD149FF8D81A4B4D68D53569F6","userId":"60800","path":"/mobile/index.html"


sed -n 's/^.*\("userId":"[0-9]*"\).*$/\1/p' logfile.txt


首先想到搞个python,后来又觉得麻烦了点,果断google得来上面的命令

python可以这样:

import re
f = open("logfile.txt")
p = re.compile(r'"userId":"\d+"')
for line in f:
  m = p.search(line)
  print m.group(0)


2、查看指定行范围的内容

sed -n '2,5p' backtask.py


3、在匹配行的下面插入

sed '0,/Matched Keyword/s//Matched Keyword\nNew Inserted Line/' myfile.txt
sed -i '/tracker_server=10.45.11.37:22122/a tracker_server=10.45.11.37:22123' /etc/fdfs/storage.conf