Skip to main content
Commonmark migration
Source Link

##Sed solution## #!/bin/bash

Sed solution

#!/bin/bash

sed -nr '
    /^<[^<]*>$/ {
        N
        /^<([^<]*)>\n<\/\1>$/=
    }
' "$1" | awk '{print "Output: "$NF - 1" -> line number"}'

###Explanation:###

Explanation:

  1. 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.
  2. 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"

##Sed solution## #!/bin/bash

sed -nr '
    /^<[^<]*>$/ {
        N
        /^<([^<]*)>\n<\/\1>$/=
    }
' "$1" | awk '{print "Output: "$NF - 1" -> line number"}'

###Explanation:###

  1. 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.
  2. 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"

Sed solution

#!/bin/bash

sed -nr '
    /^<[^<]*>$/ {
        N
        /^<([^<]*)>\n<\/\1>$/=
    }
' "$1" | awk '{print "Output: "$NF - 1" -> line number"}'

Explanation:

  1. 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.
  2. 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"
added 228 characters in body
Source Link
MiniMax
  • 4.2k
  • 1
  • 21
  • 36

##Sed solution## #!/bin/bash

sed -nr '
    /^<[^<]*>$/ {
        N
        /^<([^<]*)>\n<\/\1>$/=
    }
' "$1" | awk '{print "Output: "$NF - 1" -> line number"}'

###Explanation:###

  1. 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.
  2. 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"number";}
        line_num = 0;
    }
    NF == 2 {line_num = NR;}
' "$1"

##Sed solution## #!/bin/bash

sed -nr '
    /^<[^<]*>$/ {
        N
        /^<([^<]*)>\n<\/\1>$/=
    }
' "$1" | awk '{print "Output: "$NF - 1" -> line number"}'

###Explanation:###

  1. 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.
  2. 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##

#!/bin/bash

awk -F'[>/]' '
    line_num {
        if(NF == 3) {print "Output: " line_num " -> line number"}
        line_num = 0;
    }
    NF == 2 {line_num = NR;}
' "$1"

##Sed solution## #!/bin/bash

sed -nr '
    /^<[^<]*>$/ {
        N
        /^<([^<]*)>\n<\/\1>$/=
    }
' "$1" | awk '{print "Output: "$NF - 1" -> line number"}'

###Explanation:###

  1. 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.
  2. 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"
added 228 characters in body
Source Link
MiniMax
  • 4.2k
  • 1
  • 21
  • 36

##Sed solution## #!/bin/bash

#!/bin/bash

sed -nr '
    /^<[^<]*>$/ {
        N
        /^<([^<]*)>\n<\/\1>$/=
    }
' "$1" | awk '{print "Output: "$NF - 1" -> line number"}'

###Explanation:###

  1. 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.
  2. 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##

#!/bin/bash

awk -F'[>/]' '
    line_num {
        if(NF == 3) {print "Output: " line_num " -> line number"}
        line_num = 0;
    }
    NF == 2 {line_num = NR;}
' "$1"
#!/bin/bash

sed -nr '
    /^<[^<]*>$/ {
        N
        /^<([^<]*)>\n<\/\1>$/=
    }
' "$1" | awk '{print "Output: "$NF - 1" -> line number"}'

###Explanation:###

  1. 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.
  2. 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

##Sed solution## #!/bin/bash

sed -nr '
    /^<[^<]*>$/ {
        N
        /^<([^<]*)>\n<\/\1>$/=
    }
' "$1" | awk '{print "Output: "$NF - 1" -> line number"}'

###Explanation:###

  1. 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.
  2. 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##

#!/bin/bash

awk -F'[>/]' '
    line_num {
        if(NF == 3) {print "Output: " line_num " -> line number"}
        line_num = 0;
    }
    NF == 2 {line_num = NR;}
' "$1"
added 2 characters in body
Source Link
MiniMax
  • 4.2k
  • 1
  • 21
  • 36
Loading
added 472 characters in body
Source Link
MiniMax
  • 4.2k
  • 1
  • 21
  • 36
Loading
Source Link
MiniMax
  • 4.2k
  • 1
  • 21
  • 36
Loading