简体   繁体   English

Unix Shell脚本从字符串中提取数字

[英]Unix Shell Script to extract the number from the String

How to extract the bold number from the string below using unix shell script? 如何使用Unix Shell脚本从下面的字符串中提取粗体数字?

17: H.0(-2073):File ID (40008)in xyz file not equal to the file ID(**40004**)in file header.

Thanks :) 谢谢 :)

echo '17: H.0(-2073):File ID (40008)in xyz file not equal to the file ID(40004)in file header.' | sed -e 's/.*(\([0-9]*\)).*/\1/'

The second part of this line runs sed with command s (substitution). 该行的第二部分使用命令s (替代)运行sed Part between first two slashes ( / ) is regular expression which matches the following: 前两个斜杠( / )之间的部分是与以下内容匹配的正则表达式

Everything ( .* ) in greedy manner, ie until the last occurrence of any number of digits in brackets ( ([0-9]*) ) and then everything again ( .* ) until the end of line. 所有内容( .* )都以贪婪的方式显示,即直到最后一次出现括号中的任意数量的数字( ([0-9]*) ),然后再次出现所有内容( .* )直至行尾。 Expression between \\( and \\) (ie 40004 in this case) is memorized to be used in the second part of s command. \\(\\)之间的表达式\\(在这种情况下,即40004 )被存储为在s命令的第二部分中使用。

The part between the second / and third / is what we want to place instead of the line matched with regular expression. 第二之间的部分/第三/是我们要放置的,而不是用正则表达式匹配的行。 Here it is \\1 , meaning reference to the substring between 1st occurrence of \\( and \\) which is 40004 in our case. 这里是\\1 ,表示对第一次出现\\(\\)之间的子字符串的引用,在本例中为40004

So the part after | 所以之后的部分| replaces the whole input string with string 40004 extracted from it. 将整个输入字符串替换为从其中提取的字符串40004 Regular expressions are powerful but often write-only technique, so I hope this explanation will bring a bit more clarity. 正则表达式是强大的功能,但通常是只写技术,因此我希望这种解释会带来更多的清晰度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM