E Bobrov
E Bobrov

Reputation: 135

Bash, grep between two lines with specified strings

example_file.txt:

    a43
    <un:Test1 id="U111">
    abc1
    cvb1
    bnm1
    </un:Test1>
    <un:Test1 id="U222">
    abc2
    cvb2
    bnm2
    </un:Test1>

I need all lines between <un:Test1 id="U111"> and first </un:Test1> only. Number of these lines is differ from one input file to another input file. I have tried

grep -E -A100000 '<un:Test1 id=\"U111\">' example_file.txt | grep -B100000 '</un:Test1>'

but it returns all strings bellow <un:Test1 id="U222"> also. I know that it`s better to use xmlparser to parse such kind of files but it is not allowed to install additional libs to the server so I can use grep, awk, sed etc. only. Help me please.

Upvotes: 0

Views: 335

Answers (1)

Kent
Kent

Reputation: 195059

Do you mean this?

sed -n '/<un:Test1 id="U111">/,/<\/un:Test1>/p' file

update with xmllint

If your input is xml, you can try:

xmllint --xpath "//*[local-name()='Test1'][@id='U111']" file.xml

Note: If you have different namespaces for same localname ("Test1"), you need add the namespace-uri()

Upvotes: 2

Related Questions