Bash – Using a “for statement” to simplify bash statement with multiple repeats

bashshell-script

I'm subsetting a filename via grep and then concatenating the resulting files with cat. However, I'm still a bit confused as to how I should use the for statement, e.g. for ((i=1;i<23;i+=1));

Given my file file1.txt, I would like to grep sample1 as follows:

grep -w '^sample1' file1.txt > sample1_file.txt
grep -w '^sample2' file2.txt > sample2_file.txt
grep -w '^sample3' file3.txt > sample3_file.txt
....
grep -w '^sample22' file22.txt > sample22_file.txt

And then concatenate these:

cat  sample1_file.txt  sample2_file.txt  sample3_file.txt ...  sample22_file.txt > final_output.txt

Best Answer

Try:

for i in {1..22}
do
    grep -w "^sample$i" "file$i.txt"
done >final_output.txt

Notes:

  1. {1..22} runs through all the integers from 1 to 22. For people not familiar with C, it is probably more intuitive (but less flexible) than ((i=1;i<23;i+=1))

  2. It is important that the expression ^sample$i be inside double-quotes rather than single-quotes so that the shell will expand $i.

  3. If all you want is final_output.txt, there is no need to create the intermediate files.

  4. Notice that it is efficient to place the redirection to final_output.txt after the done statement: in this way, the shell needs to open and close this file only once.

Related Question