Assistance needed for pattern matching


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Assistance needed for pattern matching
# 1  
Old 12-24-2010
Question Assistance needed for pattern matching

Hi,

Below are input files:-

File1:-
Code:
1: ### CREATE TABLE  ##
2: SOMETHING ELSE 1
3: ### CREATE TABLE  ##
4: ### CREATE   <spaces>
5: TABLE ###
6:SOMETHING ELSE 2
7: ##  CREATE
8: <spaces>   TABLE ###
9: SOMETHING ELSE 3

File2:-
Similar Format...

Desired 1 Output File:-
Code:
File1: 1: ### CREATE TABLE  ##
File1: 3: ### CREATE TABLE  ##
File1: 4: ### CREATE   <spaces>
File1: 5: TABLE ###
File1: 7: ##  CREATE
File1: 8: <spaces>   TABLE ###

File2: ...
..

So, as is clear from Example, I am looking for "CREATE TABLE" .

If it is found in 1 line, then just print that line with File Name.
Else is it is spread in 2 lines, print both lines with file Name & continue scanning from 3rd line.

I was trying to use sed, but it is not getting me desired output :-

sedfile:-
Code:
s/CREATE TABLE/&/
/CREATE/ {N; s/CREATE *\n* *TABLE/&/p}

Code:
sed -f sedfile Filename

Thanks for your help.Smilie

Last edited by Scott; 12-24-2010 at 04:24 AM.. Reason: Code tags, please...
# 2  
Old 12-24-2010
It appears quiet straightforward for me using egrep:

egrep "CREATE|TABLE" File1

Hope it helped.
# 3  
Old 12-24-2010
Thanks, but it doesn't meet my all requirements.

Because, if input file is:-
Code:
1: ### CREATE TABLE  ##
2: SOMETHING ELSE 1 CREATE
3: ### CREATE TABLE  ##
4: ### CREATE   <spaces>
5: TABLE ###
6:SOMETHING ELSE 2
7: ##  CREATE
8: <spaces>   TABLE ###
9: SOMETHING ELSE 3

then , it will give o/p as :-
Code:
1: ### CREATE TABLE  ##
2: SOMETHING ELSE 1 CREATE   // NOT NEEDED
3: ### CREATE TABLE  ##
4: ### CREATE   <spaces>
5: TABLE ###
7: ##  CREATE
8: <spaces>   TABLE ###

But, Line 2 in o/p should not be printed because, it has only CREATE but there is no TABLE keyword after that.

So, what is needed is to find all occurrences of "CREATE TABLE" with only Newlines & spaces permitted between 2 words.

Last edited by Scott; 12-24-2010 at 08:02 AM.. Reason: Code tags, PLEASE!
# 4  
Old 12-24-2010
Code:
awk '/CREATE/||/TABLE/ {a=$0;gsub("#*","");for(i=2;i<=NF;i++) if($i != "CREATE" && $i != "TABLE") next; print FILENAME": "a}' File1 File2 File3 File4

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Archiving assistance needed date to month

We have year folder say in a path /opt/informat/Archive a folder 2012. And in the same folder /opt/informat/Archive we have different folders month based, like 201210, 201211, 201212, this have data for each month, ie files. Now time to time i need to move the monthly folders to the main folder... (1 Reply)
Discussion started by: raghavraok
1 Replies

2. Shell Programming and Scripting

Awk - Script assistance on identifying non matching fields

Hoping for some assistance. my source file consists of: os, ip, username win7, 123.56.78, john win7, 123.56.78, paul win7, 10.1.1.1, john win7, 10.2.2.3, joe I've been trying to run a script that will only return ip and username where the IP address is the same and the username is... (3 Replies)
Discussion started by: tekvaio
3 Replies

3. Shell Programming and Scripting

assistance needed to add 2 other routines to my script

Hello guys, In my script, I need to add two other routines where I Manipulate the files with a 'x' The routine looks at CLI named qip-getobjectprof that references a input file named hosts_list.txt Then I use the CLI named qip-setobject to set the orignal name with an 'x'and move the... (3 Replies)
Discussion started by: richsark
3 Replies

4. UNIX for Advanced & Expert Users

Assistance Needed With Find/Replace in Vi

Hello All I have always had a question about find and replace in Vi. As this uses Vi, sed, and RegEx I never knew how or where to post the question but I thought I would give it a shot here. Say I have a text file filled with the following: Sue, your IP address is 192.168.1.10 which is... (4 Replies)
Discussion started by: NoSalt
4 Replies

5. Shell Programming and Scripting

Assistance needed with perl script

Ok, theres a log file containing the below. Lets call the logfile log_fantastic: 2009/03/16 21:42:45 USER: tonnabo - MAC: 0014BF2D385A - STATUS_ID: 30 - STATE: ERROR 2009/03/16 21:42:45 USER: tonnabo - MAC: 001310AC120D - STATUS_ID: 15 - STATE: OK 2009/03/16 21:42:45 USER: tonnabo - MAC:... (5 Replies)
Discussion started by: SkySmart
5 Replies

6. Shell Programming and Scripting

help needed .. Unable to write the data to new file after matching the pattern

Hi, i am pretty new to Unix environment ..... Can i get some help from any of you guyz on writing Unix script. my requirement is like reading a csv file, finding a specific pattern in the lines and repalce the string with new string and write it to another file. My file is file ABC123.dat... (3 Replies)
Discussion started by: prashant_jsw
3 Replies

7. UNIX for Dummies Questions & Answers

Yellow Book assistance needed

Has anyone worked with yellow book in a unix/linux environment? If so, could you provide me with more answers around this? (2 Replies)
Discussion started by: FL1
2 Replies

8. UNIX for Dummies Questions & Answers

Assistance needed.

the command "nawk" returns the error command cannot be found in my unix system. Is there a specific library i need to have to use this command? I tried, the whereis command and it returns nothing. if there is nothing to do, what command can i use to replace this nawk command? Appreciate some... (4 Replies)
Discussion started by: 12yearold
4 Replies

9. UNIX for Dummies Questions & Answers

NFS mount assistance needed...

Hello all - I've searched this forum, but was unable to find out the info I need. I'm trying to mount (nfs mount) a directory on another box from my Linux machine. The mount point resides on a Tru64 digital unix machine. The machine trying to do the mount is a Linux machine (redhat 8.0). ... (2 Replies)
Discussion started by: Heron
2 Replies
Login or Register to Ask a Question