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.


1 Answer 1


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)?


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.