2

I've declared a string with two newlines inside of string

somestring=$'\n##### Branch FREEZE enable/disable\nRelease:'

I have a $file with a text inside like this

###############################

##### Branch RELEASE enable/disable
Release: disable

##### Branch FREEZE enable/disable
Freeze: disable

##### Mail list #####

I am trying to figure out, if there is a string inside with both of the newlines with a command

if grep -q "$somestring" "$file"; then echo "found the string"

But the result is always positive, when there is a newline inside of string.

How can I make it work correct with newlines inside?

dice2011
  • 937
  • 2
  • 15
  • 30

1 Answers1

1

grep patterns are matched against individual lines so there is no way for a pattern to match a newline found in the input.

try pcregrep instead of regular grep:

pcregrep -M "pattern1.*\n.*pattern2" filename

the -M option allows it to match across multiple lines, so you can search for newlines as \n.

Srini V
  • 11,045
  • 14
  • 66
  • 89