awk script Problem


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk script Problem
# 1  
Old 07-05-2007
awk script Problem

I wrote a awk but doesnt work as expected.

The Input File
attached input file
My awk Script

/^......[^*].*EXEC CICS /,/END-EXEC/
{
if ( $0 ~ / LINK / )
{ tsflag=1 }
if ( $0 ~ /EXEC CICS/ && tsflag == 1 )
{print "This is Before EXEC CICS"}
if( $0 ~ /END-EXEC/ && tsflag ==1 )
{ print "This is After END-EXEC" }
};

I wanted to Insert a line before EXEC CICS and One After END-EXEC if it has a word LINK in it
But the Out put comes as follows


attached Otput

The After Print is fine but the Before One also comes after the EXEC CICS{ I didnt know how to put if before }

And the Last chunk
AAAAAA EXEC CICS
AAAAAA ABEND
AAAAAA ABCODE ( AB-CODE )
AAAAAA END-EXEC.

Matches Even if It Doesnt have LINK in it (Dont Know Why)


Need get 2 things working in this.....
1) TO put the Statement before EXEC CICS
2) Prevent it from adding if it Doesnt Contain LINK

Last edited by pbsrinivas; 07-05-2007 at 03:21 AM.. Reason: spaces are getting deleted after posting...
# 2  
Old 07-05-2007
Code:
$ cat file
           EXEC CICS
               ASKTIME
               ABSTIME ( WS-ABS-TIME )
           END-EXEC
           EXEC CICS LINK
                  PROGRAM ('PROGRAMA')
                  COMMAREA (COMM-POINTERS)
                  LENGTH (LENGTH OF COMM-POINTERS)
                  RESP (WS-RESP)
              END-EXEC
           EXEC CICS
           RETURN
           END-EXEC.
           EXEC CICS
           LINK PROGRAM ( 'PROGRAMB' )
               RESP ( WS-RESP )
           END-EXEC
AAAAAA    EXEC CICS
AAAAAA         ABEND
AAAAAA        ABCODE ( AB-CODE )
AAAAAA    END-EXEC.
$ awk '/EXEC CICS/,/END-EXEC/ {
> if ( $0 ~ /EXEC CICS/ )
>       str=$0
> else
>       str=str"\n"$0
>
> if ( $0 ~ /LINK/ ) tsflag=1
>
> if( $0 ~ /END-EXEC/ ){
>       if( tsflag ==1 ) {
>               str="This is Before EXEC CICS\n"str
>               str=str "\nThis is After END-EXEC"
>               tsflag=0
>       }
>       print str
> }
> next }; 1 ' file
           EXEC CICS
               ASKTIME
               ABSTIME ( WS-ABS-TIME )
           END-EXEC
This is Before EXEC CICS
           EXEC CICS LINK
                  PROGRAM ('PROGRAMA')
                  COMMAREA (COMM-POINTERS)
                  LENGTH (LENGTH OF COMM-POINTERS)
                  RESP (WS-RESP)
              END-EXEC
This is After END-EXEC
           EXEC CICS
           RETURN
           END-EXEC.
This is Before EXEC CICS
           EXEC CICS
           LINK PROGRAM ( 'PROGRAMB' )
               RESP ( WS-RESP )
           END-EXEC
This is After END-EXEC
AAAAAA    EXEC CICS
AAAAAA         ABEND
AAAAAA        ABCODE ( AB-CODE )
AAAAAA    END-EXEC.

# 3  
Old 07-05-2007
Quote:
Originally Posted by pbsrinivas
Matches Even if It Doesnt have LINK in it (Dont Know Why)
You must reset tsflag when you found the 'END-EXEC' line
You can do something like that :
Code:
/^......[^*].*EXEC CICS /,/END-EXEC/  {
   if ( $0 ~ / LINK / )
      tsflag = 1;
   if ( $0 ~ /EXEC CICS/ && tsflag == 1 ) {
      print "This is Before EXEC CICS"
      print $0;
      next;
   }
   else if ( $0 ~ /END-EXEC/ && tsflag ==1 ) {
      print $0;
      print "This is After END-EXEC"
      tsflag = 0;
      next;
   }
}
{
   print $0;
}

In fact, you don't need the tsflag :
Code:
/^......[^*].*EXEC CICS LINK/,/END-EXEC/  {
   if ( /EXEC CICS/ ) {
      print "This is Before EXEC CICS" 
      print $0;
      next;
   }
   else if ( /END-EXEC/ ) {
      print $0;
      print "This is After END-EXEC"
      next;
   }
}
{
   print $0;
}

# 4  
Old 07-05-2007
Dear Anbu

When i put the Scrip in a file and do awk -f the Statements get added fine but all the Lines Get Repeted..

Like this
EXEC CICS
ASKTIME
ABSTIME ( WS-ABS-TIME )
END-EXEC
EXEC CICS
ASKTIME
ABSTIME ( WS-ABS-TIME )
END-EXEC
EXEC CICS
FORMATTIME ABSTIME ( WS-ABS-TIME )
TIME ( WS-CUR-TIME )
END-EXEC
EXEC CICS
FORMATTIME ABSTIME ( WS-ABS-TIME )
TIME ( WS-CUR-TIME )
END-EXEC


Aigles

Ur Script is fine but...

the LINK can Appear in same Line or the Next

As in My input file

its EXEC CICS LINK once and

EXEC CICS
LINK PRORGAM ..

Then is doesnt do..
# 5  
Old 07-05-2007
Quote:
Originally Posted by pbsrinivas
Dear Anbu

When i put the Scrip in a file and do awk -f the Statements get added fine but all the Lines Get Repeted..

Like this
EXEC CICS
ASKTIME
ABSTIME ( WS-ABS-TIME )
END-EXEC
EXEC CICS
ASKTIME
ABSTIME ( WS-ABS-TIME )
END-EXEC
EXEC CICS
FORMATTIME ABSTIME ( WS-ABS-TIME )
TIME ( WS-CUR-TIME )
END-EXEC
EXEC CICS
FORMATTIME ABSTIME ( WS-ABS-TIME )
TIME ( WS-CUR-TIME )
END-EXEC
Try this
Code:
awk '/EXEC CICS/,/END-EXEC/ { 
if ( $0 ~ /EXEC CICS/ ) 
	str=$0
else	
	str=str"\n"$0

if ( $0 ~ /LINK/ ) tsflag=1

if( $0 ~ /END-EXEC/ ){
	if( tsflag ==1 ) {
		str="This is Before EXEC CICS\n"str
		str=str "\nThis is After END-EXEC"
		tsflag=0 		
	} 
	print str 
}
} ' file

# 6  
Old 07-05-2007
Thanks anbu aigles...

It works now..

Anbu what was the 1 at the end For...??
# 7  
Old 07-05-2007
Quote:
Originally Posted by pbsrinivas
Thanks anbu aigles...

It works now..

Anbu what was the 1 at the end For...??
The code
Code:
awk '1' file

is a short form for :
Code:
awk '{ print $0 }' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk script problem

Hello guys i have following problem. I'm trying to copy content of one file and paste this content in all .txt files in directory, but at line 15. My script copy the content at first line, not 15. I'm confused how to do this. Thank you in advance for your help! This is my script: ARGS=2 ... (9 Replies)
Discussion started by: r00ty
9 Replies

2. Shell Programming and Scripting

Awk Script Problem

Can someone please explain to me what is wrong with this awk script? echo 74 85 | awk '{ if ( $1 > $2 ) PRESULTS = ( $1 - $2 ); print $0,"=>","P"PRESULTS ; else if ( $1 > $2 ) NRESULTS = ( $2 - $1... (3 Replies)
Discussion started by: SkySmart
3 Replies

3. Shell Programming and Scripting

problem with awk script

Hi, I have two files Hi, I have two files file1 :> Code: val="10" port="localhost:8080"httpadd="http:\\192.168.0.239" file2 :> Code: val=${val} val="pdssx" port=${port}port="1324"httpadd=${httpadd}httpadd="raamraav"fileloc=${fileloc} file3(or file2) should have following... (1 Reply)
Discussion started by: nitin.pathak
1 Replies

4. Shell Programming and Scripting

Awk script Problem

Hi , I am having two files FILE1 and FILE2 as shown below I need to search each and every element of Coulumn1 in the FILE1 in FILE2 and Globally replace with the Corresponding element of the Column2 in the FILE2 , For example and1 which is the first element of COl 1 of the FILE1 should be... (4 Replies)
Discussion started by: jaita
4 Replies

5. Shell Programming and Scripting

Problem with awk script

Hi, I have one csv file with 3 fileds like tmp1.csv 2079|2010Aug|cardilogy 2349|2010Aug|numerology 2213|2010Aug|immunlogy another csv file with code for those specialities spec.csv cardiology|CRD numerology|NMY immunology|IMY i want to replace the contents of file 1 with codes... (2 Replies)
Discussion started by: Man83Nagesh
2 Replies

6. Shell Programming and Scripting

Problem with an awk Script

hello, first, yes i searched the forum , google and read many tutorials but still have a problem with my script. I have great Problems, because i haven't worked with regular expressions before and never had anything to do with shellscripts. i am a complete Newby in this sort of theme. I have... (8 Replies)
Discussion started by: Crashvogel
8 Replies

7. Shell Programming and Scripting

awk script problem

Hi All, I have the following input data: That I'd like to look like this ($2 is the column I'd like it to appear in) where the entries are grouped by date: The code I have at present is: awk 'BEGIN {} { dt = $1 if (dt == dt_prev) { pp = $3 ... (7 Replies)
Discussion started by: pondlife
7 Replies

8. Shell Programming and Scripting

Problem with a AWK Script

Hi I am having some contents in my file like this file1 ########################## pin (PIN1) { direction : input ; capacitance : 121 ; max_transition : 231 ; } pin (PIN2) { direction : input ; capacitance : 124 ; max_transition : 421 ;... (8 Replies)
Discussion started by: kshitij
8 Replies

9. Shell Programming and Scripting

Problem with awk script

Hi Can anyone help me in this Problem File1 ######################### HOLI 123 AND ONE TWO THREE AMITABH SAMSUNG POLI AND TWO SENSE CRYING WING PPIN TBFLAG I B AND OROLE TB_HOT=" DCT" TB_CAT=" CAT" TC_NOT=" AND" +PIN TB=" HOT" TB_GATE=" KOT" TB_LATE=" MAT" TC=LOT MAT DAT SAT... (5 Replies)
Discussion started by: kshitij
5 Replies

10. Shell Programming and Scripting

Problem with one awk script

Hi , I am having a file having the contents like this file1 ##################### kite kshitij jolly admire in the wing and tell me the secret behind opus 123 and the right of the track ######################### I have to write one awk script to substitue some values with other... (6 Replies)
Discussion started by: kshitij
6 Replies
Login or Register to Ask a Question