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.


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

sed -i.bak "1 s/^.*$/$new_header/" inputfile
| improve this answer | |
  • 2
    You don't need to put anchors because sed reads the input file line by line. – Avinash Raj Oct 18 '14 at 14:00

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.