1

I need to insert a command "new file" in a test.txt file at line number 4.

Tried sed; I can see the changed file output, but when I again do cat test.txt, the changes are gone.

sed "4i new file" /test.txt

How can I save the changes?

agc
  • 7,973
  • 2
  • 29
  • 50
cool_shibz
  • 23
  • 4

2 Answers2

5

Use in place edit option sed -i "4i new file" test.txt

Without the -i option sed will not make any changes to the file. It will only print the result.

-i[SUFFIX], --in-place[=SUFFIX]
    edit files in place (makes backup if SUFFIX supplied)
aebudak
  • 641
  • 6
  • 8
-1

sed '4i new file' test.txt > tmp && mv tmp test.txt

Krlos
  • 140
  • 8