我有一个日志文件,在一系列行的末尾,您可以查看此块是否相关。现在我正在寻找像 sed 这样的命令来删除以“Content-Length: 0”结尾并以该行之前的最后一个“--”开头的块。
我试过了, sed -n "/--/,/Content-Length: 0/d"但这需要第一个“--”和第一个“Content-Length:0”并将其删除。
前任 :
line 1 "--"
line 2
line 3 "Content-Length: 20"
line 4 "--"
line 5
line 6 "Content-Length: 0"
Run Code Online (Sandbox Code Playgroud)
我想删除第 4,5 和 6 行而不是第 1 到 6 行
我怎样才能做到这一点?
使用tac而不是工作的答案cat!但最终我想在tail -f建筑中使用它
这是使用的一种方法GNU sed:
sed -n '/--/,/Content-Length: 0/ { H; /Content-Length: 0/ { g; s/\n\(.*\)\n.*--.*/\1/p } }'
Run Code Online (Sandbox Code Playgroud)
结果:
line 1 "--"
line 2
line 3 "Content-Length: 20"
Run Code Online (Sandbox Code Playgroud)
解释:
Match between the pattern range. Append this range to the hold space. On the last
line of the range, copy the hold space to pattern space to work with it. Then use
find/replace regex to remove everything after the last occurrence of '--'. HTH.
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2112 次 |
| 最近记录: |