tags:

views:

219

answers:

2

I'm stil pretty new to regular expression and just started learning to use awk. What I am trying to accomplish is writing a ksh script to read-in lines from text, and and for every lines that match the following:

*RECORD 0000001 [some_serial_#]

to replace $2 (i.e. 000001) with a different number. So essentially the script read in batch record dump, and replace the record number with date+record#, and write to separate file.

So this is what I'm thinking the format should be:

awk 'match($0,"/*RECORD")!=0{$2="$DATE-n++"; print $0} match($0,"/*RECORD")==0{print $0}' $BATCH > $OUTPUT

but obviously "/*RECORD" is not going to work, and I'm not sure if changing $2 and then write the whole line is the correct way to do this. So I am in need of some serious enlightenment.

A: 

So you want your example line to look like

*RECORD $DATE-n++ [some_serial_#]

after awk's done with it?

awk '{ if (match($0, "*RECORD") != 0) { $2="$DATE-n++"; }; print }' $BATCH > $OUTPUT

Based on your update, it looks like you instead expect $DATE to be an environment variable which is used in the awk expression and n is a variable in the awk script that keeps count of how many records matched the pattern. Given that, this may look more like what you want.

$ cat script.awk
BEGIN { n=0 }
{
    if (match($0, "\*RECORD") != 0) {
        n++;
        $2 = (ENVIRON["DATE"] "-" n);
    }
    print;
}

$ awk -f script.awk $BATCH > $OUTPUT
jamessan
I must've not getting it quite right... I got this erroutput"awk: 0602-521 There is a regular expression error./n ?*+ not preceded by valid expression./n/n The source line number is 1./n The error context is/n { if (match($0, >>> "*FTR") <<</n
Ken Chen
Then your awk needs the `*` to be escaped -- `match($0 "\\*RECORD")`. Mine was the opposite and gave a warning that I was needlessly escaping the `*`.
jamessan
No matter how I tried I just can't get it right: The error context is { if (match($0, >>> "\*RECORD") <<<
Ken Chen
In the end I discarded match() and used binary conditional for the matching.... match() would not let me escape the Asterisk right... awk '{if ($1 == "\*RECORD"){$2="$DATE-$SEQ"};{print $0}}'
Ken Chen
A: 

use equality.

D=$(date +%Y%m%d)
awk -vdate="$D" '
{
  for(i=1;i<=NF;i++){
   if ( $i == "*RECORD" ){
      $(i+1) = date"00002"
      break # break after searching for one record, otherwise, remove break
   }
  }
}1' file
ghostdog74
thanks for the suggestion, after a few tweaks on my own, I finally got it to do what I needed!
Ken Chen