简体   繁体   中英

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

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

Command:

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

Expected_Result:

    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*****************

EDIT: Since OP's samples are changed so adding this solution now.

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


Initial solution: Where as per OP's samples it was looking if even a single instance of matched regex is found print whole line is assumed here, with awk could you please try following.

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

OR with GNU awk :

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

Explanation: Reading Input_file 2 times. First time checking if either of the pattern is found then set variable to 1 . In 2nd time Input_file reading simply checking if found is NOT set then exiting else it will print whole Input_file.

You can use this awk solution:

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*****************

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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