hieu hieu - 2 years ago 109
Linux Question

add new column of a number

I am writing the following codes to extract data from an existing file using awk within a for loop.

for c in {1..300}
awk '{if($28==1) print $12,$26,$28}' file1.txt > file2.txt

This is ok and I have file2.txt
Now, I want to add a new column containing c generated from the for loop above.
I do this but it does not work.

awk '{if($28==1) print $12,$26,$28, paste c}' file1.txt > file2.txt

This only works when I replace c by a real number such as 1,2,...
Finally, I want to append file1.txt to file2.txt, i.e. every time the loop runs it will add new data to file2.txt.
I have this but it seems not the best:

awk <file2.txt>> file_final.txt

Can you please give me some advice? Thank you!

Using the approach recommended by William below, I am able to do produce outputs that I want. Thank you very much!

Answer Source
rm file2.txt
for c in $(seq 1 300); do
  awk '$28==1{print $12,$26,$28,c}' c=$c file1.txt >> file2.txt
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download