Visit Our UNIX and Linux User Community


Search between pattrens with one string in one or other line........


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search between pattrens with one string in one or other line........
# 1  
Old 06-06-2007
Search between pattrens with one string in one or other line........

Hi have a lots of files with this type of code

Example--1
PROCEDURE DIVISION USING

AAA
BBB
CCC.

Example--2

PROCEDURE DIVISION
Some Commented Lines....
USING

AAA
BBB
CCC.

Example--3
PROCEDURE DIVISION
USING

AAA
BBB
CCC
.


Basically the word USING can be in same line as PROCDURE DIVISION or on next line or on the next after a commnet

I want to count no.of arguments between PROCEDURE DIVISION followed by USING (in the same line or next line) and "." (same thing over here "." can be in the same line as last argument or in a new line)

I wanted to find the no.of arguments each file uses...
# 2  
Old 06-06-2007
something to start with:
Code:
nawk -f pb.awk myFile

pb.awk:
BEGIN {
  FS=RS=""
}
$1 ~ "^PROCEDURE DIVISION" {
  block=1
  next
}
block {
  block=0
  n=split($0,a)
  printf("FILENAME->[%s] n=%d\n", FILENAME, a[n]!="."?n:n-1)
}

# 3  
Old 06-06-2007
Code:
# !/opt/third-party/bin/perl

open(FILE, "<", "p");

while(<FILE>) {
  if( /using/i ) {
    $start = 1;
    next;
  }
  if ( /\./ ) {
    $start = 0;
    $cnt++ if( !/^\./ );
  }
  next if( /^$/ );
  $cnt++ if( $start == 1 );
}

close(FILE);

print "count: $cnt\n";

exit 0

# 4  
Old 06-06-2007
Try the following awk program :
Code:
#!/usr/bin/awk -f
# Awk File : cbl_args.awk

function print_args_count() {
   if (file) print file," #Args=" args_count;
}

FNR==1  {
   print_args_count();
   file         = FILENAME;
   proceed_args = 0;
   args_count   = 0;
}

/^[[:space:]]*\*/ {
   next; # skip comment
}

/PROCEDURE DIVISION/,/\./ {
   if ($0 ~ /USING/) {
      proceed_args=1;
      next;
   }
   if ($0 ~ /^[[:space:]]*\./) next;
   if (proceed_args && NF>0) ++args_count;
}

END {
   print_args_count();
}

Input Files :
Code:
$ cat pgm0.cbl
*
PROCEDURE DIVISION
.
*
$ cat pgm1.cbl
*
PROCEDURE DIVISION USING

CCC.
*
$ cat pgm2.cbl
*
PROCEDURE DIVISION 
* Some Commented Lines....
USING

BBB
CCC.
*
$ cat pgm3.cbl
*
PROCEDURE DIVISION 
USING

AAA
BBB
CCC
.
*
$

Output:
Code:
$ awk -f cbl_args.awk *.cbl
pgm0.cbl  #Args=0
pgm1.cbl  #Args=1
pgm2.cbl  #Args=2
pgm3.cbl  #Args=3
$

Jean-Pierre.
# 5  
Old 06-06-2007
Jean-Pierre that was AWESOME....

I would be very very thankfull if u can take the pain of explaining the code...


thanks in advance.....
# 6  
Old 06-06-2007
Sorry

but small change ..

there is also a case where we have

PROCEDURE DIVISION USING AAA BBB. (In this case it Fails)

And One more....

after USING we have Comments in few cases... like

PROCEDURE DIVISION
Some Comments...
USING
Some Comments
AAA
BBB
.

For the Second case i have added a line (May be wrong)


/PROCEDURE DIVISION/,/\./ {
if ($0 ~ /USING/) {
proceed_args=1;
next;
}
if ($0 ~ /^[[:space:]]*\./) next;
if ($0 ~ /^[[:space:]]*\*/) next; (Thought it will skip for "*")
if (proceed_args && NF>0) ++args_count;
}
# 7  
Old 06-06-2007
Quote:
For the Second case i have added a line (May be wrong)

/PROCEDURE DIVISION/,/\./ {
if ($0 ~ /USING/) {
proceed_args=1;
next;
}
if ($0 ~ /^[[:space:]]*\./) next;
if ($0 ~ /^[[:space:]]*\*/) next; (Thought it will skip for "*")
if (proceed_args && NF>0) ++args_count;
}
Comments are already ignored.


Quote:
Sorry

but small change ..

there is also a case where we have

PROCEDURE DIVISION USING AAA BBB. (In this case it Fails)
I have rewritten the central part of the awk program to handle all cases (i hope) :
Code:
#!/usr/bin/awk -f
# Awk File : cbl_args.awk

function print_args_count() {
   if (file) print file," #Args=" args_count;
}

FNR==1  {
   print_args_count();
   file         = FILENAME;
   proceed_args = 0;
   args_count   = 0;
}

/^[[:space:]]*\*/ {
   next; # skip comment
}

/PROCEDURE DIVISION/,/\./ {
   count = NF;
   if (/PROCEDURE DIVISION/) count -= 2;
   if (/USING/) {
      proceed_args=1;
      --count;
   }
   if (NF == ".") --count;
   if (proceed_args) args_count += count;
}

END {
   print_args_count();
}

and add some modifications to my input files and create new ones :
Code:
$ cat pgm3.cbl
*
PROCEDURE DIVISION
USING

AAA BBB
CCC
.
*

$ cat pgm4.cbl
PROCEDURE DIVISION USING AAA BBB CCC DDD.
$ cat pgm5.cbl

PROCEDURE DIVISION USING
    AAA
    BBB
    CCC
    DDD
    EEE. 
$

The output :
Code:
$ awk -f cbl_args.awk pgm*.cbl
pgm0.cbl  #Args=0
pgm1.cbl  #Args=1
pgm2.cbl  #Args=2
pgm3.cbl  #Args=4
pgm4.cbl  #Args=4
pgm5.cbl  #Args=5
$

Jean-Pierre.

Previous Thread | Next Thread
Test Your Knowledge in Computers #326
Difficulty: Medium
Time on Computers is commonly referred to as GNU Time.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Search for a string,delete the line and replace with new string in a file

Hi Everyone, I have a requirement in ksh where i have a set of files in a directory. I need to search each and every file if a particular string is present in the file, delete that line and replace that line with another string expression in the same file. I am very new to unix. Kindly help... (10 Replies)
Discussion started by: Pradhikshan
10 Replies

2. Shell Programming and Scripting

Search string within a file and list common words from the line having the search string

Hi, Need your help for this scripting issue I have. I am not really good at this, so seeking your help. I have a file looking similar to this: Hello, i am human and name=ABCD. How are you? Hello, i am human and name=PQRS. I am good. Hello, i am human and name=ABCD. Good bye. Hello, i... (12 Replies)
Discussion started by: royzlife
12 Replies

3. Shell Programming and Scripting

Search a string in a text file and add another string at the end of line

Dear All I am having a text file which is having more than 200 lines. EX: 001010122 12000 BIB 12000 11200 1200003 001010122 2000 AND 12000 11200 1200003 001010122 12000 KVB 12000 11200 1200003 In the above file i want to search for string KVB... (5 Replies)
Discussion started by: suryanarayana
5 Replies

4. Shell Programming and Scripting

Search a string in a text file and add another string at the particular position of a line

I am having a text file which is having more than 200 lines. EX: 001010122 12000 BIB 12000 11200 1200003 001010122 2000 AND 12000 11200 1200003 001010122 12000 KVB 12000 11200 1200003 In the above file i want to search for string KVB and add/replace... (1 Reply)
Discussion started by: suryanarayana
1 Replies

5. Shell Programming and Scripting

Search several string and convert into a single line for each search string using awk command AIX?.

I need to search the file using strings "Request Type" , " Request Method" , "Response Type" and by using result set find the xml tags and convert into a single line?. below are the scenarios. Cat test Nov 10, 2012 5:17:53 AM INFO: Request Type Line 1.... (5 Replies)
Discussion started by: laknar
5 Replies

6. Shell Programming and Scripting

search a string in a particular column of file and return the line number of the line

Hi All, Can you please guide me to search a string in a particular column of file and return the line number of the line where it was found using awk. As an example : abc.txt 7000,john,2,1,0,1,6 7001,elen,2,2,0,1,7 7002,sami,2,3,0,1,6 7003,mike,1,4,0,2,1 8001,nike,1,5,0,1,8... (3 Replies)
Discussion started by: arunshankar.c
3 Replies

7. Shell Programming and Scripting

search string in a file and retrieve 10 lines including string line

Hi Guys, I am trying to write a perl script to search a string "Name" in the file "FILE" and also want to create a new file and push the searched string Name line along with 10 lines following the same. can anyone of you please let me know how to go about it ? (8 Replies)
Discussion started by: sukrish
8 Replies

8. Shell Programming and Scripting

Perl: Search for string on line then search and replace text

Hi All, I have a file that I need to be able to find a pattern match on a line, search that line for a text pattern, and replace that text. An example of 4 lines in my file is: 1. MatchText_randomNumberOfText moreData ReplaceMe moreData 2. MatchText_randomNumberOfText moreData moreData... (4 Replies)
Discussion started by: Crypto
4 Replies

9. Shell Programming and Scripting

Search and insert between Pattrens...

Hi Every One... I wanted to inserted a line in between matched pattrens.. Ex... InPut File.. WRITEQ TS ************************** aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbb cccccccccccccccccccccccc SOME PATTREN's RESP ( WS-RESP ) ... (7 Replies)
Discussion started by: pbsrinivas
7 Replies

10. Shell Programming and Scripting

Search between pattrens.

i wanted to search between pattrens so i used awk /"EXEC CICS DELETEQ TS"/,/END-IF/ but the some change is AAAAAAAA EXEC CICS DELETEQ TS IF ..... END-IF.... XXXXXx XXX IF (3 Replies)
Discussion started by: pbsrinivas
3 Replies

Featured Tech Videos