Search for a html tag and print the entire tag

I want to print from <fruits> to </fruits> tag which have <fruit> as mango. Also i want both <fruits> and </fruits> in output. Please help

eg.

<fruits>
<fruit id="111">mango<fruit>
.
another 20 lines
.
</fruits>

You can use sed to search between the start and end you are looking for:

$ sed -n "/<fruits>/,/<\/fruits>/p" file

edit: I missed the mango part :o Thanks, pamu :slight_smile:

awk '/<fruits>/{k=1;s=0;f=""}
    {f=f?f"\n"$0:$0}
    /mango/{s=1}
    /<\/fruits>/{k=0}
    k==0 && s==1{print f;}' file
1 Like
awk ' {$1=$1} /mango/ {print $0,"</fruits>"}' RS="</fruits>" file