Search code examples
shellcut

Applying same command on different input files to different output files


I'm doing

$ cut -f 1,5,6 myfile1.csv > myoutput1.csv
$ cut -f 1,5,6 myfile2.csv > myoutput2.csv
$ cut -f 1,5,6 myfile3.csv > myoutput3.csv

...

Suppose I have 400 files, I want to write just one command for the 400 files, it seems that cut doesn't support specifying output filenames? I'm sure I'm missing a command here, but I couldn't guess how to use xargs here, any ideas?


Solution

  • for i in `seq 1 3`; do cut -d, -f1,5,6 "myfile${i}.csv" > "myoutput${i}.csv"; done
    

    To handle arbitrary filenames, try this:

    for filename in `find . -maxdepth 1 -name '*.csv' | cut -d/ -f2 | cut -d. -f1`
    do
        cut -d, -f1,5,6 "${filename}.csv" > "${filename}.out.csv"
    done
    

    Yet another way to skin this cat:

    for filename in `find -name '*.csv' -exec basename "{}" \;`
    do
        cut -d, -f1,5,6 $filename > ${filename/.csv}.out.csv
    done