bash - 从文件中提取两个模式之间的内容
问题描述
我想在The SUMMARY
andEnd processing summary for first university
以及The SUMMARY
and之间提取以下内容End processing summary for second university
这是我的文件:
Logs here
...
...
...
More logs here
...
...
The SUMMARY
Total students: 1200
Total teachers: 10
Total subjects: 20
Total attendance: 12000
End processing summary for first university
Logs here
...
...
...
More logs here
...
...
The SUMMARY
Total students: 1500
Total teachers: 12
Total subjects: 15
Total attendance: 20000
End processing summary for second university
Logs here
...
...
...
More logs here
...
...
以下效果很好:
firstUniversity=$(awk '/The SUMMARY/ && ++n == 1, /End processing summary for first university/' < theLog.log)
secondUniversity=$(awk '/The SUMMARY/ && ++n == 2, /End processing summary for second university/' < theLog.log)
但是,有时 要么 要么the summary for first university
丢失the summary for second university
,上面的代码不起作用。
第一个大学街区不见了
Logs here
...
...
...
More logs here
...
...
Logs here
...
...
...
More logs here
...
...
The SUMMARY
Total students: 1500
Total teachers: 12
Total subjects: 15
Total attendance: 20000
End processing summary for second university
Logs here
...
...
...
More logs here
...
...
或者第二个大学街区不见了
Logs here
...
...
...
More logs here
...
...
The SUMMARY
Total students: 1200
Total teachers: 10
Total subjects: 20
Total attendance: 12000
End processing summary for first university
Logs here
...
...
...
More logs here
任何使用sed
或awk
命令的解决方案?
解决方案
您能否尝试使用显示的示例进行以下、编写和测试。在这个解决方案中,为了清楚起见,我创建了found_university
变量。
awk '
/The SUMMARY/{
found_summary=1
val=found_university=""
}
/End processing summary for first university|End processing summary for second university/{
found_university=1
if(found_university && found_summary){
print val ORS $0
}
val=found_university=found_summary=""
}
found_summary{
val=(val?val ORS:"")$0
}
' Input_file
可以尝试遵循不使用变量found_university
并简单地检查大学字符串出现的条件。
awk '
/The SUMMARY/{
found_summary=1
val=""
}
/End processing summary for first university|End processing summary for second university/{
if(found_summary){
print val ORS $0
}
val=found_summary=""
}
found_summary{
val=(val?val ORS:"")$0
}
' Input_file
说明:为上述代码添加详细级别的说明。请向右滚动以查看说明:)
awk ' ##Starting awk program from here.
/The SUMMARY/{ ##Checking condition if line has string The SUMMARY then do following.
found_summary=1 ##Setting found_summary as 1 here.
val="" ##Nullifying variable val here.
}
/End processing summary for first university|End processing summary for second university/{ ##Checking condition if university string present in line then do following.
if(found_summary){ ##Checking condition if found_summary is SET then do following.
print val ORS $0 ##Printing variable val ORS and current line here.
}
val=found_summary="" ##Nullifying variables val and found_summary here.
}
found_summary{ ##Checking condition if found_summary is SET then do following.
val=(val?val ORS:"")$0 ##Keep concatenating current line in val value.
}
' Input_file ##Mentioning Input_file name here.
推荐阅读
- dialog - Angular 8 Button type="file" 不会打开文件选择对话框
- python - pip3 install tensorflow-gpu 安装tensorflow 1.14.0版本
- java - 如何在地图中存储多个值(在运行时)并再次获取单个值
- azure - Azure Monitor / Log Analytics 指标警报查询
- java - maven中的package和groupId有什么区别
- javascript - 加入服务器时触发 discord.js channelCreate() 事件
- php - 如何使用 RAND() 优化慢速 SQL 语句
- sql-server - 在“选择”中更好地查询“选择”
- javascript - Angular formatDate() 将我的值设置为以前的值
- c# - 为什么 Visual Studio 会自动使用所有异步方法创建 ApiController?如何在其中创建带有同步方法的 ApiController?