How to cut multiple columns from several files and print the output to different files

user1458512 picture user1458512 · Jun 15, 2012 · Viewed 45k times · Source

I have several files and I only want to take specific columns from it. At the moment, I am using the following code:

$cut -f 1,2,5 AD0062-C.vcf > cutAD0062.txt

However, to speed up the process I was wondering if I could cut the same columns (fields 1,2,5) in multiple files and then print the output to several different files. I.e columns 1,2,5 of files AD0063-C.vcf, AD0064-C.vcf, AD0065-C.vcf should output results to separate files: cutAD0063.txt, cutAD0064.txt, cutAD0065.txt?

Answer

kev picture kev · Jun 15, 2012

You can write a for...loop:

for i in AD*-C.vcf
do
    cut -f 1,2,5 $i > cut${i%-C.vcf}.txt
done