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