0

I have 136 .vcf files in a folder; I want to extract some information from each of them and write the output in a .txt file like below

[fi1d18@cyan01 snp]$ bcftools query -f '%CHROM\t%POS\t%REF\t%ALT[\t%ID]\n' file.vcf > file.txt 

I am doing that one by one manually but takes an ages; Can somebody please helps me in any script to do that for all files in Linux?

Thank you

1 Answer 1

1

Go to the folder with your files, and loop over these files. Use "$f" for input and "${f%.*}.txt" as output file name

${f%.*} will strip the extension from the filename (via).

for f in *.vcf; do bcftools query -f '%CHROM\t%POS\t%REF\t%ALT[\t%ID]\n' "$f" > "${f%.*}.txt" done 
4
  • Sorry how I say put the .txt files in another PATH? Commented Nov 6, 2019 at 14:30
  • 1
    > "some/relative/path/${f%.*}.txt" or > "/some/absolute/path/${f%.*}.txt" Commented Nov 6, 2019 at 14:32
  • Sorry for disturbing you; The output .txt files have 4 columns, I want to add a column as the first column to each .txt file which corresponds to the name of that file; For instance if a .txt file is A.txt so in the first column of this file should be filled by A and finally concatenate all these files as a unified .txt file. Is it possible in terminal? Potentially manually it is possible by opening each file, adding extra column, filling the first column with the name of that file and finally attaching files together but this takes an ages Commented Nov 6, 2019 at 14:42
  • Sorry today when I was trying your command on new files; I saw that does not work like this [fi1d18@cyan01 snp]$ for f in *.vcf; bcftools query -f '%CHROM\t%POS\t%REF\t%ALT[\t%ID]\n' "$f" > "${f%.*}.txt" done bash: syntax error near unexpected token `bcftools' [fi1d18@cyan01 snp]$ Commented Nov 7, 2019 at 12:11

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.