繁体   English   中英

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

[英]awk with AND condition on two patterns misses in between lines which don not match

输入文件:

    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

或使用 GNU awk

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

说明:读取 Input_file 2 次。 第一次检查是否找到任一模式,然后将变量设置为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*****************

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM