bash 使用 pcregrep -M 搜索多行字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24520428/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using pcregrep -M to search for a multi-line string
提问by CLK
I am trying to use pcregrep -M
to search for a multiline string.
我正在尝试用于pcregrep -M
搜索多行字符串。
This is the line in my script:
这是我的脚本中的一行:
lineNumber=$(pcregrep -Mn '$firstLine.*\n.*$secondLine.*' $myFile)
myFile
contains multiple lines of the form:
myFile
包含多行形式:
firstLine\n
secondLine(with other characters here that I don't need to match)
I get an empty string for lineNumber
and that's all.
我得到一个空字符串lineNumber
,仅此而已。
What am I doing wrong?
我究竟做错了什么?
What should I expect for the return value? Shouldn't -n
give me the line number? And if so, which line number, first or second?
我应该对返回值有什么期望?不应该-n
给我行号吗?如果是这样,哪个行号,第一还是第二?
Should I use awk
or sed
instead and if so how?
我应该使用awk
还是sed
代替,如果是,如何使用?
回答by John1024
First, a working regex is needed. If I correctly understand what you asking for, this will work:
首先,需要一个有效的正则表达式。如果我正确理解您的要求,这将起作用:
pcregrep -Mn '^firstLine.*\n^secondLine' myFile
Note, that this prints more than just the line numbers. As per the pcregrep
man page, it also prints the the matching lines.
请注意,这不仅仅是打印行号。根据pcregrep
手册页,它还打印匹配的行。
If you want to print just the starting line numbers, try:
如果您只想打印起始行号,请尝试:
sed -n '/^firstLine/=' myFile
the regex /^firstLine/
selects the first line and the command =
tells sed
to print the line number.
正则表达式/^firstLine/
选择第一行,命令=
告诉sed
打印行号。
To print just the ending line numbers:
只打印结束行号:
sed -n '/^secondLine/=' myFile
To get both and any line in between:
要获得两者之间的任何一条线:
sed -n '/^firstLine/,/^secondLine/=' myFile
awk
can also be used. The line selection is similar. The command to print the line number differs. For example:
awk
也可以使用。线路选择类似。打印行号的命令不同。例如:
awk '/^firstLine/ {print NR}' myFile
Capturing the line numbers into a variable
将行号捕获到变量中
The line numbers can be captured into a variable with command substitution:
可以使用命令替换将行号捕获到变量中:
lineNumber=$(awk '/^firstLine/ {print NR}' myFile)
However, if there are more two or more line numbers, that may not be useful to you. In that event, if you are using a shell that supports arrays, such as bash
, you may prefer to capture the line numbers into an array as follows:
但是,如果有两个或多个行号,那可能对您没有用。在这种情况下,如果您使用支持数组的 shell,例如bash
,您可能更喜欢将行号捕获到数组中,如下所示:
lineNumbers=($(awk '/^firstLine/ {print NR}' myFile))
If you are unfamiliar with arrays, note that statements such as echo $lineNumbers
will not display the entire array, only its first element. To see the whole array, run:
如果您不熟悉数组,请注意诸如此类的语句echo $lineNumbers
不会显示整个数组,只会显示其第一个元素。要查看整个数组,请运行:
declare -p lineNumbers