从字符串中提取正则表达式结果并将其写入一个变量 [英] extract regexp result from string and write it to a variable
本文介绍了从字符串中提取正则表达式结果并将其写入一个变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图编写一个shell脚本,用于在正则表达式中指定为参数$ 1的文件中进行搜索,并将找到的子模式写入一个我可以使用的变量。 p>让我们说我的脚本叫做'dosth.sh',并且我有一个'plot.gp'文件用于行
set output'test.tex'
如果我现在执行'dosth.sh plot.gp'脚本应该从plot.gp中提取'test.tex'并将其写入变量。
这对grep或其他人来说可能如何?
感谢和问候,Jan Oliver
解决方案
value =`猫$ 1 | grep -e'set output'| cut -d'''-f 2`
使用而不是和上面的行做工作。
I'm trying to write a shell script that searches in a file specified as argument $1 for a regex and writes the found subpattern into a variable I can then use.
let's say my script is called 'dosth.sh' and I have a file 'plot.gp' with the line
set output 'test.tex'
If I now execute 'dosth.sh plot.gp' the script should extract 'test.tex' from plot.gp and write it to a variable.
How is that possible with grep or others?
Thanks and regards, Jan Oliver
解决方案
value=`cat $1 | grep -e 'set output' | cut -d'"' -f 2`
using " instead of ' and the line above does the job.
这篇关于从字符串中提取正则表达式结果并将其写入一个变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文