BASH外壳使用正则表达式得到的文件值转换成一个参数 [英] BASH shell use regex to get value from file into a parameter

查看:218
本文介绍了BASH外壳使用正则表达式得到的文件值转换成一个参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经得到了我需要从使用正则表达式得到一块文本文件。我们将调用文件 x.txt 。我想这样做的是开放 x.txt ,提取该文件的正则表达式匹配,并设置成一个参数。任何人都可以给我一些指点一下吗?

I've got a file that I need to get a piece of text from using regex. We'll call the file x.txt. What I would like to do is open x.txt, extract the regex match from the file and set that into a parameter. Can anyone give me some pointers on this?

因此​​,在 x.txt 我有以下行

So in x.txt I have the following line

$variable = '1.2.3';

我需要提取 1.2.3 从文件到我的bash脚本,然后使用一个zip文件

I need to extract the 1.2.3 from the file into my bash script to then use for a zip file

推荐答案

您可以使用grep,砍斫技术

You can use the grep-chop-chop technique

var="$(grep -F -m 1 '$variable =' file)"; var="${var#*\'}"; var="${var%\'*}"

这篇关于BASH外壳使用正则表达式得到的文件值转换成一个参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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