简体   繁体   中英

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.

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM