使用"sed"从文件中获取子字符串

Art*_*lma 4 bash substring sed

谁能帮助我使用sed程序获取子串?

我有一个这行文件:

....
define("BASE", "empty"); # there can be any string (not only "empty").
....
Run Code Online (Sandbox Code Playgroud)

我需要将"空"作为字符串变量添加到我的bash脚本中.

这时我有:

sed -n '/define(\"BASE\"/p' path/to/file.ext
# returns this line:
# define("BASE", "empty");
# but I need 'empty'
Run Code Online (Sandbox Code Playgroud)

UPD:感谢@Jaypal

现在我有bash脚本:

DBNAME=`sed -n '/define(\"BASE\"/p' path/to/file.ext`
echo $DBNAME | sed -r 's/.*"([a-zA-Z]+)".*/\1/'
Run Code Online (Sandbox Code Playgroud)

它工作正常,但如果有任何方法使用一行代码进行相同的操作?

ezd*_*ena 6

你应该使用is

sed -n 's/.*\".*\", \"\(.*\)\".*/\1/p' yourFile.txt
Run Code Online (Sandbox Code Playgroud)

这意味着something(.*)后跟引号(\".*\")中的内容,然后是逗号和空格(,),然后是引号(\"\(.*\)\")中的内容.

括号定义了您稍后可以重用的部分,即第二个引号中的字符串.用它\1.

我放在-n前面,以回答更新的问题,上线被操纵的线.