Skip to main content
added 14 characters in body
Source Link
guest_7
  • 5.8k
  • 1
  • 8
  • 13

Perl can be used in this scenario and due to its powerful regex capabilities can sort it out in a true one-liner.

perl in slurp mode -0777 with autoprint current record turned ON -p, we examine all lines that are followed by ABC line or eof. In both scenarios slip in the string "END" at the beginning, overwriting upto 3 leading whitespace characters.

perl -0777pe ' s/^(?!ABC)\h{0,3}(?=.*\n(?:ABC|\z))/END/mg; ' file 

Study of the regex:

  • Look for a line that doesn't begin w/ ABC ^(?!ABC)
  • Has none to 3 whitespace(atleast) in the beginning \h{0,3}
  • From this vantage point point if we are able to see till the end of current line (?=.*\n(?:ABC|\z) and from there on the next line Either begin withwhich begins with ABC or it's the eof.
  • Perform the substitution. Understand that lookarounds do not consume data but are there to assert the match locations.

Perl can be used in this scenario and due to its powerful regex capabilities can sort it out in a true one-liner.

perl in slurp mode -0777 with autoprint current record turned ON -p, we examine all lines that are followed by ABC line or eof. In both scenarios slip in the string "END" at the beginning, overwriting upto 3 leading whitespace characters.

perl -0777pe ' s/^(?!ABC)\h{0,3}(?=.*\n(?:ABC|\z))/END/mg; ' file 

Study of the regex:

  • Look for a line that doesn't begin w/ ABC ^(?!ABC)
  • Has none to 3 whitespace in the beginning \h{0,3}
  • From this vantage point point if we are able to see till the end of current line (?=.*\n(?:ABC|\z) and there on the next line Either begin with ABC or it's the eof.
  • Perform the substitution. Understand that lookarounds do not consume data but are there to assert the match locations.

Perl can be used in this scenario and due to its powerful regex capabilities can sort it out in a true one-liner.

perl in slurp mode -0777 with autoprint current record turned ON -p, we examine all lines that are followed by ABC line or eof. In both scenarios slip in the string "END" at the beginning, overwriting upto 3 leading whitespace characters.

perl -0777pe ' s/^(?!ABC)\h{0,3}(?=.*\n(?:ABC|\z))/END/mg; ' file 

Study of the regex:

  • Look for a line that doesn't begin w/ ABC ^(?!ABC)
  • Has none to 3 whitespace(atleast) in the beginning \h{0,3}
  • From this vantage point point if we are able to see till the end of current line (?=.*\n(?:ABC|\z) and from there on the next line which begins with ABC or it's the eof.
  • Perform the substitution. Understand that lookarounds do not consume data but are there to assert the match locations.
Source Link
guest_7
  • 5.8k
  • 1
  • 8
  • 13

Perl can be used in this scenario and due to its powerful regex capabilities can sort it out in a true one-liner.

perl in slurp mode -0777 with autoprint current record turned ON -p, we examine all lines that are followed by ABC line or eof. In both scenarios slip in the string "END" at the beginning, overwriting upto 3 leading whitespace characters.

perl -0777pe ' s/^(?!ABC)\h{0,3}(?=.*\n(?:ABC|\z))/END/mg; ' file 

Study of the regex:

  • Look for a line that doesn't begin w/ ABC ^(?!ABC)
  • Has none to 3 whitespace in the beginning \h{0,3}
  • From this vantage point point if we are able to see till the end of current line (?=.*\n(?:ABC|\z) and there on the next line Either begin with ABC or it's the eof.
  • Perform the substitution. Understand that lookarounds do not consume data but are there to assert the match locations.