7

Hi i am trying to replace the header line from my file using the sed command as mentioned below,

sed "1s/.*/$new_header/" Test_file.csv 

The above line replaces the header and prints in stdout , But How can i redirect the output to new file or replace in the files directly ?

sed "1s/.*/$new_header/" Test_file.csv > new_file.csv 

The above command works fine , But i want to redirect to same file.

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
William R
  • 609
  • 3
  • 8
  • 16
  • Two related Q/A: [How can I achieve portability with sed -i (in-place editing)?](https://unix.stackexchange.com/q/92895) [How to change a file in-place using awk? (as with "sed -i")](https://unix.stackexchange.com/q/496179) – Kusalananda May 20 '21 at 17:04

1 Answers1

10

You can use -i flag to sed which will edit in-place and also take backup:

sed -i.bak "1 s/.*/$new_header/" inputfile

Note that the -i option is non-standard and may work differently in different implementations of sed. See How can I achieve portability with sed -i (in-place editing)?

Kusalananda
  • 320,670
  • 36
  • 633
  • 936
Rahul Patil
  • 24,281
  • 25
  • 80
  • 96