gringer
• 10.9k
• 3
• 17
• 66

Another awk approach. This should work on a normal VCF file (it will print the headers) as well as your example and only changes the ALT field if its first character is the same as the 2nd field:

$$awk -F"\t" -vOFS="\t" '{ if(substr(5,1,1) == 4){5=substr($$5,2); }}1' ff.vcf
#CHROM  POS ID  REF ALT
chr4    5708160 .   G   ATAAC
chr4    30280292    .   C   ACGTAT
chr6    166253422   .   C   CTTTCT
chr11   21931340    .   A   ACACACGC

To run this on all of your files, assuming they are the only files in the current directory with a .txt extension, you can do:

for file in *txt; do
awk -F"\t" -vOFS="\t" '{ if(substr($$5,1,1) ==$$4){$$5=substr($$5,2); }}1' "$$file" > "fixed.$$file"
done

This will loop over all *.txt files in the current directory. For each foo.txt, it will create fixed.foo.txt with the modified data.

terdon
• 7.8k
• 3
• 14
• 40

Another awk approach. This should work on a normal VCF file (it will print the headers) as well as your example and only changes the ALT field if its first character is the same as the 2nd field:

$$awk -F"\t" -vOFS="\t" '{ if(substr(3,1,1) == 2){3=substr($$3$$awk -F"\t" -vOFS="\t" '{ if(substr(5,1,1) == 4){5=substr($$5,2); }}1' fileff.vcf
CHROM#CHROM  POS ID  REF ALT
chr1chr4    C5708160 .   TG   ATAAC
chr1chr4    30280292    .   C   TACGTAT
chr1chr6    166253422   .   C   ACTTTCT
chr1chr11   21931340 C   T.   A   ACACACGC

To run this on all of your files, assuming they are the only files in the current directory with a .txt extension, you can do:

for file in *txt; do
awk -F"\t" -vOFS="\t" '{ if(substr($$3,1,1) ==$$2$$5,1,1) ==$$4){$$3=substr($$3$$5=substr($$5,2); }}1' "$$file" > "fixed.$$file"
done
terdon
• 7.8k
• 3
• 14
• 40

Another awk approach. This should work on a normal VCF file (it will print the headers) as well as your example and only changes the ALT field if its first character is the same as the 2nd field:

$$awk -F"\t" -vOFS="\t" '{ if(substr(3,1,1) == 2){3=substr($$3,2); }}1' file.vcf
CHROM   REF ALT
chr1    C   T
chr1    C   T
chr1    C   A
chr1    C   T

To run this on all of your files, assuming they are the only files in the current directory with a .txt extension, you can do:

for file in *txt; do
awk -F"\t" -vOFS="\t" '{ if(substr($$3,1,1) ==$$2){$$3=substr($$3,2); }}1' "$$file" > "fixed.$$file"
done
terdon
• 7.8k
• 3
• 14
• 40