Search code examples
for-loopcommand-linedirectorycsvkit

Change the output directory on command line For Loop


I have been working CSVKIT to build out a solution for parsing fixed-width data files to csv. I have put together the below code to iterate through all the files in a directory, but this same code also places the files back into the same directory it came from. As best practice I believe in using an 'IN' folder and an 'OUT' folder. I am also processing this command-line on a MAC.

for x in $(ls desktop/fixedwidth/*.txt); do x1=${x%%.*}; in2csv -f fixed -s desktop/ff/ala_schema.csv $x > $x1.csv; echo "$x1.csv done."; done

I feel that I am missing something and or that I need to change something within my snippet shown below, but I just can't put my finger on it.

x1=${x%%.*}

Any help on this would be wonderful and I thank you in advance.


Solution

  • When you run,

    in2csv -f fixed -s desktop/ff/ala_schema.csv $x > $x1.csv
    

    the output of your programm will be writing to the file, ${x1}.csv.

    So, just set correct path to x1:

    output_dir=/path/to/your/dir/
    output_file=${output_dir}${x%%.*}.csv
    in2csv -f fixed -s desktop/ff/ala_schema.csv $x > $output_file;
    

    But, you should create your output_dir, before running this code. Otherwise you can receive an error, that directory doesn't exists.