How can I add some text after some sequence of text if this sequence matches certain criteria using Shell script?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite












I want to add a statement just after the text matching the conditions mentioned in a shell script,



Below is my sample File(SQL File) :



begin
AFFECTED_ROWS := 0;

UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;

DELETE FROM table_name
WHERE condition;

INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);

MERGE INTO employees e
USING hr_records h
ON (e.id = h.emp_id)
WHEN MATCHED THEN
UPDATE SET e.address = h.address
WHEN NOT MATCHED THEN
INSERT (id, address)
VALUES (h.emp_id, h.address);
end;


I will take this file and perform below transformation,
Once I see any of below Text in sequence



1: "UPDATE ... SET ...;"
2: "DELETE ... FROM ...;"
3: "INSERT ... INTO ...;"
4: "MERGE ... INTO ... [WHEN MATCHED THEN | WHEN NOT MATCHED] ... [UPDATE|INSERT|DELETE] ... ;"


I need to add 1 extra row just after the semicolon



AFFECTED_ROWS := AFFECTED_ROWS + SQL%ROWCOUNT;


So My new file will look like something below



begin
AFFECTED_ROWS := 0;

UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
AFFECTED_ROWS := AFFECTED_ROWS + SQL%ROWCOUNT;

DELETE FROM table_name
WHERE condition;
AFFECTED_ROWS := AFFECTED_ROWS + SQL%ROWCOUNT;

INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);
AFFECTED_ROWS := AFFECTED_ROWS + SQL%ROWCOUNT;

MERGE INTO employees e
USING hr_records h
ON (e.id = h.emp_id)
WHEN MATCHED THEN
UPDATE SET e.address = h.address
WHEN NOT MATCHED THEN
INSERT (id, address)
VALUES (h.emp_id, h.address);
AFFECTED_ROWS := AFFECTED_ROWS + SQL%ROWCOUNT;

end;


I tried finding and implementing the approach in the scope of PLSQL but didn't find any generic way possible to get no of rows affected, so I thought
of text parsing but I don't know much about awk or sed.



For now what I was trying to do something like below:



sed '/Patterns Go Here/a AFFECTED_ROWS := AFFECTED_ROWS + SQL%ROWCOUNT;' temp.sql



So pattern may have conditions mentioned above









share







New contributor




Hargun Suri is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.























    up vote
    0
    down vote

    favorite












    I want to add a statement just after the text matching the conditions mentioned in a shell script,



    Below is my sample File(SQL File) :



    begin
    AFFECTED_ROWS := 0;

    UPDATE table_name
    SET column1 = value1, column2 = value2, ...
    WHERE condition;

    DELETE FROM table_name
    WHERE condition;

    INSERT INTO table_name (column1, column2, column3, ...)
    VALUES (value1, value2, value3, ...);

    MERGE INTO employees e
    USING hr_records h
    ON (e.id = h.emp_id)
    WHEN MATCHED THEN
    UPDATE SET e.address = h.address
    WHEN NOT MATCHED THEN
    INSERT (id, address)
    VALUES (h.emp_id, h.address);
    end;


    I will take this file and perform below transformation,
    Once I see any of below Text in sequence



    1: "UPDATE ... SET ...;"
    2: "DELETE ... FROM ...;"
    3: "INSERT ... INTO ...;"
    4: "MERGE ... INTO ... [WHEN MATCHED THEN | WHEN NOT MATCHED] ... [UPDATE|INSERT|DELETE] ... ;"


    I need to add 1 extra row just after the semicolon



    AFFECTED_ROWS := AFFECTED_ROWS + SQL%ROWCOUNT;


    So My new file will look like something below



    begin
    AFFECTED_ROWS := 0;

    UPDATE table_name
    SET column1 = value1, column2 = value2, ...
    WHERE condition;
    AFFECTED_ROWS := AFFECTED_ROWS + SQL%ROWCOUNT;

    DELETE FROM table_name
    WHERE condition;
    AFFECTED_ROWS := AFFECTED_ROWS + SQL%ROWCOUNT;

    INSERT INTO table_name (column1, column2, column3, ...)
    VALUES (value1, value2, value3, ...);
    AFFECTED_ROWS := AFFECTED_ROWS + SQL%ROWCOUNT;

    MERGE INTO employees e
    USING hr_records h
    ON (e.id = h.emp_id)
    WHEN MATCHED THEN
    UPDATE SET e.address = h.address
    WHEN NOT MATCHED THEN
    INSERT (id, address)
    VALUES (h.emp_id, h.address);
    AFFECTED_ROWS := AFFECTED_ROWS + SQL%ROWCOUNT;

    end;


    I tried finding and implementing the approach in the scope of PLSQL but didn't find any generic way possible to get no of rows affected, so I thought
    of text parsing but I don't know much about awk or sed.



    For now what I was trying to do something like below:



    sed '/Patterns Go Here/a AFFECTED_ROWS := AFFECTED_ROWS + SQL%ROWCOUNT;' temp.sql



    So pattern may have conditions mentioned above









    share







    New contributor




    Hargun Suri is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.





















      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I want to add a statement just after the text matching the conditions mentioned in a shell script,



      Below is my sample File(SQL File) :



      begin
      AFFECTED_ROWS := 0;

      UPDATE table_name
      SET column1 = value1, column2 = value2, ...
      WHERE condition;

      DELETE FROM table_name
      WHERE condition;

      INSERT INTO table_name (column1, column2, column3, ...)
      VALUES (value1, value2, value3, ...);

      MERGE INTO employees e
      USING hr_records h
      ON (e.id = h.emp_id)
      WHEN MATCHED THEN
      UPDATE SET e.address = h.address
      WHEN NOT MATCHED THEN
      INSERT (id, address)
      VALUES (h.emp_id, h.address);
      end;


      I will take this file and perform below transformation,
      Once I see any of below Text in sequence



      1: "UPDATE ... SET ...;"
      2: "DELETE ... FROM ...;"
      3: "INSERT ... INTO ...;"
      4: "MERGE ... INTO ... [WHEN MATCHED THEN | WHEN NOT MATCHED] ... [UPDATE|INSERT|DELETE] ... ;"


      I need to add 1 extra row just after the semicolon



      AFFECTED_ROWS := AFFECTED_ROWS + SQL%ROWCOUNT;


      So My new file will look like something below



      begin
      AFFECTED_ROWS := 0;

      UPDATE table_name
      SET column1 = value1, column2 = value2, ...
      WHERE condition;
      AFFECTED_ROWS := AFFECTED_ROWS + SQL%ROWCOUNT;

      DELETE FROM table_name
      WHERE condition;
      AFFECTED_ROWS := AFFECTED_ROWS + SQL%ROWCOUNT;

      INSERT INTO table_name (column1, column2, column3, ...)
      VALUES (value1, value2, value3, ...);
      AFFECTED_ROWS := AFFECTED_ROWS + SQL%ROWCOUNT;

      MERGE INTO employees e
      USING hr_records h
      ON (e.id = h.emp_id)
      WHEN MATCHED THEN
      UPDATE SET e.address = h.address
      WHEN NOT MATCHED THEN
      INSERT (id, address)
      VALUES (h.emp_id, h.address);
      AFFECTED_ROWS := AFFECTED_ROWS + SQL%ROWCOUNT;

      end;


      I tried finding and implementing the approach in the scope of PLSQL but didn't find any generic way possible to get no of rows affected, so I thought
      of text parsing but I don't know much about awk or sed.



      For now what I was trying to do something like below:



      sed '/Patterns Go Here/a AFFECTED_ROWS := AFFECTED_ROWS + SQL%ROWCOUNT;' temp.sql



      So pattern may have conditions mentioned above









      share







      New contributor




      Hargun Suri is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      I want to add a statement just after the text matching the conditions mentioned in a shell script,



      Below is my sample File(SQL File) :



      begin
      AFFECTED_ROWS := 0;

      UPDATE table_name
      SET column1 = value1, column2 = value2, ...
      WHERE condition;

      DELETE FROM table_name
      WHERE condition;

      INSERT INTO table_name (column1, column2, column3, ...)
      VALUES (value1, value2, value3, ...);

      MERGE INTO employees e
      USING hr_records h
      ON (e.id = h.emp_id)
      WHEN MATCHED THEN
      UPDATE SET e.address = h.address
      WHEN NOT MATCHED THEN
      INSERT (id, address)
      VALUES (h.emp_id, h.address);
      end;


      I will take this file and perform below transformation,
      Once I see any of below Text in sequence



      1: "UPDATE ... SET ...;"
      2: "DELETE ... FROM ...;"
      3: "INSERT ... INTO ...;"
      4: "MERGE ... INTO ... [WHEN MATCHED THEN | WHEN NOT MATCHED] ... [UPDATE|INSERT|DELETE] ... ;"


      I need to add 1 extra row just after the semicolon



      AFFECTED_ROWS := AFFECTED_ROWS + SQL%ROWCOUNT;


      So My new file will look like something below



      begin
      AFFECTED_ROWS := 0;

      UPDATE table_name
      SET column1 = value1, column2 = value2, ...
      WHERE condition;
      AFFECTED_ROWS := AFFECTED_ROWS + SQL%ROWCOUNT;

      DELETE FROM table_name
      WHERE condition;
      AFFECTED_ROWS := AFFECTED_ROWS + SQL%ROWCOUNT;

      INSERT INTO table_name (column1, column2, column3, ...)
      VALUES (value1, value2, value3, ...);
      AFFECTED_ROWS := AFFECTED_ROWS + SQL%ROWCOUNT;

      MERGE INTO employees e
      USING hr_records h
      ON (e.id = h.emp_id)
      WHEN MATCHED THEN
      UPDATE SET e.address = h.address
      WHEN NOT MATCHED THEN
      INSERT (id, address)
      VALUES (h.emp_id, h.address);
      AFFECTED_ROWS := AFFECTED_ROWS + SQL%ROWCOUNT;

      end;


      I tried finding and implementing the approach in the scope of PLSQL but didn't find any generic way possible to get no of rows affected, so I thought
      of text parsing but I don't know much about awk or sed.



      For now what I was trying to do something like below:



      sed '/Patterns Go Here/a AFFECTED_ROWS := AFFECTED_ROWS + SQL%ROWCOUNT;' temp.sql



      So pattern may have conditions mentioned above







      shell-script shell awk grep





      share







      New contributor




      Hargun Suri is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.










      share







      New contributor




      Hargun Suri is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.








      share



      share






      New contributor




      Hargun Suri is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 3 mins ago









      Hargun Suri

      1




      1




      New contributor




      Hargun Suri is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Hargun Suri is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Hargun Suri is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.

























          active

          oldest

          votes











          Your Answer







          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "106"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );






          Hargun Suri is a new contributor. Be nice, and check out our Code of Conduct.









           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f477631%2fhow-can-i-add-some-text-after-some-sequence-of-text-if-this-sequence-matches-cer%23new-answer', 'question_page');

          );

          Post as a guest



































          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          Hargun Suri is a new contributor. Be nice, and check out our Code of Conduct.









           

          draft saved


          draft discarded


















          Hargun Suri is a new contributor. Be nice, and check out our Code of Conduct.












          Hargun Suri is a new contributor. Be nice, and check out our Code of Conduct.











          Hargun Suri is a new contributor. Be nice, and check out our Code of Conduct.













           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f477631%2fhow-can-i-add-some-text-after-some-sequence-of-text-if-this-sequence-matches-cer%23new-answer', 'question_page');

          );

          Post as a guest













































































          Popular posts from this blog

          How to check contact read email or not when send email to Individual?

          Displaying single band from multi-band raster using QGIS

          How many registers does an x86_64 CPU actually have?