Search code examples
excelbashexport-to-excelexport-to-csv

Bash Script to export first columns in txt file to excel with Header


I would like to export first column of my txt file into excel along with the user defined header

Currently my txt file has following information

667869 667869
580083 580083
316133 316133
9020 9020

and i would like to export it to excel with my own header, how could i possibly do it in bash script?.


Solution

  • Using a for loop with sed maybe this will help:

    for file in /path/to/folder/*.txt ; do
     bname=$(basename $file)
     pref=${bname%%.txt}
     sed -i '1iCOL1,COL2' $file
     done
    

    This will add a header COL1,COL2 to each .txt file in the directory