CLK
CLK

Reputation: 221

Using pcregrep -M to search for a multi-line string

I am trying to use pcregrep -M to search for a multiline string.

This is the line in my script:

lineNumber=$(pcregrep -Mn '$firstLine.*\n.*$secondLine.*' $myFile)

myFile contains multiple lines of the form:

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.

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?

Should I use awk or sed instead and if so how?

Upvotes: 3

Views: 6483

Answers (1)

John1024
John1024

Reputation: 113814

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.

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.

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 '/^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:

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:

declare -p lineNumbers

Upvotes: 3

Related Questions