You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Sed/awk移除指定模式间的换行符并提取目标内容

Hey there! Let's tackle this log parsing task. You want to extract the SQL commands that come right after Parsing command: and remove the line breaks between that and the next 2018- starting line. Here are a couple of straightforward ways to do this using common command-line tools:

方法1:使用awk实现

awk is perfect for this kind of line-by-line text processing. We'll track a "capture state" to grab the SQL content correctly:

/ Parsing command: / {
    # Remove everything before "Parsing command: " on this line
    sub(/.*Parsing command: /, "")
    # Enable capture mode
    capture = 1
}
capture == 1 {
    # If we hit the next timestamp line, stop capture and add a newline
    if (/^2018-/) {
        capture = 0
        print ""
    } else {
        # Print the line content without a newline (to concatenate)
        printf "%s", $0
    }
}

To use this:

  1. Save the script above as extract_sql.awk
  2. Run it against your log file (e.g., log.txt):
awk -f extract_sql.awk log.txt

方法2:单行awk命令(快速测试)

If you want to run it directly without saving a script:

awk '/ Parsing command: /{sub(/.*Parsing command: /,"");capture=1} capture==1{if(/^2018-/){capture=0;print ""}else{printf "%s",$0}}' log.txt

预期输出

Both methods will output exactly the SQL commands you need, one per line:

select * from table limit 10
select * from table group by column
select * from table order by column limit 20

Either of these should get you exactly the output you're looking for. Let me know if you need adjustments for edge cases like multi-line SQL or different timestamp formats!

内容的提问来源于stack exchange,提问作者Express Cars Inc

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 03:36:28