从字符串中提取正则表达式结果并将其写入变量 [英] extract regexp result from string and write it to a variable

查看:23
本文介绍了从字符串中提取正则表达式结果并将其写入变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个 shell 脚本,该脚本在指定为参数 $1 的文件中搜索正则表达式,并将找到的子模式写入我可以使用的变量中.

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.

假设我的脚本名为dosth.sh",并且我有一个带有该行的文件plot.gp"

let's say my script is called 'dosth.sh' and I have a file 'plot.gp' with the line

set output 'test.tex'

如果我现在执行 'dosth.sh plot.gp' 脚本应该从 plot.gp 中提取 '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.

grep 或其他人怎么可能做到这一点?

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆