i need to fetch n+9th line from the occurance of a specific word which is store in variable i from a file LIST.txt

Below is the command

Used_pages=`awk '/${i}/{nr[NR+9]}; NR in nr'  LIST.txt`

it works when i do a test run on the file by replacing the variable $i with one of the values like

 awk '/APPLE/{nr[NR+9]}; NR in nr'  LIST.txt

Please help me figgure out how to use the same command with the variable.



awk has the -v parameter for setting variables. To set the variable $i in awk, you would say awk -v i=$i and then within the awk statement, simply reference the variable as i rather than $i.

I haven't tested the following, but try this:

used_pages="$(awk -v i=$i '/i/{nr[NR+9]}; NR in nr' LIST.txt)"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.