Search code examples
linuxbashvcf-variant-call-format

How to extract filename without extension and change header in the same file with the extracted name?


I have about 30 VCF files with filenames as ID_001.new.vcf. I want to extract only the "ID_001" part from the filename and change it in the header line of the VCF file where "Sample1" is given:

#CHROM POS ID REF ALT QUAL FILTER INFO FORMAT Sample1

So that the result would look like:

#CHROM  POS     ID      REF     ALT     QUAL    FILTER  INFO    FORMAT  ID_001

How can I do it?

I have tried to use echo in bash and extract the IDs from the Filename but I am unable to iterate it to change inside the file.

Thanks for your help.


Solution

  • You could use the below :

    for file in *.vcf 
    do 
       name=$(basename -- "$file" .new.vcf) 
       sed -i "s/Sample1/$name/g" "$file" 
    done