Skip to main content
added 39 characters in body
Source Link
JoL
  • 5k
  • 1
  • 20
  • 37

Since you're using -z, ^ corresponds to the beginning of each optionally-null-terminated record (i.e. the beginning of the file, and after each null byte but the last if it's the last byte). That means that the first ^ won't catch [etc] when it's at the start of any other line but the first. You can use (\n|^) for that. For the second ^, ^ will never match after the beginning of the match, so \n^ will always fail. Finally, there's the point that jesse_b pointed out, that [etc] is regex for one character of those 3.

Those are the issues with the pattern, now the remaining issue is with the substitution, where you're lacking the trailing newline. sed would normally add it itself (more accurately, restore it from the input, since the last terminator is optional), but since you're using -z, the "separator"/terminator is set to a null byte rather than newline.

Once you fix those 4 issues (and added -E to avoid some escapes), you have:

sed -Ezi '/(\n|^)\[etc\]\ntransient = true/!s/$/[etc]\ntransient = true\n/' file

Since you're using -z, ^ corresponds to the beginning of each optionally-null-terminated record (i.e. the beginning of the file, and after each null byte but the last if it's the last byte). That means that the first ^ won't catch [etc] when it's at the start of any other line but the first. You can use (\n|^) for that. For the second ^, ^ will never match after the beginning of the match, so \n^ will always fail. Finally, there's the point that jesse_b pointed out, that [etc] is regex for one character of those 3.

Those are the issues with the pattern, now the remaining issue is with the substitution, where you're lacking the trailing newline. sed would normally add it itself (more accurately, restore it from the input), but since you're using -z, the "separator"/terminator is set to a null byte rather than newline.

Once you fix those 4 issues (and added -E to avoid some escapes), you have:

sed -Ezi '/(\n|^)\[etc\]\ntransient = true/!s/$/[etc]\ntransient = true\n/' file

Since you're using -z, ^ corresponds to the beginning of each optionally-null-terminated record (i.e. the beginning of the file, and after each null byte but the last if it's the last byte). That means that the first ^ won't catch [etc] when it's at the start of any other line but the first. You can use (\n|^) for that. For the second ^, ^ will never match after the beginning of the match, so \n^ will always fail. Finally, there's the point that jesse_b pointed out, that [etc] is regex for one character of those 3.

Those are the issues with the pattern, now the remaining issue is with the substitution, where you're lacking the trailing newline. sed would normally add it itself (more accurately, restore it from the input, since the last terminator is optional), but since you're using -z, the "separator"/terminator is set to a null byte rather than newline.

Once you fix those 4 issues (and added -E to avoid some escapes), you have:

sed -Ezi '/(\n|^)\[etc\]\ntransient = true/!s/$/[etc]\ntransient = true\n/' file
added 168 characters in body
Source Link
JoL
  • 5k
  • 1
  • 20
  • 37

Since you're using -z, ^ corresponds to the beginning of each optionally-null-terminated record (i.e. the beginning of the file, and after anyeach null character that might be inbyte but the filelast if it's the last byte). That means that the first ^ won't catch [etc] when it's at the start of any other line but the first. You can use (\n|^) for that. For the second ^, ^ will never match after the beginning of the match, so \n^ will always fail. Finally, there's the point that jesse_b pointed out, that [etc] is regex for one character of those 3.

Those are the issues with the pattern, now the remaining issue is with the substitution, where you're lacking the trailing newline. sed would normally add it itself (more accurately, restore it from the input), but since you're using -z, the "separator"/terminator is set to a null byte rather than newline.

Once you fix those 4 issues (and added -E to avoid some escapes), you have:

sed -Ezi '/(\n|^)\[etc\]\ntransient = true/!s/$/[etc]\ntransient = true\n/' file

Since you're using -z, ^ corresponds to the beginning of the file and after any null character that might be in the file. That means that the first ^ won't catch [etc] when it's at the start of any other line but the first. You can use (\n|^) for that. For the second ^, ^ will never match after the beginning of the match, so \n^ will always fail. Finally, there's the point that jesse_b pointed out, that [etc] is regex for one character of those 3.

Those are the issues with the pattern, now the remaining issue is with the substitution, where you're lacking the trailing newline. sed would normally add it itself (more accurately, restore it from the input), but since you're using -z, the "separator"/terminator is set to a null byte rather than newline.

Once you fix those 4 issues (and added -E to avoid some escapes), you have:

sed -Ezi '/(\n|^)\[etc\]\ntransient = true/!s/$/[etc]\ntransient = true\n/' file

Since you're using -z, ^ corresponds to the beginning of each optionally-null-terminated record (i.e. the beginning of the file, and after each null byte but the last if it's the last byte). That means that the first ^ won't catch [etc] when it's at the start of any other line but the first. You can use (\n|^) for that. For the second ^, ^ will never match after the beginning of the match, so \n^ will always fail. Finally, there's the point that jesse_b pointed out, that [etc] is regex for one character of those 3.

Those are the issues with the pattern, now the remaining issue is with the substitution, where you're lacking the trailing newline. sed would normally add it itself (more accurately, restore it from the input), but since you're using -z, the "separator"/terminator is set to a null byte rather than newline.

Once you fix those 4 issues (and added -E to avoid some escapes), you have:

sed -Ezi '/(\n|^)\[etc\]\ntransient = true/!s/$/[etc]\ntransient = true\n/' file
added 168 characters in body
Source Link
JoL
  • 5k
  • 1
  • 20
  • 37

Since you're using -z, ^ corresponds to the beginning of the file (andand after any null character that might be in the file). That means that the first ^ won't catch [etc] when it's at the start of any other line but the first. You can use (\n|^) for that. For the second ^, ^ will never match after the beginning of the match, so \n^ will always fail. Finally, there's the point that jesse_b pointed out, that [etc] is regex for one character of those 3.

Those are the issues with the pattern, now the remaining issue is with the substitution, where you're lacking the trailing newline. sed would normally add it itself (more accurately, restore it from the input), but since you're using -z, the delimiter"separator"/terminator is set to a null byte rather than newline.

Once you fix those 4 issues (and added -E to avoid some escapes), you have:

sed -Ezi '/(\n|^)\[etc\]\ntransient = true/!s/$/[etc]\ntransient = true\n/' file

Since you're using -z, ^ corresponds to the beginning of the file (and after any null character that might be in the file). ^ will never match after the beginning of the match, so \n^ will always fail. Finally, there's the point that jesse_b pointed out, that [etc] is regex for one character of those 3.

Those are the issues with the pattern, now the remaining issue is with the substitution, where you're lacking the trailing newline. sed would normally add it itself (more accurately, restore it from the input), but since you're using -z, the delimiter is set to a null byte rather than newline.

Once you fix those 4 issues (and added -E to avoid some escapes), you have:

sed -Ezi '/(\n|^)\[etc\]\ntransient = true/!s/$/[etc]\ntransient = true\n/' file

Since you're using -z, ^ corresponds to the beginning of the file and after any null character that might be in the file. That means that the first ^ won't catch [etc] when it's at the start of any other line but the first. You can use (\n|^) for that. For the second ^, ^ will never match after the beginning of the match, so \n^ will always fail. Finally, there's the point that jesse_b pointed out, that [etc] is regex for one character of those 3.

Those are the issues with the pattern, now the remaining issue is with the substitution, where you're lacking the trailing newline. sed would normally add it itself (more accurately, restore it from the input), but since you're using -z, the "separator"/terminator is set to a null byte rather than newline.

Once you fix those 4 issues (and added -E to avoid some escapes), you have:

sed -Ezi '/(\n|^)\[etc\]\ntransient = true/!s/$/[etc]\ntransient = true\n/' file
Source Link
JoL
  • 5k
  • 1
  • 20
  • 37
Loading