I am learning AWK by trying out examples whenever I need a specific conversion. I would like to edit the 'before.txt' so that all the missing data points between 140-150 are added and shown as 0.
You do something in the BEGIN section, before the before.txt is processed.
And then you do nothing in the main section (where it's too late anyway).
The solution is to process the file in the main section and store it into variables (An array that is addressed by the $1 values), and then process/print in the END section. Like RudiC did.
The following is multi-line for readability (at least for me), and does not have the NR==FNR {...; next} overhead (that takes the first given file, where the following code will take the next file(s) - but here is only one file).
awk '
# main section; process all lines of all given files
{ T[$1]=$2 }
END {
for (i=140; i<=150; ++i) {
if (i in T) print i,T; else print i,0
}
}' before.txt
It prints with an explicit if - else, like you did; compare with RudiC's implicit if!
Welcomes to forums, special thanks for using the code tags. Could you please try following and let me know if this helps.
1st: If you need to have maximum number till 150 only from 140 then following may help you.
Let's say following is the Input_file:
2nd: If you need to run the commands till the last maximum number starting from 140 then following may help you in same. But in case maximum number is lesser than value of 150 then it will take care of printing the values till 150 .
Let's say following is the Input_file: