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



IBM's Command List programming language & Restructured Extended Executor

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

Postby anshul7jain » Mon Aug 12, 2019 11:52 pm

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
anshul7jain
 
Posts: 2
Joined: Wed Aug 07, 2019 7:48 am
Has thanked: 3 times
Been thanked: 0 time

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

Postby willy jensen » Tue Aug 13, 2019 1:31 am

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.

These users thanked the author willy jensen for the post:
anshul7jain (Tue Aug 13, 2019 7:35 pm)
willy jensen
 
Posts: 465
Joined: Thu Mar 10, 2016 5:03 pm
Has thanked: 0 time
Been thanked: 70 times

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

Postby anshul7jain » Tue Aug 13, 2019 1:57 am

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.
anshul7jain
 
Posts: 2
Joined: Wed Aug 07, 2019 7:48 am
Has thanked: 3 times
Been thanked: 0 time

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

Postby willy jensen » Tue Aug 13, 2019 2:12 am

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.

These users thanked the author willy jensen for the post:
anshul7jain (Tue Aug 13, 2019 7:35 pm)
willy jensen
 
Posts: 465
Joined: Thu Mar 10, 2016 5:03 pm
Has thanked: 0 time
Been thanked: 70 times

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

Postby sergeyken » Tue Aug 13, 2019 6:35 pm

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.
Javas and Pythons come and go, but JCL and SORT stay forever.

These users thanked the author sergeyken for the post:
anshul7jain (Tue Aug 13, 2019 7:35 pm)
User avatar
sergeyken
 
Posts: 437
Joined: Wed Jul 24, 2019 10:12 pm
Has thanked: 7 times
Been thanked: 40 times

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

Postby willy jensen » Tue Aug 13, 2019 9:45 pm

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.
willy jensen
 
Posts: 465
Joined: Thu Mar 10, 2016 5:03 pm
Has thanked: 0 time
Been thanked: 70 times

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

Postby sergeyken » Wed Aug 14, 2019 12:21 am

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.
Javas and Pythons come and go, but JCL and SORT stay forever.
User avatar
sergeyken
 
Posts: 437
Joined: Wed Jul 24, 2019 10:12 pm
Has thanked: 7 times
Been thanked: 40 times


Return to CLIST & REXX

 


  • Related topics
    Replies
    Views
    Last post