Grep包含正则表达式的多个字符串

时间:2018-03-12 14:18:53

标签: bash grep

我试图从XML(Input_file.txt)中打印出看起来像这样的特定行(新行中的每个标记)

 <?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
 <INVOIC02>
 <IDOC>
 <EDI_DC40>
 <DOCNUM/>
 <DOCREL/>
 <IDOCTYP>INVOIC02</IDOCTYP>
 <CIMTYP/>
 <MESTYP>INVOIC</MESTYP>
 <SNDPOR>SAPP01</SNDPOR>
 <SNDPRT/>
 <SNDPFC/>
 <SNDPRN>ALE400</SNDPRN>
 <SNDSAD/>
 <SNDLAD/>
 <RCVPOR>XMLINVOICE</RCVPOR>
 <RCVPRT>KU</RCVPRT>
 </EDI_DC40>
 </IDOC>
 </INVOIC02>

我希望我的输出如下:

 <?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
 <EDI_DC40>
 <DOCNUM/>
 <IDOCTYP>INVOIC02</IDOCTYP>
 <MESTYP>INVOIC</MESTYP>
 <SNDPOR>SAPP01</SNDPOR>
 </EDI_DC40>
 </IDOC>
 </INVOIC02>

我能够以这种方式打印特定的行,例如

 grep '^.*<IDOCTYP>.*</IDOCTYP>$' Input_file.txt

打印

 <IDOCTYP>INVOIC02</IDOCTYP>

但是如何在同一个grep命令中打印出每一行并生成预期的输出? (尝试与\ |分开,但没有为我工作)。提前谢谢。

1 个答案:

答案 0 :(得分:0)

假设您在 test.xml 中输入,这将在Bash中起作用。

Sub Main
    Dim responseFromServer = "{
    ""branches"":  {
        ""1"": {
            ""member_id"": ""-16"",
            ""branch_id"": ""1""
        },
        ""2"": {
            ""member_id"": ""-16"",
            ""branch_id"": ""1""
        }
    }
}"

    Dim jss As New JavaScriptSerializer()
    Dim data As OfficeCheckJsonResponse =
        jss.Deserialize(Of OfficeCheckJsonResponse)(responseFromServer)

    For Each branch As KeyValuePair(Of String, Branch) In data.branches
        Console.WriteLine(Int32.Parse(branch.Key))
        Console.WriteLine(branch.Value)
    Next
End Sub

Public Class OfficeCheckJsonResponse
    Public Property branches As Dictionary(Of String, Branch)
End Class

Public Class Branch
    Property member_id As String
    Property branch_id As String
End Class

或者也许......

grep  '<?xml\|<EDI_DC40>\|<DOCNUM>\|<IDOCTYP>\|<MESTYP>\|<SNDPOR>\|<EDI_DC40>\|<IDOC>\|<INVOIC02>' test.xml

或者......

awk '/<?xml|<EDI_DC40>|<DOCNUM>|<IDOCTYP>|<MESTYP>|<SNDPOR>|<EDI_DC40>|<IDOC>|<INVOIC02>/'  test.xml

问候!