I have the following shell script code. Here if I’m using NR==$i in gawk statement, it doesn’t work, while if I simply use NR==1 (or some other integer value) it works perfectly fine.
for ((i=1;i<=5;i++))
do
gawk 'NR==$i{//some action}' input.txt
done
Can anyone please let me know what is the issue here?
The problem is that the single quotes around the
awkscript mean thatbashdoes no substitution inside the string andgawksees literally$i(which is the same as$0sinceiis not initialized in thegawkscript).This time, use double quotes instead:
In general, it would probably be better to pass the variable explicitly: