Page 1 of 1

Insert a line in PS File after some specific Keyword: REXX

PostPosted: Mon Aug 12, 2019 11:52 pm
by anshul7jain
Hi All,

I need to insert a line(Say: "New Message here") after specific line (Say: "After this line") in a PS File(Say: "APP.D1111.FILE") using REXX

For Eg:
Contents of APP.D1111.FILE

First Line
After this line
Second Line
Third Line
After this line
Fourth Line

Output Needed:
First Line
After this line
New Message here
Second Line
Third Line
After this line
New Message here
Fourth Line

Thanks for help in Advance

~Anshul

Re: Insert a line in PS File after some specific Keyword: RE

PostPosted: Tue Aug 13, 2019 1:31 am
by willy jensen
something like:
"execio * diskr ddname (finis)"  /* stack the dataset contents */
qn=queued()
do qn      /* process stacked data */
  parse pull r
  queue r
  if pos('whatever you are looking for',r)>0 then queue ' inserted data'
end
"execio" queued() "diskw ddname (finis)"  /* rewrite dataset */

If you need to allocate and free the dataset then I suggest that you look at the BPXWDYN command (preferred) or the ALLOC / FREE command set.

Re: Insert a line in PS File after some specific Keyword: RE

PostPosted: Tue Aug 13, 2019 1:57 am
by anshul7jain
Thanks a Lot Willy, it worked. I submitted the program three times as it was erroring out due to free command. Now i see three 'Inserted data' lines are inserted. Is this because it got queued 3 times? How can we clear the queued() if this is the case.

Re: Insert a line in PS File after some specific Keyword: RE

PostPosted: Tue Aug 13, 2019 2:12 am
by willy jensen
If you run it 3 times,then of course you will get 3 inserts as you haven't removed the trigger line.
But you raise a good point, which I should have mentioned.
I find it advisable to always delete the stack at the top of my program (command "delstack") if I know that I are going to create a stack. This prevents processing some leftover stack entries. Or you can use the "NEWSTACK" command at the top with a "DELSTACK" at the end. I suggest that you read up on stacks in the manual.

Re: Insert a line in PS File after some specific Keyword: RE

PostPosted: Tue Aug 13, 2019 6:35 pm
by sergeyken
willy jensen wrote:I find it advisable to always delete the stack at the top of my program (command "delstack") if I know that I are going to create a stack. This prevents processing some leftover stack entries. Or you can use the "NEWSTACK" command at the top with a "DELSTACK" at the end. I suggest that you read up on stacks in the manual.

Using "DELSTACK" at the beginning is not very good idea: we could accidentally eliminate some data created by the "outer" program for its own purpose.
Correct and robust way is #2 mentioned: enclose your own code supposed to use the stack in the pair of commands: "NEWSTACK"-----"DELSTACK"
It can be considered as "creation of the private stack", without disturbing possible other code outside of your program, which is using the stack too.

Re: Insert a line in PS File after some specific Keyword: RE

PostPosted: Tue Aug 13, 2019 9:45 pm
by willy jensen
Well, if your pgm issues the "newstack" and then terminates prematurely before doing "delstack", then any 'outside' pgm will work with your newly created stack, though the "qstack" command might help there. All I'm saying is be carefull. Personally I try to avoid using stacks, but a a stack did make sense in this particular case.

Re: Insert a line in PS File after some specific Keyword: RE

PostPosted: Wed Aug 14, 2019 12:21 am
by sergeyken
willy jensen wrote:Well, if your pgm issues the "newstack" and then terminates prematurely before doing "delstack", then any 'outside' pgm will work with your newly created stack,

This may happen only in two cases:
    1) the program has been wrongly designed, and may EXIT in the middle of its code (the same effect as GOTO within spaghetti-code). -- Need to be re-designed.
    2) the program failed abnormally. -- If so it doesn't matter too much if the stack is empty, or not; it is disaster to be investigated anyway.

Using "NEWSTACK"---"DELSTACK" prevents unexpected results during normal run; in general we don't know what the calling program is doing outside of the scope of this specific part of code.