Adding a header into multiple .txt files

前端 未结 3 1958
名媛妹妹
名媛妹妹 2021-01-26 05:44

I need a batch file that will add the text \"Write In\" in a new line at the beginning of the content of hundreds of .txt files without removing any existing text. I found somet

3条回答
  •  余生分开走
    2021-01-26 06:19

    You can simply use the Linux Sed command to insert a header into a file.

    sed -i '1s/^/This is my header\n/' filename
    e.g. sed -i '1s/^/Write In\n/' myfile.txt
    

    This can be applied to multiple files under a directory:

    For .txt files

    for file in *.txt
    do
      sed -i '1s/^/This is my header\n/' $file
    done
    

    For CSV files

    for file in *.csv
    do
      sed -i '1s/^/This is my header\n/' $file  
    done
    

提交回复
热议问题