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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to catch a two word keyword which may contain a new line(may include spaces or tab) in it?
# 1  
Old 10-26-2012
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.
Code:
$more a.txt
create view 
as
(select  from
.........
..........
(   select
....
(
select
......
..
 
select only no (((
number
(             select
end

Code:
 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?

Last edited by jim mcnamara; 10-26-2012 at 05:52 PM..
# 2  
Old 10-26-2012
Don't think I understand, check out the below:
Code:
$ 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

# 3  
Old 10-26-2012
Code:
egrep "\([[:space:]]*select|select$" a.txt

# 4  
Old 10-27-2012
@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
Code:
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 .

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

(      select

output should be :
Code:
(
select
(
select all 
(           select


Last edited by Scrutinizer; 10-27-2012 at 07:52 AM.. Reason: code tags
# 5  
Old 10-27-2012
Try:
Code:
sed -n '/(/{/select/!N;//p;}' infile


Last edited by Scrutinizer; 10-27-2012 at 09:37 AM..
# 6  
Old 10-27-2012
With a sed which supports bracket expressions:
Code:
sed -n ':strt
/([[:space:]]*select/{;p;b;}
/([[:space:]]*/{;N;b strt;}' file


Last edited by elixir_sinari; 10-27-2012 at 09:47 AM..
# 7  
Old 10-28-2012
@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

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

the output should be:
Code:
(select  from
(   select
(
select
(             select
(
select all
as (
select

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need to extract the word after a particular keyword throughout the file..

Hi Everyone, Need help in extracting the hostname from the below output. Expected output: DS-TESTB-GDS-1.TEST.ABC.COM DS-TESTB-GDS-2.TEST.ABC.COM .... ... /tmp $ cat -n /tmp/patchreport 1 /usr/bin/perl /admin/bin/patch/applyPatches.pl --apply_patches... (4 Replies)
Discussion started by: thiyagoo
4 Replies

2. Shell Programming and Scripting

Removing tab spaces at the end of each line

I have a file which contains the data lines like below.I want to remove the tab spaces at the end of each line.I have tried with the command sed 's/\+$//' file.but it does not work.Can anyone help me on this? 15022 15022 15022 15022 15022 15022 15023 15023 15023 15023 15023 ... (16 Replies)
Discussion started by: am24
16 Replies

3. Shell Programming and Scripting

Search for a Keyword in file and replace another keyword or add at the end of line

Hi I want to implement something like this: if( keyword1 exists) then check if(keyword2 exists in the same line) then replace keyword 2 with New_Keyword else Add New_Keyword at the end of line end if eg: Check for Keyword JUNGLE and add/replace... (7 Replies)
Discussion started by: dashing201
7 Replies

4. Shell Programming and Scripting

How to get the next word which falls just after a keyword?

Hi friends, i just want to know the command though which i can get the next word which comes just after a particluar keyword. For example: suppose text.out is file which contains a pl/sql procedure . i want to find out the word which falls just after the "table1" keyword. Thank... (7 Replies)
Discussion started by: neelmani
7 Replies

5. Shell Programming and Scripting

catch a particular word from a specific line -perlscript

Hi, App.log contains the data- ================================================= Value of DsRef =null Recovery File exixts Recovered readFile 20110509 17:00:00.369019 +0100s The DsRef Recovered from Recovery.txt file : 20110509 17:00:00.369019 +0100 Recovered from Recovery.txt file... (2 Replies)
Discussion started by: pspriyanka
2 Replies

6. Shell Programming and Scripting

Include white spaces while using CUT command

Hi I tried to extract 19 characters (default) enclosed with in tag from a file using cut command. If the characters comprises of double space, the cut command gives the output with a single spacing. file 1 <name>Kumar Rajasekaran</name> cut -c7-26 "file1" the out put i received is ... (48 Replies)
Discussion started by: Sekar1
48 Replies

7. Shell Programming and Scripting

Removing blank spaces, tab spaces from file

Hello All, I am trying to remove all tabspaces and all blankspaces from my file using sed & awk, but not getting proper code. Please help me out. My file is like this (<b> means one blank space, <t> means one tab space)- $ cat file NARESH<b><b><b>KUMAR<t><t>PRADHAN... (3 Replies)
Discussion started by: NARESH1302
3 Replies

8. Shell Programming and Scripting

Replace spaces between strings in a line with tab

Hi All I am having problem in substitution of any number of spaces, or a combination of space and tab in between strings in the lines of text file. Is there any way out in Perl? Please help me. e.g., Say the input is in the following format:- XX yyy zzz... (1 Reply)
Discussion started by: my_Perl
1 Replies

9. Shell Programming and Scripting

Replace only if the keyword is the first word in every line

How do I replace only if the keyword is at the begining of a line? Code: -- a = “This is a print statement” print a -- What if I want to replace print by #print only in the second line i.e only if the line starts with that keyword. Please help me out. I'm new to SED. -----Post... (5 Replies)
Discussion started by: alexzubin
5 Replies

10. Shell Programming and Scripting

How to include PDF file with spaces

I am able to include a pdf file as an attachment in an email using the following: echo "" > reports elm -s "RW100 PDF Reports" me@myemail.com < reports However, if I have a filename that contains some spaces, I'm not so lucky. I've tried: echo "" > reports but no luck. I keep getting... (1 Reply)
Discussion started by: lawadm1
1 Replies
Login or Register to Ask a Question

Featured Tech Videos