Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash: using sed with a variable line number, and variables in the line replacement

Tags:

bash

sed

So I have seen this question: Replace complete line getting number from variable which is pretty similar, but in my case I am trying to use multiple variables: one for the line number [lineNo], one for the text to replace [transFormatted] and the file to which it should be looking in [OUTPUT_FILE] I've tried dozens of combinations to try to get it to recognize all these variables but nothing seems to work. It's unhappy no matter which way I try. What am I doing wrong?

sed -e '${lineNo}s/.*/${transFormatted}/' < $OUTPUT_FILE
like image 566
kusold Avatar asked Oct 11 '14 21:10

kusold


2 Answers

Single quotes inhibit parameter expansion.

sed -e "${lineNo}s/.*/$transFormatted/" < "$OUTPUT_FILE"
like image 123
Ignacio Vazquez-Abrams Avatar answered Nov 11 '22 16:11

Ignacio Vazquez-Abrams


You have to use double quotes for the variables to be expanded from the shell environment.

like image 23
holygeek Avatar answered Nov 11 '22 17:11

holygeek