Unix:正确地从行中提取内容

时间:2014-12-12 07:52:43

标签: unix awk grep cut

我们有以下几行:

ghselm|Linka|Alzelmo|BJKSKJ|SK1234|adm-approved-email*adm-approved-kmart*adm-approved-sears2*arb-user*fls-buyer*impt-user*mch-kminvmgmt*mdm-inventoryanalyst*pac_eligible*res-astpln-genacc*shc-shc-assoc*shc-shc-corp*sig-preview*sys-has-email*sys-has-kmart*sys-has-sears2*vpn-remoteaccess*adm-approved-email*adm-approved-kmart*adm-approved-sears2*arb-user*fls-buyer*impt-user*mch-kminvmgmt*mch-vpppfl*mdm-inventoryanalyst*pac_eligible*res-astpln-genacc*shc-shc-assoc*shc-shc-corp*sig-preview*mch-ga*sys-has-email*sys-has-kmart*sys-has-sears2*vpn-remoteaccess|

我们需要一个脚本,它可以为我们提供如下数据,只有mch类型的数据:

ghselm|Linka|Alzelmo|BJKSKJ|SK1234|mch-kminvmgmt*mch-kminvmgmt*mch-vpppfl*mch-ga|

1 个答案:

答案 0 :(得分:1)

这可以通过Perl完成。

$ perl -pe 's/[^|]*?(\*mch-\w+).*?(?=\*mch|\|)/\1/g' file
ghselm|Linka|Alzelmo|BJKSKJ|SK1234|*mch-kminvmgmt*mch-kminvmgmt*mch-vpppfl*mch-ga|

DEMO

要删除第一个*值之前的mch

$ perl -pe 's/[^|]*?(\*mch-\w+).*?(?=\*mch|\|)/\1/g' file | sed 's/|\*/|/'
ghselm|Linka|Alzelmo|BJKSKJ|SK1234|mch-kminvmgmt*mch-kminvmgmt*mch-vpppfl*mch-ga|