I want to add a column to a file depending on a key value. I have infile_chr"N".txt (where N is a number from 1 to 22) and I need a single output file (outfile.txt) in which the first column is N.
Here there is an example of the output file:
1 856108 0.02625
1 870806 0.02625
1 884635 0.02625
...
22 51111340 0.02625
22 51135384 0.02625
But in the input files there is no column with the number N. Here the first two lines of the input file "infile_chr1.txt", where marked with ** you can find the columns I want to print:
**856108** 14774 908823 40 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, () 0.025 0.024375 **0.02625** 0.975 0.02875
**870806** 55545 921716 40 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, () 0.025 0.024375 **0.02625** 0.975 0.02875
I tried with this code:
for K in {1..22}; do awk '{$2="$K"; print $2,$1,$9}' infile_chr"$K".txt >> outfile.txt; done
but the output is wrong:
$K 856108 0.02625
$K 870806 0.02625
$K 884635 0.02625
$K 899937 0.02625
$K 908823 0.02625
Can anyone help me? Many thanks.
You don't need to do bash looping.
awkcan do this in a single command like this: