检查一行中是否存在所有多个字符串

Check if all multiple strings exist in one line

我有一个包含此信息的文件

IRE_DRO_Fabric_A drogesx0112_IRE_DRO_A_ISIL03_091_871
IRE_DRO_Fabric_A drogesx0112_IRE_DRO_A_NETAPP_7890_2D5_1D8
IRE_DRO_Fabric_A drogesx0112_SAN_A
IRE_DRO_Fabric_B drogesx0112_IRE_DRO_B_ISIL03_081_873
IRE_DRO_Fabric_B drogesx0112_IRE_DRO_B_NETAPP_7890_9D3_2D8
IRE_DRO_Fabric_B drogesx0112_SAN_B

并想检查每行是否找到多个字符串。试过这个命令,但它不工作。不确定当前文本类型是否可行?

grep 'drogesx0112.*ISIL03_091_871\|ISIL03_091_871.*drogesx0112' file  << tried this but not working
grep 'drogesx0112' file | grep 'ISIL03_091_871'                       << tried this but not working

寻找这个输出(我实际上是在寻找 string1(drogesx0112) 和 string2(ISIL03_091_871)

>grep 'drogesx0112.*ISIL03_091_871\|ISIL03_091_871.*drogesx0112' file # command

>IRE_DRO_Fabric_A drogesx0112_IRE_DRO_A_ISIL03_091_871       < output

所以我想检查 drogesx0112ISIL03_091_871 是否出现在文件的一行中。

如果您不是在寻找任何顺序,而只是想检查两个字符串是否出现在一行中,请尝试执行以下操作。

awk '/drogesx0112/ && /ISIL03_091_871/' Input_file


如果您正在寻找字符串序列:

  • 如果您的线路先有 drogesx0112,然后有 ISIL03_091_871,请尝试以下。

awk '/drogesx0112.*ISIL03_091_871/' Input_file
  • 如果您的线路先有 ISIL03_091_871,然后有 drogesx0112,请尝试以下。

awk '/ISIL03_091_871.*drogesx0112/' Input_file

简单的 awk

$ awk ' /drogesx0112/ && /ISIL03_091_871/ ' gafm.txt
IRE_DRO_Fabric_A drogesx0112_IRE_DRO_A_ISIL03_091_871
$

简单的 Perl

$ perl -ne ' print if /drogesx0112/ and /ISIL03_091_871/ ' gafm.txt
IRE_DRO_Fabric_A drogesx0112_IRE_DRO_A_ISIL03_091_871
$

这可能适合您 (GNU sed):

sed '/drogesx0112/!d;/ISIL03_091_871/!d' file

当前行不包含drogesx0112则删除,不包含ISIL03_091_871则删除

另一种方式:

sed -n '/drogesx0112/{/ISIL03_091_871/p}' file

第三个:

sed '/drogesx0112/{/ISIL03_091_871/p};d' file