0

Let's say we want to replace the letter "a" with its Morse code equivalent from a text.txt file to a hidden .out.txt file using sed, with the contents of the original file being "this is a test".

#!/bin/bash
touch .out.txt
cat text.txt > .out.txt
sed 's/a/.-/g' .out.txt

The output, as expected, is "this is .- test".
However, let's say we want to do multiple instances of sed, without constantly printing the result of each instance of sed using the -n parameter. In this case:

#!/bin/bash
touch .out.txt
cat text.txt > .out.txt
sed -n 's/a/.-/g' .out.txt
sed -n 's/t/-/g' .out.txt
cat .out.txt

However, in this case, the output of cat is the same as the contents of text.txt, "this is a test".
Is there a possible substitute for -n, or in general a way to prevent sed from printing anything with our wanted result?

Yunnosch
  • 26,130
  • 9
  • 42
  • 54
Tasos500
  • 107
  • 2
  • 11
  • 3
    `sed -n 's/t/-/g' .out.txt` is not really changing `out.txt` at all. – anubhava Jan 17 '18 at 18:43
  • 2
    To emphasize what @anubhava said: `sed` does not, by default, *modify* the files referenced on the command line. It simply sends the modified output to *stdout*. *Nothing* you are doing ever makes changes to your `.out.txt` file. – larsks Jan 17 '18 at 18:45
  • In addition, the `-n` suppresses the automatic output. You then have to tell sed what to output (using a `p` at the end of the expression). `sed -in 's/a/.-/pg' .out.txt`... Note: this will not print non-matching lines. – HardcoreHenry Jan 17 '18 at 19:02
  • Is there any reason why wouldn't go with `sed -e 's/a/.-/g' -e 's/t/-/g' test.txt > .out.txt` ? – ULick Jan 17 '18 at 21:32

1 Answers1

1

Check your seds sed --help for the syntax of -ioption.
This is necessary, because the syntax is different depending on which sed you are using, i.e. I cannot simply tell you.
Then use it accordingly to have the effects of sed take place in-place on the file.

Yunnosch
  • 26,130
  • 9
  • 42
  • 54