How to catch a two word keyword which may contain a new line(may include spaces or tab) in it?

How to catch a two word keyword which may contain a new line(may include spaces or tab) in it.
for example there is a file a.txt.

$more a.txt
create view 
as
(select  from
.........
..........
(   select
....
(
select
......
..
 
select only no (((
number
(             select
end
 grep "([[:space:]]*select" a.txt
output:
(select  from
(   select
(             select

but i want the 3rd select also to be caught(where there is a newline between "(" and "select" keyword)
i.e
(
select
what modification i should do in my grep command?

Don't think I understand, check out the below:

$ cat t
create view
as
(select  from
.........
..........
(   select
....
(
select
......
..

select only no (((
number
(             select
end

This finds all of them:
$ grep "select" t
(select  from
(   select
select
select only no (((
(             select
egrep "\([[:space:]]*select|select$" a.txt

@spacebar Thanks for our reply. but i dont want all to be listed out. i want to catch a two word keyword(here it is "(" and "select" words) which may be seperated by a new line(may be searated by spaces or tab) . so "select only no (((" should not be there in the output.

@bipinajith

egrep "\([[:space:]]*select|select$" a.txt

in the second filter, you are assuming that it ends with "select" which is not the case always. The only goal is to catch a two word keyword(here it is "(" and "select" words) which may be seperated by a new line(may be searated by spaces or tab). for example b.txt .

more b.txt
(
select
....
(
select all
...
as 
select

(      select

output should be :

(
select
(
select all 
(           select

Try:

sed -n '/(/{/select/!N;//p;}' infile

With a sed which supports bracket expressions:

sed -n ':strt
/([[:space:]]*select/{;p;b;}
/([[:space:]]*/{;N;b strt;}' file

@scrutinizer + sinari.. thanks for you reply. The sed command given by you is working for b.txt. But it will not work for all the cases to catch a two word keyword(here it is "(" and "select" words) which may be seperated by a new line(may be searated by spaces or tab) . for example

more c.txt
create view 
as
(select  from
.........
..........
(   select
....
(
select
......
..
 
select only no (((
number
(             select
 
.....
(
select all
.......
........
as (
select
........
 
end

the output should be:

(select  from
(   select
(
select
(             select
(
select all
as (
select

My bad.
Try a slight modification:

sed -n ':strt
/([[:space:]]*select/{;p;b;}
/([[:space:]]*$/{;N;b strt;}' file
1 Like
sed -n '/(/{/(.*select/!N;//p;}' infile
1 Like

@Scrutinizer: thanks for your reply.. could you please explain me the below command(especially the part highlighted in red.

sed -n '/(/{/(.*select/!N;//p;}' infile

@sinari
Hey thanks for your reply.. could you please explain me the below command(especially the part highlighted in red.

sed -n ':strt
/([[:space:]]*select/{;p;b;}
/([[:space:]]*$/{;N;b strt;}' file

Thanks in advance

Sure:

/(.*select/!N     if the line consists of a "(" followed by "select" then do not append the next line to the buffer..
//p               repeat the previous match operation ( "(" followed by "select" ) and print the buffer if successful 
1 Like

@scrutinizer
thanks a lot.
suppose we have to perform my serch on a folder like:

sed -n '/(/{/(.*select/!N;//p;}' path_name/*

i am getting all the matching line without the file name. Could you please modily the command so that i get file name plus the matching lines??

thanks a lot in advance

For that an awk version may be better suited, for example:

awk '/\(/{if(!/\(.*select/){getline p; $0=$0 RS p} if(/\(.*select/) print FILENAME ": " $0}' infile