I have a folder with many .vcf files. I need to extract all the lines that begin with # and save them in a file and then append all the lines where the 12th column starts with 1/1 in the same file. It should create a new output file for each input file.

abc.vcf, def.vcf --> filter --> abcfiltered.vcf, deffiltered.vcf

I thought something like that but doesn't work:

printf '%s\0' *.vcf | xargs -0 -n 1 sh -c 'grep "#" "$1" > "candilist${1#case*}"' sh

this works for the first condition

I tried to add this command

awk -F '\t' '$12~/^1/' "$1");

using them separately works, but together doesn't. I guess is something with quotes or brackets.

  • 2
    Please edit your question to include a [mcve] with concise, testable sample input, expected output and your attempt to solve the problem yourself so we can help you further. See How to Ask.
    – Ed Morton
    Commented Apr 26, 2022 at 1:11

1 Answer 1

awk -F'\t' '
    /^#/          { print >"file1" }
    ($12 ~/^1\/1/){ print >"file2" }' ./*.vcf

this will save lines from all .vcf files which those starts with character # into file1 and lines that column#12 starts with 1/1 (slash in regex in awk is a special character which we escaped that with a backslash) into separate file2.

Note that in the above code file2 will contains both lines types (start with # and also column#12 starts with 1/1); if want exclude file2 from the lines start with #, add next statement in the first block /^#/{ print >"file1"; next }.

Also note that the priority is with the lines that starts with # since it's checking at first so if a line both conditions were meet only the first one will be considered, if you need that to be checked as second priority re-order the blocks.

Answer to thr revised question (process every .vcf file with generating outputs seperatly):

for file in ./*.vcf; do
    awk -F'\t' '
        /^#/          { print >(FILENAME"_1.procced") }
        ($12 ~/^1\/1/){ print >(FILENAME"_2.procced") }' "$file"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .