Sed solution
#!/bin/bash
sed -nr '
/^<[^<]*>$/ {
N
/^<([^<]*)>\n<\/\1>$/=
}
' "$1" | awk '{print "Output: "$NF - 1" -> line number"}'
Explanation:
sed
/^<[^<]*>$/ if we are have one open tag in the line
N - append the next line of input into the pattern space.
/^<([^<]*)>\n<\/\1>$/ and check, does the next line have the equivalent closed tag.
- if so, print this line number by
= command. Bear in mind, that it is the closed tag line number. We should decrease it by one in further.
awk - decreases the line number and print it in the message string.
Testing:
Input
<a>
</a>
<a>
<b></b>
<c></c>
<c>
</c>
</a>
Output
./empty_tag.sh input.txt
Output: 1 -> line number
Output: 6 -> line number
AWK solution
Usage: ./empty_tag.sh input.txt
#!/bin/bash
awk -F'[>/]' '
line_num {
if(NF == 3) {print "Output: " line_num " -> line number";}
line_num = 0;
}
NF == 2 {line_num = NR;}
' "$1"
$HOME).xmlstarletcould be compiled on another compatible system and installed under $HOME...but doing that on such a tightly controlled system will probably annoy the admins. It may even be a sackable offence to install any software on a production server without authorisation. Some sites are extremely rigid about change management, and/or have strict regulations they have to comply with (e.g. banks)