Link to home
Start Free TrialLog in
Avatar of NAEDI2
NAEDI2Flag for United States of America

asked on

AWK or Perl or something else?

I want to modify the attached file with a script using AWK or PERL or any utility that might be better.  I am trying to make two changes to the input file which is attached.

1) When I reach a group of FST records like this block of the file here, I need to remove the second occurrence of the SDP*Y*Y record so all the FST records are in a continuous block.

So change this:

SDP*Y*Y
FST*32400*D*D*101014**002*1321
FST*32400*D*D*101025**002*0000
FST*32400*D*D*101105**002*0000
SDP*Y*Y                                                            << lose this record on output
FST*64800*D*F*101108*101130
FST*64800*D*F*101201*101231
FST*129600*D*F*110101*110131

To this:

SDP*Y*Y
FST*32400*D*D*101014**002*1321
FST*32400*D*D*101025**002*0000
FST*32400*D*D*101105**002*0000
FST*64800*D*F*101108*101130
FST*64800*D*F*101201*101231
FST*129600*D*F*110101*110131

2) Other places in the file we have the SDP*B*F record.  Here I need to replicate this SDP*B*F so it occurs in front of each FST record with a DO*SA99999 on the end.  

So change this:

SDP*B*F
FST*4160*C*D*101012**002*1321*DO*SA23093
SDP*B*F
FST*4160*Z*D*101007**002*1321*DO*SA07184
FST*4160*Z*D*101011**002*1321*DO*SA17617
FST*4160*Z*D*101011**002*1321*DO*SA17618
FST*4160*Z*D*101011**002*1321*DO*SA17619

To this:

SDP*B*F
FST*4160*C*D*101012**002*1321*DO*SA23093
SDP*B*F
FST*4160*Z*D*101007**002*1321*DO*SA07184
SDP*B*F
FST*4160*Z*D*101011**002*1321*DO*SA17617
SDP*B*F
FST*4160*Z*D*101011**002*1321*DO*SA17618
SDP*B*F
FST*4160*Z*D*101011**002*1321*DO*SA17619


Is AWK or Perl the way to go here?
Avatar of farzanj
farzanj
Flag of Canada image

1)
Try this:
perl -ne 'print if ($. == 1 || ( $. > 1 && $_ !~ /SDP\*Y\*Y/))' filename

Open in new window


2)
perl -ne 'if(/DO\*SA\d{5}/){print "SDP*B*F\n$_"} elsif($_!~/SDP\*B\*F/) {print}' filename

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
perl -nle 'print "SDP*B*F" if /DO\*SA\d+$/; print unless /SDP\*B\*F/ || /SDP\*Y\*Y/&&$n++'   filename
Avatar of NAEDI2

ASKER

This works perfectly, thank you very much.
Avatar of Bill Prew
Bill Prew

Great, glad that was helpful, thanks for the feedback.

~bp