仅返回匹配模式后的行的一部分

时间:2019-04-25 09:42:46

标签: linux shell unix

我需要一个以*(星号)分隔的文件有多行的COMMAND,如下所述。我想搜索以NM1*IL开头的行并提取最后一列。下面是显示的示例。

$ cat temp.txt
NM1*IL*1*RESTAR*FRENNY*M***MI*99358358~
N3*2164 Boeing Avenue~
N4*NEW DELHI*CA*94114~
DMG*D0*19550610*F~
NM1*PR*2*KFHP*****PI*94135~
NM1*IL*1*STAR*FRENY*M***MI*99358958~
NM1*IL*1*RESTA*ANNIE*M***MI*993583589~

我希望输出如下

99358358
99358958
993583589

2 个答案:

答案 0 :(得分:0)

grep 'NM1[*]IL' temp.txt | awk -F'*' '{print $NF}' | sed 's/~$//'

您可以运行该管道的各个部分(直到|(不包括grep 'NM1[*]IL'),以查看特定命令对输入的作用。

grep-过滤输入行,以便仅保留与该模式匹配的行。 *以正则表达式作为参数,而[]是特殊的正则表达式字符(表示其后的符号为0或更多),因此需要转义。我在这里(NM1\*IL使用了字符类,因为我认为它很整洁。可以使用awk -F'*'达到相同的效果。

-F*指定字段分隔符,我将其设置为*。 Awk将每条输入行分为 fields ,因此两个'{print $NF}'之间的任何内容都是awk的字段。

NF是awk程序,现在将在其输入上执行。它说“对于所有输入行,打印带有数字NF的字段”,其中sed 's/~$//'是一个特殊的awk变量,它代表“字段数”。所以它的作用是打印最后一个字段。

~ —仅删除结尾的$tr -d '~'表示行尾。因此,此sed使用空字符串替换EOL之前的所有tilda。可以写javax.security.auth.login.LoginException: null (68) at com.sun.security.auth.module.Krb5LoginModule.attemptAuthentication(Krb5LoginModule.java:804) at com.sun.security.auth.module.Krb5LoginModule.login(Krb5LoginModule.java:617) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at javax.security.auth.login.LoginContext.invoke(LoginContext.java:755) at javax.security.auth.login.LoginContext.access$000(LoginContext.java:195) at javax.security.auth.login.LoginContext$4.run(LoginContext.java:682) at javax.security.auth.login.LoginContext$4.run(LoginContext.java:680) at java.security.AccessController.doPrivileged(Native Method) at javax.security.auth.login.LoginContext.invokePriv(LoginContext.java:680) at javax.security.auth.login.LoginContext.login(LoginContext.java:587) at com.teamcenter.net.auth.NegotiateAuthenticator.acquireTGT(NegotiateAuthenticator.java:235) at com.teamcenter.net.auth.NegotiateAuthenticator.getTGT(NegotiateAuthenticator.java:200) at com.teamcenter.net.auth.NegotiateAuthenticator. (NegotiateAuthenticator.java:107) at com.teamcenter.net.auth.NegotiateScheme31$NegotiateAuthenticatorAction.run(NegotiateScheme31.java:350) at com.teamcenter.net.auth.NegotiateScheme31$NegotiateAuthenticatorAction.run(NegotiateScheme31.java:337) at java.security.AccessController.doPrivileged(Native Method) at javax.security.auth.Subject.doAs(Subject.java:422) at com.teamcenter.net.auth.NegotiateScheme31.authenticate(NegotiateScheme31.java:181) at org.apache.commons.httpclient.HttpMethodDirector.authenticateHost(HttpMethodDirector.java:282) at org.apache.commons.httpclient.HttpMethodDirector.authenticate(HttpMethodDirector.java:234) at org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMethodDirector.java:170) at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:397) at com.teamcenter.net.proxy.http.ProxiedHttpClient.executeDirectConfig(ProxiedHttpClient.java:707) at com.teamcenter.net.proxy.http.ProxiedHttpClient.httpConnect(ProxiedHttpClient.java:666) at com.teamcenter.net.proxy.http.ProxiedHttpClient.tryDest(ProxiedHttpClient.java:631) at com.teamcenter.net.proxy.http.ProxiedHttpClient.executeMethodInternal(ProxiedHttpClient.java:509) at com.teamcenter.net.proxy.http.ProxiedHttpClient.executeMethod(ProxiedHttpClient.java:325) at com.teamcenter.net.tcserverproxy.http.HttpTSPProcessor.executeMethod(HttpTSPProcessor.java:614) at com.teamcenter.net.tcserverproxy.http.HttpTSPProcessor.processRequest(HttpTSPProcessor.java:274) at com.teamcenter.net.tcserverproxy.TSPReplyHandler.processTSPRequest(TSPReplyHandler.java:745) at com.teamcenter.net.tcserverproxy.TSPRequestProcessor.processRequest(TSPRequestProcessor.java:125) at com.teamcenter.net.tcserverproxy.TSPReplyHandler.runInternal(TSPReplyHandler.java:253) at com.teamcenter.fms.util.threads.BaseProcessor.run(BaseProcessor.java:152) at java.lang.Thread.run(Thread.java:745) Caused by: KrbException: null (68) at sun.security.krb5.KrbAsRep. (KrbAsRep.java:76) at sun.security.krb5.KrbAsReqBuilder.send(KrbAsReqBuilder.java:316) at sun.security.krb5.KrbAsReqBuilder.action(KrbAsReqBuilder.java:361) at com.sun.security.auth.module.Krb5LoginModule.attemptAuthentication(Krb5LoginModule.java:766) ... 36 more Caused by: KrbException: Identifier doesn't match expected value (906) at sun.security.krb5.internal.KDCRep.init(KDCRep.java:140) at sun.security.krb5.internal.ASRep.init(ASRep.java:64) at sun.security.krb5.internal.ASRep. (ASRep.java:59) at sun.security.krb5.KrbAsRep. (KrbAsRep.java:60) ... 39 more ,但是此sed更为精确(仅在EOL之前去除tilda)。

grep(1)awk(1p)sed(1)都是标准的UNIX命令行实用程序。研究手册页以获取有关如何使用它们的更多信息。

答案 1 :(得分:0)

由于您的问题被标记为linux,因此我认为您正在使用GNU grep:

$ grep -oP '^NM1\*IL.*\*\K.*(?=~)' /tmp/file
99358358
99358958
993583589

这是使用pcre语法以及后置和先行零宽度断言-匹配到\K且在(?=...)内部的内容将不包含在其中使用-o选项时的输出。