在两个模式上带有AND条件的awk在不匹配的行之间未命中



Input_File:

2020-09-24 12:08:18,085.085 INFO     38210f97f184 lCpkydbL6wlNNi0 94rXXKaa3KLamiJ prep - Benchmarking Data function ENDED
2020-09-24 12:08:18,085.085 INFO     38210f97f184 lCpkydbL6wlNNi0 94rXXKaa3KLamiJ prep -                                                      
Average_Unit_Price_00         0.000000         Average_Unit_Price
Competition_Media_00          0.000000          Competition_Media
2020-09-24 12:08:18,229.229 INFO     38210f97f184 lCpkydbL6wlNNi0 94rXXKaa3KLamiJ prep - Configure JSON function ENDED
2020-09-24 12:08:18,302.302 INFO     38210f97f184 lCpkydbL6wlNNi0 94rXXKaa3KLamiJ prep - Writing into CSV STARTED
2020-09-24 11:44:03,070.070 ERROR    38210f97f184 lCpkydbL6wlNNi0 94rXXKaa3KLamiJ prep - Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/pandas/core/indexes/base.py", line 2646, in get_loc
return self._engine.get_loc(key)
File "pandas/_libs/hashtable_class_helper.pxi", line 1626, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: None
2020-09-24 12:08:18,503.503 INFO     38210f97f184 lCpkydbL6wlNNi0 94rXXKaa3KLamiJ prep - *************Data Prep Step ENDED*****************
2020-09-25 07:39:09,008.008 INFO     8ccaf8d81212 LWpHmfMDcu03xp5 J0ps4NHADvsyIFt model2 - model2 Time: 0:00:03.281901 
2020-09-25 07:39:09,008.008 INFO     8ccaf8d81212 LWpHmfMDcu03xp5 J0ps4NHADvsyIFt model2 -
2020-09-25 07:39:09,010.010 INFO     8ccaf8d81212 LWpHmfMDcu03xp5 J0ps4NHADvsyIFt model2 - Generating CModel_Meas_Agg STARTED
2020-09-25 07:39:09,019.019 INFO     8ccaf8d81212 LWpHmfMDcu03xp5 J0ps4NHADvsyIFt model2 - CModel_Meas_All file has 21 observations

命令:

awk '/38210f97f184/&&/94rXXKaa3KLamiJ/' Input_File

预期结果:

2020-09-24 12:08:18,085.085 INFO     38210f97f184 lCpkydbL6wlNNi0 94rXXKaa3KLamiJ prep - Benchmarking Data function ENDED
2020-09-24 12:08:18,085.085 INFO     38210f97f184 lCpkydbL6wlNNi0 94rXXKaa3KLamiJ prep -                                                      
Average_Unit_Price_00         0.000000         Average_Unit_Price
Competition_Media_00          0.000000          Competition_Media
2020-09-24 12:08:18,229.229 INFO     38210f97f184 lCpkydbL6wlNNi0 94rXXKaa3KLamiJ prep - Configure JSON function ENDED
2020-09-24 12:08:18,302.302 INFO     38210f97f184 lCpkydbL6wlNNi0 94rXXKaa3KLamiJ prep - Writing into CSV STARTED
2020-09-24 11:44:03,070.070 ERROR    38210f97f184 lCpkydbL6wlNNi0 94rXXKaa3KLamiJ prep - Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/pandas/core/indexes/base.py", line 2646, in get_loc
return self._engine.get_loc(key)
File "pandas/_libs/hashtable_class_helper.pxi", line 1626, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: None
2020-09-24 12:08:18,503.503 INFO     38210f97f184 lCpkydbL6wlNNi0 94rXXKaa3KLamiJ prep - *************Data Prep Step ENDED*****************

编辑: 由于OP的样本已更改,因此现在添加此解决方案。

awk '
FNR==NR{
if($0~/38210f97f184.*94rXXKaa3KLamiJ/ && ++count==1){
start=FNR
}
if($0~/38210f97f184.*94rXXKaa3KLamiJ/ && count>1){
end=FNR
}
next
}
FNR>=start && FNR<=end
'  Input_file  Input_file


初始解决方案:根据OP的样本,如果找到匹配正则表达式的单个实例,则假设打印整行,使用awk,您可以尝试以下操作吗。

awk '
FNR==NR{
if($0~/38210f97f184 .* 94rXXKaa3KLamiJ/ || $0~ /94rXXKaa3KLamiJ .* 38210f97f184/){
found=1
}
next
}
!found{
exit
}
1
' Input_file  Input_file

或与GNUawk:

awk '
FNR==NR{
if($0~/38210f97f184 .* 94rXXKaa3KLamiJ/ || $0~ /94rXXKaa3KLamiJ .* 38210f97f184/){
found=1
nextfile
}
}
!found{
exit
}
1
' Input_file  Input_file

解释:读取Input_file两次。第一次检查是否找到任何一个模式,然后将变量设置为1。在第二次读取Input_file时,只需检查是否未设置find,然后退出,它将打印整个Input_file。

您可以使用此awk解决方案:

awk '!/^[0-9]{4}(-[0-9]{2}){2} / && p;
/^[0-9]{4}(-[0-9]{2}){2} / && p = (/38210f97f184/ && /94rXXKaa3KLamiJ/)' file
2020-09-24 12:08:18,085.085 INFO     38210f97f184 lCpkydbL6wlNNi0 94rXXKaa3KLamiJ prep - Benchmarking Data function ENDED
2020-09-24 12:08:18,085.085 INFO     38210f97f184 lCpkydbL6wlNNi0 94rXXKaa3KLamiJ prep -
Average_Unit_Price_00         0.000000         Average_Unit_Price
Competition_Media_00          0.000000          Competition_Media
2020-09-24 12:08:18,229.229 INFO     38210f97f184 lCpkydbL6wlNNi0 94rXXKaa3KLamiJ prep - Configure JSON function ENDED
2020-09-24 12:08:18,302.302 INFO     38210f97f184 lCpkydbL6wlNNi0 94rXXKaa3KLamiJ prep - Writing into CSV STARTED
2020-09-24 11:44:03,070.070 ERROR    38210f97f184 lCpkydbL6wlNNi0 94rXXKaa3KLamiJ prep - Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/pandas/core/indexes/base.py", line 2646, in get_loc
return self._engine.get_loc(key)
File "pandas/_libs/hashtable_class_helper.pxi", line 1626, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: None
2020-09-24 12:08:18,503.503 INFO     38210f97f184 lCpkydbL6wlNNi0 94rXXKaa3KLamiJ prep - *************Data Prep Step ENDED*****************

相关内容

  • 没有找到相关文章

最新更新