I was trying to search for lines that start with create
and end in ;
. The match may span multiple lines. I was trying to use grep for that and after searching internet I found out how to do it.
The following query does it
grep -zioE 'create (\w|\W|\n)*?;' Day1.sql | less
Output
create schema sigmoid_db;
create table instructor(
ID char(5),
name varchar(20),
dept_name varchar(20),
salary numeric(8,2));
What I want to ask why wouldn't the same query without \n
work? Like the following query should produce the same output
grep -zioE 'create (\w|\W)*?;' Day1.sql | less
Output
create schema sigmoid_db;
My reasoning is \w|\W
should match any character. But the second command doesn't print the patterns that span multiple lines.
Can anyone tell why so?
*?
for non-greedy*
is a perl operator. Very fewgrep
implementations support it with-E
. – Stéphane Chazelas Feb 10 '23 at 11:02pcregrep -Mio '(?s)^\h*create .*?;'
. – Stéphane Chazelas Feb 10 '23 at 11:04grep (BSD grep, GNU compatible) 2.6.0-FreeBSD
– Dhruv Feb 10 '23 at 11:14printf 'a\nb' | grep -zEo 'a\Wb'
match for you? – Stéphane Chazelas Feb 10 '23 at 11:472.5.1-FreeBSD
on FreeBSD 12.3, I find thatprintf 'a\nb' | grep -zEo 'a\Wb'
matches.printf 'a\nb' | grep -zEo a.b
also matches in that it reports a zero exit status, but outputs nothing which looks very much like a bug. – Stéphane Chazelas Feb 10 '23 at 12:05printf 'a\nb' | grep -zEo 'a\Wb'
doesn't work and neither doesprintf 'a\nb' | grep -zEo 'a.b'
. I am using Mac. – Dhruv Feb 10 '23 at 12:11