awk script 
Author Message
 awk script

Hi,  

I have a log file, which has three parts.
Each part start with "Begin-1" or "Begin-2", and they end with "End".
How can I print out two paragraphs have line "yes yes yes"

Thanks in advance.
Minh

Following is my log file:
-----------------------------------------------
Begin-1
111
222
yes yes yes
333
444
End

Begin-2
111
222
yes yes yes
333
444
End

Begin-1
111
222
no no no
333
444
End



Tue, 01 May 2001 03:00:00 GMT  
 awk script

Quote:

>Hi,  

>I have a log file, which has three parts.
>Each part start with "Begin-1" or "Begin-2", and they end with "End".
>How can I print out two paragraphs have line "yes yes yes"

>Thanks in advance.
>Minh

>Following is my log file:
>-----------------------------------------------
>Begin-1
>111
>222
>yes yes yes
>333
>444
>End

>Begin-2
>111
>222
>yes yes yes
>333
>444
>End

>Begin-1
>111
>222
>no no no
>333
>444
>End

This awk/gawk script should do the job:

/^Begin-[12]/,/^End/ {a[n++]=$0; if ($0=="yes yes yes"){p=1}}
/^End/ {if ( p==1){for(i=0;i<n;i++){print a[i]};print ""};n=0;p=0}

the first line puts the paragraph into an array and sets a flag (p) if
"yes yes yes" was one of the lines.

the second line prints the array if the flag (p) was set, and then
resets the flag and array index.  I put in a print "" to put a
spacer line after printing the paragraph.

Chuck Demas
Needham, Mass.

--
  Eat Healthy    |   _ _   | Nothing would be done at all,

  Die Anyway     |    v    | That no one could find fault with it.



Tue, 01 May 2001 03:00:00 GMT  
 awk script

Quote:

> I have a log file, which has three parts.
> Each part start with "Begin-1" or "Begin-2", and they end with "End".
> How can I print out two paragraphs have line "yes yes yes"

How about this script?

     /yes yes yes/

Now all you need to do is define a record as a block of several lines
of text separated by one or more blank lines. Just set the value of
the built-in variable RS to the null string.

Here's a demonstration:

$ awk '/yes yes yes/' RS='' ORS='\n\n' input.txt
Begin-1
111
222
yes yes yes
333
444
End

Begin-2
111
222
yes yes yes
333
444
End

$ awk '/no no no/' RS='' ORS='\n\n' input.txt
Begin-1
111
222
no no no
333
444
End

$ cat input.txt
Begin-1
111
222
yes yes yes
333
444
End

Begin-2
111
222
yes yes yes
333
444
End

Begin-1
111
222
no no no
333
444
End
$

Use different values of OFS and ORS to control the output format.

See pp. 60-61 and 82-85 of _The AWK Programming Language_ (ISBN
0-201-07981-X) for more information and examples.

--
Jim Monty

http://www.primenet.com/~monty/
Tempe, Arizona USA



Tue, 01 May 2001 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Creating an awk script to extract other scripts from a file

2. Access to Script Name Within Awk Script

3. Replacing a string from an input file within awk script

4. Awk-Script for Text Analysis

5. awk script not working on SUN Solaris

6. AWK-Script to set a shell variable (Win32) ?

7. Help optimising AWK script

8. awk script error

9. cgi awk script

10. name of awk script

11. piping input files into awk script

12. problems with awk script

 

 
Powered by phpBB® Forum Software