首页 > 解决方案 > 从文件中提取两个模式之间的内容

问题描述

我想在The SUMMARYandEnd processing summary for first university以及The SUMMARYand之间提取以下内容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

任何使用sedawk命令的解决方案?

标签: bashawkpattern-matchingextract

解决方案


您能否尝试使用显示的示例进行以下、编写和测试。在这个解决方案中,为了清楚起见,我创建了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.

推荐阅读