Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWK Replace pattern at specific line trough passing variables

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
like image 261
Another.Chemist Avatar asked Dec 20 '25 05:12

Another.Chemist


1 Answers

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
like image 58
jaypal singh Avatar answered Dec 21 '25 23:12

jaypal singh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!