[关闭]
@ysongzybl 2015-05-08T19:20:03.000000Z 字数 927 阅读 1620

Use Bash variable with Sed command

bash


Problem Description

I'm expecting to replace all instances of ${version.number} with the contents of the variable "version". Instead the literal text $version is being inserted.

What do I need to do to make sed use the current value of $version instead?

  1. #!/bin/bash
  2. version=$1
  3. echo $version
  4. sed 's/\${version.number}/$version/' template.txt > readme.txt

Solution

  1. sed "s/\${version.number}/$version/" template.txt > readme.txt

Only double quotes do dollar-sign replacement. That also means single quotes don't require the dollar sign to be escaped.

Get 10-th line from file

sed "10q;d" file.txt

argument q: quit searching
argument d: delete pattern space, start next cycle

To store the output to a variable, use

  1. line=$(sed "10q;d" file.txt)
  2. echo $line

Source

http://stackoverflow.com/questions/3204302/how-to-use-a-bash-script-variable-with-sed

http://stackoverflow.com/questions/6022384/bash-tool-to-get-nth-line-from-a-file

http://stackoverflow.com/questions/6749128/store-output-from-sed-into-a-variable

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注