Grep支持多模式多选项的多搜索

Grep支持多模式多选项的多搜索,grep,pattern-matching,Grep,Pattern Matching,内容如下: client_name: client_since: client_address: client_city: client_country_code: contact_number: <some number 1> <some number 2> client_account_number: client_bank_name: 客户端名称: 客户单位自: 客户地址: 客户城市: 客户\国家\代码: 联络电话: 客户账号: 客户\

内容如下:

client_name:
client_since:
client_address:
client_city:
client_country_code:
contact_number:
       <some number 1>
       <some number 2>
client_account_number:
client_bank_name:
客户端名称:
客户单位自:
客户地址:
客户城市:
客户\国家\代码:
联络电话:
客户账号:
客户\银行\名称:
我需要打印带有“客户名称:”、“客户城市:”、“客户国家代码::”图案/字符串的行。我还需要在后面打印带有“联系人号码:”的行&2行,以便也打印这2条动态行

如何通过一个线性命令实现这一点

$ cat data 
client_name:
client_since:
client_address:
client_city:
client_country_code:
contact_number:
       landline:
       mobile:
client_account_number:
client_bank_name:
$ egrep '^(contact_number|\s*landline|\s*mobile|client_(name|city|country_code))' data 
client_name:
client_city:
client_country_code:
contact_number:
       landline:
       mobile:
$
如果联系人号码下面的两行是动态的,那么您可以使用下面的grep组合:

     grep -E "client_name:|client_city:|client_country_code:" data  && grep -A2 "contact_number:" data
client_name:
client_city:
client_country_code:
contact_number:
       landline:
       mobile:

grep -E "client_name:|client_city:|client_country_code:|`grep -A2 contact_number: data`" data
这里有一个肮脏的方法来实现你在评论中的要求:

grep -Ee "client_name:|client_city:|client_country_code:|`awk '/contact_number/{print; nr[NR+2]; next}; NR in nr' data`" data
client_name:
client_city:
client_country_code:
contact_number:
       mobile:

到目前为止你都试了些什么?@piyushjaiswal grep with-e&-A,grep with-f&-A。嗨,我已经修改了这个问题。联系人号码下方的行是动态的。我只需要在联系电话号码后打印两行。请让我知道。谢谢,我已经修改了这个问题。联系人号码下方的行是动态的。我只需要在联系电话号码后打印两行。请让我知道。谢谢你!它工作得很好。如何跳过联系人号码的第一行而只打印第二行?此外,我希望将上述内容存储在一个变量中。那么我如何从变量中搜索呢@随机用户更新。请针对不同的问题分别提问。
grep -Ee "client_name:|client_city:|client_country_code:|`awk '/contact_number/{print; nr[NR+2]; next}; NR in nr' data`" data
client_name:
client_city:
client_country_code:
contact_number:
       mobile: