##Sed solution## #!/bin/bash
Sed solution
#!/bin/bash
sed -nr '
/^<[^<]*>$/ {
N
/^<([^<]*)>\n<\/\1>$/=
}
' "$1" | awk '{print "Output: "$NF - 1" -> line number"}'
###Explanation:###
Explanation:
sed/^<[^<]*>$/if we are have one open tag in the lineN- 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:
###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
##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"