Search code examples
bashcygwin

Is this (simple) for loop doing what I want it to?


I have pretty much no experience with cygwin & UNIX but need to use it for extracting a large set of data from a even larger set of files... I had some help yesterday to do this short script, but (after running for ~7-8 hours) the script simply wrote to the same output file 22 times. Atleast that's what I think happened.

I've now changed the code to this (see below) but it would be really awesome if someone who knows how this is done properly could tell me if it's likely to work before I waste another 8 hours...

for chr in {1..22}
do
zcat /cygdrive/g/data/really_long_filename$chr | sed '/^#/d' | cut -f1-3 >> db_to_rs_$chr
done

I want it to read file 1..22, remove rows starting with #, and send columns 1 to 3 to a file ending with the same number 1..22

yesterday the last part was just ...-f1-3 >> db_to_rs which I suspect just rewrote that file 22 times?

Help is much appreciated ~L


Solution

  • Yes, the code would work as expected.

    When the command ended in ...-f1-3 >> db_to_rs, it essentially appended all the output to the file db_to_rs.

    Saying ... >> db_to_rs_$chr would create filenames ending in {1 .. 22}.

    However, note that saying >> would append the output to a file. So if db_to_rs1 already exists, the output would be appended. If you want to create a new file instead, say > instead of >>.