2 回答
TA贡献1895条经验 获得超3个赞
一线解决方案sed:
MYVAR=$(sed -nE 's/^BAZ\s*=\s*(\S*)$/\1/p' inputfile)
如一条注释中所述,sed在以下情况下,此命令将不会产生预期的结果:
BAZ = 10 // some comment ending with 0
此处MYVAR将分配0而不是10。要解决此问题,可以将正则表达式更改为:
MYVAR=$(sed -nE 's/^BAZ\s*=\s*([^\s\/]*).*/\1/p' inputfile)
现在MYVAR是10根据需要。分解正则表达式:
^BAZ\s*=\s*([^\s\/]*).*/\1
^ Match beginning of line
BAZ Match BAZ
\s*=\s* Match equal sign surrounded by zero or more spaces
([^\s\/]*) Capture in Group 1 any character that is not space or slash
.* Match the rest of the text
/\1 Replace matched text with text in Group 1
- 2 回答
- 0 关注
- 223 浏览
添加回答
举报