【发布时间】:2012-02-09 11:42:50
【问题描述】:
谁能帮我使用sed 程序获取子字符串?
我有一个包含这一行的文件:
....
define("BASE", "empty"); # there can be any string (not only "empty").
....
我需要将“空”作为我的 bash 脚本的字符串变量。
此时我有:
sed -n '/define(\"BASE\"/p' path/to/file.ext
# returns this line:
# define("BASE", "empty");
# but I need 'empty'
UPD:感谢@Jaypal
现在我有 bash 脚本:
DBNAME=`sed -n '/define(\"BASE\"/p' path/to/file.ext`
echo $DBNAME | sed -r 's/.*"([a-zA-Z]+)".*/\1/'
它工作正常,但是否有任何方法可以用一行代码进行相同的操作?
【问题讨论】: