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.

William R
  • 609

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
  • 333,661
Rahul Patil
  • 24,711