I have 2 variables, i and j. For each combination of the variables, there is a text file, text.txt, with text arranged in space-separated columns, e.g.:
string1 43245 7.45
string2 23452 9.34
string3 23652 2.57
I would like to do the following: Make one large text file containing all text files with the variables added in additional columns, and some other additional columns containing text, not from variables. E.g.
i = x01
j = y01
Then the output should be:
x01 y01 extrastring string1 43245 7.45
x01 y01 extrastring string2 23452 9.34
x01 y01 extrastring string3 23652 2.57
I have tried something like:
awk '{$0="'"$i"' '"$j"' extrastring"$0}'1 $i/$j/text.txt >> newtext.txt
That doesn't work.
Edit: I probably need to add that this should run in a double for loop:
for i in x01 x02 x03
do
for j in y01 y02 y03
do
awk '{$0="'"$i"' '"$j"' extrastring"$0}'1 $i/$j/text.txt >> newtext.txt
done
done
Edit: I tried this based on SoFan's answer
for i in x01 x02 x03
do
for j in y01 y02 y03
do
cd $i/$j/
awk -v i="$i" -v j="$j" '{print i,j,"extrastring",$0}' text.txt >> newtext.txt
done
done
Unfortunately it doesn't work. The script won't run.
test.txtconstant and you want to print it with different columns for every pair of x01.. y01..?