1

I have a file and I want to append a specific text, \0A, to the end of each of its lines.

I used this command,

sed -i s/$/\0A/ file.txt

but that didn't work with backslash \0A.

Enlico
  • 23,259
  • 6
  • 48
  • 102
lystack
  • 41
  • 4

2 Answers2

2

In its default operations, sed cyclically appends a line from input, less it's terminating <newline>-character, into the pattern space of sed.

The OP wants to use sed to append the character \0A at the end of a line. This is the hexadecimal representation of the <newline>-character (cfr. http://www.asciitable.com/). So from this perspective, the OP attempts to double space a files. This can be easilly done using:

sed G file

The G command, appends a newline followed by the content of the hold space to the pattern space. Since the hold space is always empty, it just appends a newline character to the pattern space. The default action of sed is to print the line. So this just double-spaces a file.

kvantour
  • 25,269
  • 4
  • 47
  • 72
1

Your command should be fixed by simply enclosing s/$/\0A/ in single quotes (') and escaping the backslash (with another backslash):

sed -i 's/$/\\0A/' file.txt

Notice that the surrounding 's protect that string from being processed by the shell, but the bashslash still needed escape in order to protect it from SED itself.

Obviously, it's still possible to avoid the single quotes if you escape enough:

sed -i s/$/\\\\0A/ file.txt

In this case there are no single quotes to protect the string, so we need write \\ in the shell to get SED fed with \, but we need two of those \\, i.e. \\\\, so that SED is fed with \\, which is an escaped \.

Move obviously, I'd never ever suggest the second alternative.

Enlico
  • 23,259
  • 6
  • 48
  • 102