search for a pattern using awk between two words


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting search for a pattern using awk between two words
# 1  
Old 01-16-2012
search for a pattern using awk between two words

Hi,
how can we search for a pattren between two words? below are the examples

input
1)select from table_name c1,c2,c3,c4,fn(),fn2(),c5;-->false
2)select from table_name c1,c2,c3,c4;--True
3)select from table
c1,
c2,
c3,
fn(),
c4;-->true
4)select from table_name
c1,
c2,
c3;-->true

in the above 4 examples
1) the pattren will be same each sellect will end with ; and start with "select from" in between it may have column names and function calls.
we have to search from "select from" till ; and check if functions are called inside .
2) if something with ( and ) the its function call and we should print false for that. else true.

could some one help he getting this?
# 2  
Old 01-16-2012
Code:
 
$ nawk 'BEGIN{RS="\;"}{if($0~/\(/){print $0 "-->false"}else{print $0 "-->true"}}' input.txt

# 3  
Old 01-16-2012
Similar to the above solution, but with the semi colons printed and only for lines with select. One quirk is the spurious semicolon on the last line.
Code:
awk '{sub(/$/,RS)} /select/{s=/\(\)/?"true":"false";sub(/$/,"-->"s)}1' RS=\; infile


Last edited by Scrutinizer; 01-16-2012 at 02:29 AM..
# 4  
Old 01-16-2012
Thanks both the code is working fine..

but for the statements like below though function call is not there it is showing as false the below cases also shoulf be true.

select some text
(select some text(select some text)
where some condition)
where condition

this should be true. since there are no function calles

---------- Post updated at 12:09 PM ---------- Previous update was at 12:05 PM ----------

Quote:
Originally Posted by Scrutinizer
Similar to the above solution, but with the semi colons printed and only for lines with select. One quirk is the spurious semicolon on the last line.
Code:
awk '{sub(/$/,RS)} /select/{s=/\(\)/?"true":"false";sub(/$/,"-->"s)}1' RS=\; infile

in this case the above code should print false where as it is printing true.

select some text
(select some text(select some text)
where some condition)
where condition;--true
# 5  
Old 01-16-2012
Since my statement tests for "()" it should signal false. Do these select statements also get terminated with ;?
EDIT: I tried it and it returns false. Are you sure that is the result you are getting?
# 6  
Old 01-16-2012
Quote:
Originally Posted by Scrutinizer
Since my statement tests for "()" it should signal false. Do these select statements also get terminated with ;?
yes it wil end with ;
sorry for confusion,for your case it should say true.
# 7  
Old 01-16-2012
Found it. I had true and false reversed:
Code:
awk '{sub(/$/,RS)} /select/{s=/\(\)/?"false":"true";sub(/$/,"-->"s)}1' RS=\; infile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grep/awk using a begin search pattern and end search pattern

I have this fileA TEST FILE ABC this file contains ABC; TEST FILE DGHT this file contains DGHT; TEST FILE 123 this file contains ABC, this file contains DEF, this file contains XYZ, this file contains KLM ; I want to have a fileZ that has only (begin search pattern for will be... (2 Replies)
Discussion started by: vbabz
2 Replies

2. Shell Programming and Scripting

Search words in any quote position and then change the words

hi, i need to replace all words in any quote position and then need to change the words inside the file thousand of raw. textfile data : "Ninguno","Confirma","JuicioABC" "JuicioCOMP","Recurso","JuicioABC" "JuicioDELL","Nulidad","Nosino" "Solidade","JuicioEUR","Segundo" need... (1 Reply)
Discussion started by: benjietambling
1 Replies

3. Shell Programming and Scripting

Awk to match a pattern and perform a search after the first pattern

Hello Guyz I have been following this forum for a while and the solutions provided are super useful. I currently have a scenario where i need to search for a pattern and start searching by keeping the first pattern as a baseline ABC DEF LMN EFG HIJ LMN OPQ In the above text i need to... (8 Replies)
Discussion started by: RickCharles
8 Replies

4. Shell Programming and Scripting

awk get search pattern from a file.

Here Is a problem I am facing with awk. Query --> I want to search for a string in a file and print next 15 lines below the matched string. 1.We do not have GNU grep so cannot use grep -A or grep -B commands. 2. Instead of passing the search pattern as a string to awk. I want the awk to... (4 Replies)
Discussion started by: togotutor
4 Replies

5. Shell Programming and Scripting

awk or grep to match # of words before and after pattern

Pipe binary file matches grep results to file I am using grep to match a pattern, but the output is strange. $ grep -r -o "pattern" * Gives me: Binary file foo1 matches Binary file foo2 matches Binary file foo3 matches To find the lines before/after, I then have to use the... (1 Reply)
Discussion started by: chipperuga
1 Replies

6. UNIX for Dummies Questions & Answers

Pattern search using awk

Hi All, I am trying to find numbers with balance greater than 1 and less than equal 2 from the below file using awk Input file num ,bal 100199,1.708 100225,0 100226,0 100228,0.771166 100232,2 output file 100199,1.708 100232,2 I am using the following command for this... (2 Replies)
Discussion started by: pistachio
2 Replies

7. Shell Programming and Scripting

Pattern Search using AWK

Hi All, I have the below file data.txt.Using awk i want to grep all the zone data.Form the below command i can extact data upto of zone i give but i want it should print until next pattern. awk '/^Total Collection /{c=5;next}c-->0' zin45srs08.tools_utilization instead of c=5 is it possible... (5 Replies)
Discussion started by: ajaincv
5 Replies

8. Shell Programming and Scripting

AWK:- matching pattern search

Dear Friends, I have a flat file. To pick certain details we have written an awk where we are facing difficulty. Sample of flat file. line 1 line 2 line 3 line 4 line 5 line 6 line 7 line 8 line 9 line 10 line 11 line 12 line 13 line 14 (Matching pattern "Lkm_i-lnr:"can be... (4 Replies)
Discussion started by: anushree.a
4 Replies

9. Shell Programming and Scripting

awk search pattern

Hi, I have a log file which contains lines like below: 2010-07-19 07:13:19,021 ERROR system ...(text) 2010-07-19 07:22:03,427 ERROR system ...(text) class com... (text) 2010-07-19 07:23:19,026 ERROR system ...(text) class com... (text) each line is a separate line... I am given the a... (14 Replies)
Discussion started by: a27wang
14 Replies

10. Shell Programming and Scripting

search several words with awk command

Hello, I want to test if i find the word CACCIA AND idlck in a file, i have to print a message Ok. For that , i need to user a awk command with a && logical. Can you help me ? :confused: ### CACCIA: DEBUT ### if $(grep -wqi "$2" /etc/passwd); then && rm /etc/security/.idlck ... (3 Replies)
Discussion started by: khalidou13
3 Replies
Login or Register to Ask a Question