awk pattern match and search in single statement


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk pattern match and search in single statement
# 1  
Old 06-10-2010
Java awk pattern match and search in single statement

Hi All,
I am trying to alter all lines between EXEC SQL and END-EXEC that have an INCLUDE in them.

The following code
Code:
search="INCLUDE "
  cp -f ${WORK}/$file.in ${WORK}/$file.wrk2
  for item in `echo $search `; do
      >  ${WORK}/$file.wrk1
      awk  -vITEM="$item" '{
          if ( $0 ~ ITEM ) {
             savetext = substr($0,1,80);
             col7 = substr($0,7,1);
             print "MIGAU1" col7 "       BEGIN DECLARE SECTION"
             print "MIGAU1" col7 "    END-EXEC."
             print "MIGAU1" col7 "    EXEC SQL"
             print savetext
             print "MIGAU1" col7 "    END-EXEC."
             print "MIGAU1" col7 "    EXEC SQL"
             print "MIGAU1" col7 "       END DECLARE SECTION"
             }
         else { print $0 }
      }' ${WORK}/$file.wrk2 >> ${WORK}/$file.wrk1

works to alter the lines containing include but now I want to include the following pattern matching
Code:
awk '/EXEC SQL/,/END-EXEC/' filein

I have tried
Code:
search="INCLUDE "
  cp -f ${WORK}/$file.in ${WORK}/$file.wrk2
  for item in `echo $search `; do
      >  ${WORK}/$file.wrk1
      awk  '/EXEC SQL/,/END-EXEC/' || -vITEM="$item" '{
          if ( $0 ~ ITEM ) {
             savetext = substr($0,1,80);
             col7 = substr($0,7,1);
             print "MIGAU1" col7 "       BEGIN DECLARE SECTION"
             print "MIGAU1" col7 "    END-EXEC."
             print "MIGAU1" col7 "    EXEC SQL"
             print savetext
             print "MIGAU1" col7 "    END-EXEC."
             print "MIGAU1" col7 "    EXEC SQL"
             print "MIGAU1" col7 "       END DECLARE SECTION"
             }
         else { print $0 }
      }' ${WORK}/$file.wrk2 >> ${WORK}/$file.wrk1

and various other things but the script just hangs when I do this.

Any help appreciated.
# 2  
Old 06-10-2010
Please post sample input and an example of the desired output.
# 3  
Old 06-10-2010
Code:
      *               |            |TABLE.                                         |V02.05 *
      * 02022010 |    JHH   |R10.2 SR8644 DYNAMICALLY SET       |V02.06 *
      *               |            |ACTIVITY FLAGS                            |V02.06 *
      *               |            |IPARM AMENDED TO INCLUDE OFFSET|V02.06 *
      *****************************************************************
V01.02
V01.02 01  FILLER                     PIC X(32)
V01.02     VALUE 'MHAA5J - CM_COSTELEMENT_SPLIT'.
V01.02     EXEC SQL
V01.02        INCLUDE MHRSCV01
V01.02     END-EXEC.
V01.02
V01.02 01  FILLER                     PIC X(32)
V01.02     VALUE 'MHAA5J - CM_DIV_PROD_ACTIVITY'.
V01.02     EXEC SQL
V01.02        INCLUDE MHRDAV01
V01.02     END-EXEC.

The first include must not be altered but the EXEC SQL .... END_EXEC statements containing the INCLUDE should be altered (Result shown below):

Code:
      *               |            |TABLE.                                         |V02.05 *
      * 02022010 |    JHH   |R10.2 SR8644 DYNAMICALLY SET       |V02.06 *
      *               |            |ACTIVITY FLAGS                            |V02.06 *
      *               |            |IPARM AMENDED TO INCLUDE OFFSET|V02.06 *
      *****************************************************************V01.02
V01.02 01  FILLER                     PIC X(32)
V01.02     VALUE 'MHAA5J - CM_COSTELEMENT_SPLIT'.
MIGAU1    EXEC SQL
MIGAU1       BEGIN DECLARE SECTION
MIGAU1    END-EXEC       
V01.02     EXEC SQL
V01.02        INCLUDE MHRSCV01
V01.02     END-EXEC.
MIGAU1    EXEC SQL
MIGAU1       END DECLARE SECTION
MIGAU1    END-EXEC       
V01.02
V01.02 01  FILLER                     PIC X(32)
V01.02     VALUE 'MHAA5J - CM_DIV_PROD_ACTIVITY'.
MIGAU1    EXEC SQL
MIGAU1       BEGIN DECLARE SECTION
MIGAU1    END-EXEC       
V01.02     EXEC SQL
V01.02        INCLUDE MHRDAV01
V01.02     END-EXEC.
MIGAU1    EXEC SQL
MIGAU1       END DECLARE SECTION
MIGAU1    END-EXEC

# 4  
Old 06-10-2010
Use gawk, /usr/xpg4/bin/awk or nawk on Solaris:

Code:
awk 'BEGIN {
  b = "MIGAU1\tEXEC SQL\nMIGAU1\t\t"
  b = b  "BEGIN DECLARE SECTION\nMIGAU1\tEND-EXEC"
  e = "MIGAU1\tEXEC SQL\nMIGAU1\t\tEND DECLARE SECTION"
  e = e "\nMIGAU1\tEND-EXEC"  
  }
/EXEC SQL/ { print b }
666
/END-EXEC/ { print e }
' infile


Edit: Just saw the "containing include" part, work in progress ... Smilie

---------- Post updated at 05:21 PM ---------- Previous update was at 05:10 PM ----------

Use gawk or /usr/xpg4/bin/awk on Solaris:

Code:
awk 'BEGIN {
  b = "MIGAU1\tEXEC SQL\nMIGAU1\t\t"
  b = b  "BEGIN DECLARE SECTION\nMIGAU1\tEND-EXEC"
  e = "MIGAU1\tEXEC SQL\nMIGAU1\t\tEND DECLARE SECTION"
  e = e "\nMIGAU1\tEND-EXEC"  
  }
/EXEC SQL/, /END-EXEC/ { 
  r = r ? r RS $0 : $0
  /INCLUDE/ && ok ++
  if (/END-EXEC/) {
    if (ok) {
      print b RS r RS e
      r = ok = x
      }
  else {
    print r; r = x  
    }
  }	
  next	
  }-3' infile


Last edited by radoulov; 06-10-2010 at 12:40 PM..
This User Gave Thanks to radoulov For This Post:
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

awk pattern match by looping through search patterns

Hi I am using Solaris 5.10 & ksh Wanted to loop through a pattern file by reading it and passing it to the awk to match that value present in column 1 of rawdata.txt , if so print column 1 & 2 in to Avlblpatterns.txt. Using the following code but it seems some mistakes and it is running for... (2 Replies)
Discussion started by: ananan
2 Replies

3. Shell Programming and Scripting

Multiple pattern match and print the output in a single line

I need to match two patterns in a log file and need to get the next line of the one of the pattern (out of two patterns) that is matched, finally need to print these three values in a single line. Sample Log: 2013/06/11 14:29:04 <0999> (725102) Processing batch 02_1231324 2013/06/11... (4 Replies)
Discussion started by: rpm120
4 Replies

4. Shell Programming and Scripting

Search Pattern and combine into single file

Hi Experts Please help me out with the following thing: 2 files and want the output file: {No for using FOR loop because I got 22 million lines} Tried that "It processes only 8000 records per hour" I need a faster way out !!! FileA: 9051 9052 9053 9054 9055 9056 9057 9058 9059 ... (5 Replies)
Discussion started by: navkanwal
5 Replies

5. Shell Programming and Scripting

How to specify more then one pattern in a single mget statement of ftp command?

hi, i am using ftp command to get some files from a remote server. if the remote server contains files of different extension. abc.txt def.txt ghi.lst jkl.cnf is it possible to get all the three type of files in one ftp? i am using this ftp command $FTP $Remote_server <<_FTP1 ... (4 Replies)
Discussion started by: Little
4 Replies

6. 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

7. Shell Programming and Scripting

Need one liner to search pattern and print everything expect 6 lines from where pattern match made

i need to search for a pattern from a big file and print everything expect the next 6 lines from where the pattern match was made. (8 Replies)
Discussion started by: chidori
8 Replies

8. Shell Programming and Scripting

search files which doesnot match pattern ?

Hi I need a command to search files in a directory which does not match with pattern .. Plz send me this Ex : Test is directory and has some 10 files with different name all are with *.dat extension , need to search files which doesnot contain word "Dummy file". Thanks (6 Replies)
Discussion started by: madankumar
6 Replies

9. UNIX for Dummies Questions & Answers

List files that do not match the search pattern

I need to list the files that do not match the search pattern: Example: cat file1 This is how it should work cat file2 This is why I like Unix grep -option? Unix * (or some other command) returns file1 (7 Replies)
Discussion started by: olapxpert
7 Replies

10. IP Networking

List files that do not match the search pattern

I need to list the files that do not match the search pattern: Example: cat file1 This is how it should work cat file2 This is why I like Unix grep -option? Unix * (or some other command) returns file1 (1 Reply)
Discussion started by: olapxpert
1 Replies
Login or Register to Ask a Question