Art*_*lma 4 bash substring sed
谁能帮助我使用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/'
它工作正常,但如果有任何方法使用一行代码进行相同的操作?
你应该使用is
sed -n 's/.*\".*\", \"\(.*\)\".*/\1/p' yourFile.txt
这意味着something(.*)后跟引号(\".*\")中的内容,然后是逗号和空格(,),然后是引号(\"\(.*\)\")中的内容.
括号定义了您稍后可以重用的部分,即第二个引号中的字符串.用它\1.
我放在-n前面,以回答更新的问题,上线被操纵的线.