如何用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:
- Save the script above as
extract_sql.awk - 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

