当前位置:首页 >> 脚本专栏

Linux 中awk 提取包含某个关键字的段落

前提

AWK是一种处理文本文件的语言,是一个强大的文本分析工具。

本文将使用命令awk将具有某个关键字的段落提取出来。

准备数据

"Finalizer" #3 daemon prio=8 os_prio=0 tid=0x00007fb2dc1aa800 nid=0x63f6 in Object.wait() [0x00007fb2be61f000]
 java.lang.Thread.State: WAITING (on object monitor)
 at java.lang.Object.wait(Native Method)
 - waiting on <0x00000000d6708ed8> (a java.lang.ref.ReferenceQueue$Lock)
 at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:144)
 - locked <0x00000000d6708ed8> (a java.lang.ref.ReferenceQueue$Lock)
 at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:165)
 at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:216)

"Reference Handler" #2 daemon prio=10 os_prio=0 tid=0x00007fb2dc1a6000 nid=0x63f5 in Object.wait() [0x00007fb2be720000]
 java.lang.Thread.State: WAITING (on object monitor)
 at java.lang.Object.wait(Native Method)
 - waiting on <0x00000000d6706c00> (a java.lang.ref.Reference$Lock)
 at java.lang.Object.wait(Object.java:502)
 at java.lang.ref.Reference.tryHandlePending(Reference.java:191)
 - locked <0x00000000d6706c00> (a java.lang.ref.Reference$Lock)
 at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:153)

"main" #1 prio=5 os_prio=0 tid=0x00007fb2dc04b800 nid=0x63ef waiting on condition [0x00007fb2e3382000]
 java.lang.Thread.State: TIMED_WAITING (sleeping)
 at java.lang.Thread.sleep(Native Method)
 at com.test.Test.main(Test.java:16)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
 at java.lang.reflect.Method.invoke(Method.java:498)
 at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48)
 at org.springframework.boot.loader.Launcher.launch(Launcher.java:87)
 at org.springframework.boot.loader.Launcher.launch(Launcher.java:50)
 at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51)

段落提取

假设我们需要的关键字为 nid=0x63ef

keywords=0x63ef
## 以前后双换行符为一个段落
## 查询包含关键字nid=0x63ef的段落
awk 'BEGIN {RS = "\n\n+";ORS = "\n\n"} /'$awk,关键字段落,awk,提取关键字'/ {print $0}' test.log
## 查询不包含关键字nid=0x63ef的段落
awk 'BEGIN {RS = "\n\n+";ORS = "\n\n"} !/'$awk,关键字段落,awk,提取关键字'/ {print $0}' test.log

总结

以上所述是小编给大家介绍的Linux 中awk  提取包含某个关键字的段落,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!