0

I am using awk to select lines which contain 1 to 18 respectively in column 1 in a file. I use command line as follow, and I got nothing in my output files. So anyone have a idea how could I modify my command line? Thank you in advance!

for k in `seq 1 18`; do zcat qc.vcf.gz | grep -v "^#" | awk '{if($1==$k) print$0}' > test${k}; done
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
Luna
  • 11
  • 3
  • 1
    In `if($1==$k)`, were you expecting AWK to interpret `$1`, and Bash to interpret `$k`? Seems unlikely to me. – Ruud Helderman Aug 22 '19 at 10:26
  • 1
    You try to use the shell variable `k` in your awk script. This does not work the way you implemented it. See duplicate. – kvantour Aug 22 '19 at 10:26
  • Sorry, maybe I did not explain my question properly. so I explained again. I have a text file containing ~30k lines and several columns. what I want is split this file by rows. The rule is that all lines with different numbers in the first column goes to different new files. For example, all lines in which the first column is 1 will be written to a new file called test1. To reach is goal, I used the command line above. However, I got 18 empty files. So anyone can help me with that? Many thanks! – Luna Aug 23 '19 at 01:36

1 Answers1

1

You don't need a loop, just zcat and awk:

zcat qc.vcf.gz | awk '!/^#/{f="test"$1; print > f; close(f)}'

awk command explained:

!/^#/ {
    f = "test"n     # The (f)ilename of the output file
    print >> f       # Print current line to output file
    close(f)        # Close output file
}
hek2mgl
  • 152,036
  • 28
  • 249
  • 266
  • Hi~ thank you for answering my questions. I tried your command, but I just got one line in each output file, and it is not what I want. Maybe I did not explain my question precisely, so please see my extra explain above. – Luna Aug 23 '19 at 01:39
  • 1
    @Luna I've changed the command. Please check if that works for you – hek2mgl Aug 23 '19 at 16:07