If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > How to recognize patterns in a file

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-07-04, 16:08
Serg Serg is offline
Registered User
 
Join Date: Feb 2004
Posts: 52
Thumbs up How to recognize patterns in a file

Hi everybody.

I have being trying to solve this with awk but there is always something
missing. I have one file like this :

CONFIGURATION : 1
a b c 2.5
aa b1 c1 1.3
a2 b2 c2 4.9
...
aN bN cN 8.7
CONFIGURATION : 2
z d g 4.2
aa d1 g1 12.3
z2 d2 g2 4.
...
zN dN gN 6.4
CONFIGURATION : 3
t e h 54.
CONFIGURATION : 4
CONFIGURATION : 5
...
...
CONFIGURATION : M
w y i 0.
aa y1 i1 1.
w2 y2 i2 4.2
...
wN yN iN 2.4

As you can see, between the lines containing the word CONFIGURATION
there is a number of lines (0 to N), each of them contains 4 columns.

What I need to do is quite simple but painful without a good script.
For each configuration, I must look for the word "aa". When I have
a hit, I must print the third and fourth columns and the configuration number.

I know how to do this with awk. The problem is when I do NOT have
hit. In the lines above, configurations 4 and 5 shows nothing.
I could not figure out how to build a logic flag yet.

Therefore, the output for the example above should be :

(configuration) (third column) (fourth column)
1 c1 1.3
2 g1 12.3
3 0 0
4 0 0
5 0 0
...
M i1 1.

I appreciate any help !

Thanks,

Serg
Reply With Quote
  #2 (permalink)  
Old 06-07-04, 18:16
vgersh99 vgersh99 is offline
Registered User
 
Join Date: Apr 2004
Location: Boston, MA
Posts: 325
Code:
BEGIN {
  FS=":"
}

$1 ~ "^CONFIGURATION" { block = $2 ; next }
{ print block, $0 }
Reply With Quote
  #3 (permalink)  
Old 06-08-04, 08:26
Serg Serg is offline
Registered User
 
Join Date: Feb 2004
Posts: 52
Thanks for the reply.

How do I execute your script ?
I tried using awk but it did not work :

[serg@galactic test]$ awk -f sh.script data
1 a b c 2.5
1 aa b1 c1 1.3
1 a2 b2 c2 4.9
1 aN bN cN 8.7
2 z d g 4.2
2 aa d1 g1 12.3
2 z2 d2 g2 4.
2 zN dN gN 6.4
3 t e h 54.
5

Thanks,

Serg
Reply With Quote
  #4 (permalink)  
Old 06-08-04, 08:33
vgersh99 vgersh99 is offline
Registered User
 
Join Date: Apr 2004
Location: Boston, MA
Posts: 325
Sorry - I thought you needed the ENTIRE line printed.
Here's the modified version and yes, that's how you would run script.

Code:
BEGIN {
  FS=":"
}

$1 ~ "^CONFIGURATION" { block = $2 ; next }
{ print block, $3, $4 }
Reply With Quote
  #5 (permalink)  
Old 06-09-04, 10:20
Serg Serg is offline
Registered User
 
Join Date: Feb 2004
Posts: 52
Excellent ! It worked nicely !
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On