ste*_*evo 1 regex bash grep echo
我需要提取可以在一行中任何地方出现的序列号。序列号始终具有以下正则表达式格式:
S[XN]00-[0-9]{3}
Run Code Online (Sandbox Code Playgroud)
输出:
SX00-123
Run Code Online (Sandbox Code Playgroud)
我echo只想要这个。我可以grep使用序列号,并且正在考虑使用正则表达式,但不确定如何使用。
你可以试试grep,
grep -oE 'S[XN]00-[0-9]{3}' file
Run Code Online (Sandbox Code Playgroud)
来自grep --help,
-E, --extended-regexp PATTERN is an extended regular expression (ERE)
-o, --only-matching show only the part of a line matching PATTERN
Run Code Online (Sandbox Code Playgroud)
例:
$ cat test.txt
randomtextSX00-456
SX00-164randomtext
randomtextSX00-184randomtext
SX00-986
randomtext
Run Code Online (Sandbox Code Playgroud)
$ grep -oE 'S[XN]00-[0-9]{3}' test.txt
SX00-456
SX00-164
SX00-184
SX00-986
Run Code Online (Sandbox Code Playgroud)