Bash:使用带有可变行号的 sed,以及行替换中的变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26319607/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bash: using sed with a variable line number, and variables in the line replacement
提问by kusold
So I have seen this question: Replace complete line getting number from variablewhich 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?
所以我看到了这个问题:Replace complete line getting number from variable这非常相似,但在我的情况下,我试图使用多个变量:一个用于行号 [lineNo],一个用于替换文本 [transFormatted] 和它应该在 [OUTPUT_FILE] 中查找的文件我尝试了几十种组合来试图让它识别所有这些变量,但似乎没有任何效果。无论我尝试哪种方式都不快乐。我究竟做错了什么?
sed -e '${lineNo}s/.*/${transFormatted}/' < $OUTPUT_FILE
采纳答案by Ignacio Vazquez-Abrams
Single quotes inhibit parameter expansion.
单引号禁止参数扩展。
sed -e "${lineNo}s/.*/$transFormatted/" < "$OUTPUT_FILE"
回答by holygeek
You have to use double quotes for the variables to be expanded from the shell environment.
您必须对要从 shell 环境展开的变量使用双引号。