将模式与sed匹配并同时获取整数 [英] Matching a pattern with sed and getting an integer out at the same time

查看:248
本文介绍了将模式与sed匹配并同时获取整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有这些行的xml文件(等等):

I have an xml file with these lines (among others):

#Env=DEV2,DEV3,DEV5,DEV6
#Enter your required DEV environment after the ENV= in the next line: 
Env=DEV6

我需要:


  1. 验证 ENV = 是模式 DEV {1..99}

  2. 从行<提取数字(在本例中为6) code> ENV = DEV6 到一些环境变量

  1. Verify that the text after ENV= is of the pattern DEV{1..99}
  2. extract the number (in this case, 6) from the line ENV=DEV6 to some environment variable

我知道一点awk和grep ,并且可以使用那些来获取数字,但我正在考虑Sed,我被告知匹配模式比awk更好并且花费更少的时间。另外,我担心长长的greps行匹配特定Env =的行的开头。

I know a bit of awk and grep, and can use those to get the number, but I'm thinking of Sed, which I'm told matches patterns nicer than awk and takes less time. Also, I'm concerned about long long lines of greps matching the beginning of the line for that particular Env= .

我将如何使用Sed执行此操作?我会用更短的线路逃脱吗?

How would I go about doing it with Sed? would I get away with a shorter line?

我是一个新手,读了一堆教程和例子,我的手指扭曲试图做两件事同时...

I'm a sed newbie, read a bunch of tutorials and examples and got my fingers twisted trying to do both things at the same time...

推荐答案

我建议使用GNU sed:

I suggest with GNU sed:

var=$(sed -nE 's/^Env=DEV([0-9]{1,2})$/\1/p' file)
echo "$var"

输出:


6

这篇关于将模式与sed匹配并同时获取整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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