AWK Replace pattern at specific line trough passing variables

awk, line, variables

Solution

Remove the quotes around `var2` and you should be good.

palabra=betina; i=5 ; awk -v var1="$i" -v var2="$palabra" 'NR==var1 {sub(var2," ")}{print }' file

Problem

I am trying to pass two variables to awk in order to replace a word (-v var1) with a space (" ") at specific line (-v var2). When I ran it, nothing is replaced at the line I wanted. My first approach to do it is: ``` palabra=betina; i=5 ; awk -v var1="$i" -v var2="$palabra" 'NR==var1 {sub("var2"," ")}{print }' Countries > tmp ``` on the file "Countries", with content is: ``` usa usa colombia spain betina ```

Original source