how to add opening and closing brackets in the condition using unix shell script










1















I have conditions in a file, that has around 10 thousend lines. As shown below.



COLUMN_NAME NOT IN 1234534
COLUMN_NAME1 NOT IN 34252
COLUMN_NAME_2 not in 67496575
COLUMN_NAME NOT in 1234534
foo COLUMN_NAME NOT IN (1234534,453535) rest of the line
COLUMN_NAME NOT IN 1234534,453535
columnsd not in (23123124232,6464777) rest on the line
COLUMN_NAME NOT IN 1234534
COLUMN_NAME NOT IN 1234534
fdfsdf COLUMN_NAME not in 1234534
COLUMN_NAME not in 1234534
column NOT IN (6764577,434545)
COLUMN_NAME not in (1234534)


I want to use the sed command to add the brackets after the IN clause. I want to replace the conditions above result in output like below.



COLUMN_NAME NOT IN (1234534)
COLUMN_NAME1 NOT IN (3422)
COLUMN_NAME_2 not in (67496575)
COLUMN_NAME NOT in (1234534)
COLUMN_NAME NOT IN (1234534,453535) rest of the line
COLUMN_NAME NOT IN (1234534,453535)
columnsd not in (23123124232,6464777) rest on the line
COLUMN_NAME NOT IN (1234534)
COLUMN_NAME NOT IN (1234534)
fdfsdf COLUMN_NAME not in (1234534)
COLUMN_NAME not in (1234534)
column NOT IN (6764577,434545)
COLUMN_NAME not in (1234534)









share|improve this question
























  • sed -i.bak 's/(sins*)([0-9]+)/1(2)/' filename

    – Samuel Kirschner
    Nov 15 '18 at 16:51











  • @SamuelKirschner sir its not working for case COLUMN_NAME NOT IN 1234534 and also could you please give me some link reference so I can learn

    – ram
    Nov 15 '18 at 17:11












  • @oguzismail awk also adding () where it is already there like COLUMN_NAME NOT IN ((1234534,453535)) and also after the condition also we have some constant Apologies mixed in question

    – ram
    Nov 15 '18 at 17:17






  • 1





    @oguzismail I added the new edge cases please check

    – ram
    Nov 15 '18 at 17:26











  • Please clarify — are the [NOT] IN conditions always numeric? You don't explicitly state that, and you really need to be specific when defining this sort of problem. I think Samuel Kirschner's comments are on-track (but I haven't tested)

    – Stephen P
    Nov 15 '18 at 17:34















1















I have conditions in a file, that has around 10 thousend lines. As shown below.



COLUMN_NAME NOT IN 1234534
COLUMN_NAME1 NOT IN 34252
COLUMN_NAME_2 not in 67496575
COLUMN_NAME NOT in 1234534
foo COLUMN_NAME NOT IN (1234534,453535) rest of the line
COLUMN_NAME NOT IN 1234534,453535
columnsd not in (23123124232,6464777) rest on the line
COLUMN_NAME NOT IN 1234534
COLUMN_NAME NOT IN 1234534
fdfsdf COLUMN_NAME not in 1234534
COLUMN_NAME not in 1234534
column NOT IN (6764577,434545)
COLUMN_NAME not in (1234534)


I want to use the sed command to add the brackets after the IN clause. I want to replace the conditions above result in output like below.



COLUMN_NAME NOT IN (1234534)
COLUMN_NAME1 NOT IN (3422)
COLUMN_NAME_2 not in (67496575)
COLUMN_NAME NOT in (1234534)
COLUMN_NAME NOT IN (1234534,453535) rest of the line
COLUMN_NAME NOT IN (1234534,453535)
columnsd not in (23123124232,6464777) rest on the line
COLUMN_NAME NOT IN (1234534)
COLUMN_NAME NOT IN (1234534)
fdfsdf COLUMN_NAME not in (1234534)
COLUMN_NAME not in (1234534)
column NOT IN (6764577,434545)
COLUMN_NAME not in (1234534)









share|improve this question
























  • sed -i.bak 's/(sins*)([0-9]+)/1(2)/' filename

    – Samuel Kirschner
    Nov 15 '18 at 16:51











  • @SamuelKirschner sir its not working for case COLUMN_NAME NOT IN 1234534 and also could you please give me some link reference so I can learn

    – ram
    Nov 15 '18 at 17:11












  • @oguzismail awk also adding () where it is already there like COLUMN_NAME NOT IN ((1234534,453535)) and also after the condition also we have some constant Apologies mixed in question

    – ram
    Nov 15 '18 at 17:17






  • 1





    @oguzismail I added the new edge cases please check

    – ram
    Nov 15 '18 at 17:26











  • Please clarify — are the [NOT] IN conditions always numeric? You don't explicitly state that, and you really need to be specific when defining this sort of problem. I think Samuel Kirschner's comments are on-track (but I haven't tested)

    – Stephen P
    Nov 15 '18 at 17:34













1












1








1








I have conditions in a file, that has around 10 thousend lines. As shown below.



COLUMN_NAME NOT IN 1234534
COLUMN_NAME1 NOT IN 34252
COLUMN_NAME_2 not in 67496575
COLUMN_NAME NOT in 1234534
foo COLUMN_NAME NOT IN (1234534,453535) rest of the line
COLUMN_NAME NOT IN 1234534,453535
columnsd not in (23123124232,6464777) rest on the line
COLUMN_NAME NOT IN 1234534
COLUMN_NAME NOT IN 1234534
fdfsdf COLUMN_NAME not in 1234534
COLUMN_NAME not in 1234534
column NOT IN (6764577,434545)
COLUMN_NAME not in (1234534)


I want to use the sed command to add the brackets after the IN clause. I want to replace the conditions above result in output like below.



COLUMN_NAME NOT IN (1234534)
COLUMN_NAME1 NOT IN (3422)
COLUMN_NAME_2 not in (67496575)
COLUMN_NAME NOT in (1234534)
COLUMN_NAME NOT IN (1234534,453535) rest of the line
COLUMN_NAME NOT IN (1234534,453535)
columnsd not in (23123124232,6464777) rest on the line
COLUMN_NAME NOT IN (1234534)
COLUMN_NAME NOT IN (1234534)
fdfsdf COLUMN_NAME not in (1234534)
COLUMN_NAME not in (1234534)
column NOT IN (6764577,434545)
COLUMN_NAME not in (1234534)









share|improve this question
















I have conditions in a file, that has around 10 thousend lines. As shown below.



COLUMN_NAME NOT IN 1234534
COLUMN_NAME1 NOT IN 34252
COLUMN_NAME_2 not in 67496575
COLUMN_NAME NOT in 1234534
foo COLUMN_NAME NOT IN (1234534,453535) rest of the line
COLUMN_NAME NOT IN 1234534,453535
columnsd not in (23123124232,6464777) rest on the line
COLUMN_NAME NOT IN 1234534
COLUMN_NAME NOT IN 1234534
fdfsdf COLUMN_NAME not in 1234534
COLUMN_NAME not in 1234534
column NOT IN (6764577,434545)
COLUMN_NAME not in (1234534)


I want to use the sed command to add the brackets after the IN clause. I want to replace the conditions above result in output like below.



COLUMN_NAME NOT IN (1234534)
COLUMN_NAME1 NOT IN (3422)
COLUMN_NAME_2 not in (67496575)
COLUMN_NAME NOT in (1234534)
COLUMN_NAME NOT IN (1234534,453535) rest of the line
COLUMN_NAME NOT IN (1234534,453535)
columnsd not in (23123124232,6464777) rest on the line
COLUMN_NAME NOT IN (1234534)
COLUMN_NAME NOT IN (1234534)
fdfsdf COLUMN_NAME not in (1234534)
COLUMN_NAME not in (1234534)
column NOT IN (6764577,434545)
COLUMN_NAME not in (1234534)






shell unix sed






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 18:39









oguzismail

3,75031125




3,75031125










asked Nov 15 '18 at 16:34









ramram

4818




4818












  • sed -i.bak 's/(sins*)([0-9]+)/1(2)/' filename

    – Samuel Kirschner
    Nov 15 '18 at 16:51











  • @SamuelKirschner sir its not working for case COLUMN_NAME NOT IN 1234534 and also could you please give me some link reference so I can learn

    – ram
    Nov 15 '18 at 17:11












  • @oguzismail awk also adding () where it is already there like COLUMN_NAME NOT IN ((1234534,453535)) and also after the condition also we have some constant Apologies mixed in question

    – ram
    Nov 15 '18 at 17:17






  • 1





    @oguzismail I added the new edge cases please check

    – ram
    Nov 15 '18 at 17:26











  • Please clarify — are the [NOT] IN conditions always numeric? You don't explicitly state that, and you really need to be specific when defining this sort of problem. I think Samuel Kirschner's comments are on-track (but I haven't tested)

    – Stephen P
    Nov 15 '18 at 17:34

















  • sed -i.bak 's/(sins*)([0-9]+)/1(2)/' filename

    – Samuel Kirschner
    Nov 15 '18 at 16:51











  • @SamuelKirschner sir its not working for case COLUMN_NAME NOT IN 1234534 and also could you please give me some link reference so I can learn

    – ram
    Nov 15 '18 at 17:11












  • @oguzismail awk also adding () where it is already there like COLUMN_NAME NOT IN ((1234534,453535)) and also after the condition also we have some constant Apologies mixed in question

    – ram
    Nov 15 '18 at 17:17






  • 1





    @oguzismail I added the new edge cases please check

    – ram
    Nov 15 '18 at 17:26











  • Please clarify — are the [NOT] IN conditions always numeric? You don't explicitly state that, and you really need to be specific when defining this sort of problem. I think Samuel Kirschner's comments are on-track (but I haven't tested)

    – Stephen P
    Nov 15 '18 at 17:34
















sed -i.bak 's/(sins*)([0-9]+)/1(2)/' filename

– Samuel Kirschner
Nov 15 '18 at 16:51





sed -i.bak 's/(sins*)([0-9]+)/1(2)/' filename

– Samuel Kirschner
Nov 15 '18 at 16:51













@SamuelKirschner sir its not working for case COLUMN_NAME NOT IN 1234534 and also could you please give me some link reference so I can learn

– ram
Nov 15 '18 at 17:11






@SamuelKirschner sir its not working for case COLUMN_NAME NOT IN 1234534 and also could you please give me some link reference so I can learn

– ram
Nov 15 '18 at 17:11














@oguzismail awk also adding () where it is already there like COLUMN_NAME NOT IN ((1234534,453535)) and also after the condition also we have some constant Apologies mixed in question

– ram
Nov 15 '18 at 17:17





@oguzismail awk also adding () where it is already there like COLUMN_NAME NOT IN ((1234534,453535)) and also after the condition also we have some constant Apologies mixed in question

– ram
Nov 15 '18 at 17:17




1




1





@oguzismail I added the new edge cases please check

– ram
Nov 15 '18 at 17:26





@oguzismail I added the new edge cases please check

– ram
Nov 15 '18 at 17:26













Please clarify — are the [NOT] IN conditions always numeric? You don't explicitly state that, and you really need to be specific when defining this sort of problem. I think Samuel Kirschner's comments are on-track (but I haven't tested)

– Stephen P
Nov 15 '18 at 17:34





Please clarify — are the [NOT] IN conditions always numeric? You don't explicitly state that, and you really need to be specific when defining this sort of problem. I think Samuel Kirschner's comments are on-track (but I haven't tested)

– Stephen P
Nov 15 '18 at 17:34












2 Answers
2






active

oldest

votes


















4














This would be what you are looking for



sed -i .bak 's/(sins+)([^() ]+)/1(2)/i' file


Explanation



s/regexp/replacement/flags tries to match input line against regexp, and if it manages, replaces the matched part with replacement.




  1. regexp part:




    • (sins+) (ERE: (sins+)) matches a space followed by in plus one or more spaces, and keeps the matched portion in capture group 1,


    • ([^() ]+) (ERE: ([^() ]+)) matches a group of non-space, non-parenthesis chars and keeps it in capture group 2,



  2. replacement part:




    • 1(2) expands up to <capture group 1>(<capture group 2>),



  3. flags part:




    • i makes the matching case-insensitive.






share|improve this answer




















  • 1





    Thanks it works Could you please explain the code or please provide some reference, So I can go through?

    – ram
    Nov 15 '18 at 17:37






  • 1





    Apologies I am very new in unix could you please explain /1(2)/. Thank you for your Help

    – ram
    Nov 15 '18 at 17:48






  • 1





    Thank you it helps

    – ram
    Nov 15 '18 at 17:56


















2














Lets take it step by step.



I guess the best rule would be:



  • IN (case insensetive) IN

  • followed by: a number [0-9]+

  • optionally followed by multiple (*): comma and number ,[0-9]

This would result in the regex:
IN[0-9]+(,[0-9]+)*



Next step is to add mandatory (s) and optional (s*) whitespace:



sINs*[0-9]+s*(,s*[0-9]+s*)*


Now to replace sINs* [0-9]+s*(,s*[0-9]+s*)* with 1(2) it you need capture groups.



(sINs*)([0-9]+s*(,s*[0-9]+s*)*)


Now the regex has to go inside s//1(2)/i (the i means case insensitive)



s/(sINs*)([0-9]+s*(,s*[0-9]+s*)*)/1(2)/i


And at last we have a shell command, including a backup in case of mistakes.



sed -i.bak 's/(sINs*)([0-9]+s*(,s*[0-9]+s*)*)/1(2)/i' filename


I tested it on your example data and the output is:



COLUMN_NAME NOT IN (1234534)
COLUMN_NAME1 NOT IN (34252)
COLUMN_NAME_2 not in (67496575)
COLUMN_NAME NOT in (1234534)
foo COLUMN_NAME NOT IN (1234534,453535) rest of the line
COLUMN_NAME NOT IN (1234534,453535)
columnsd not in (23123124232,6464777) rest on the line
COLUMN_NAME NOT IN (1234534)
COLUMN_NAME NOT IN (1234534)
fdfsdf COLUMN_NAME not in (1234534)
COLUMN_NAME not in (1234534)
column NOT IN (6764577,434545)
COLUMN_NAME not in (1234534)





share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    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',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53323979%2fhow-to-add-opening-and-closing-brackets-in-the-condition-using-unix-shell-script%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    4














    This would be what you are looking for



    sed -i .bak 's/(sins+)([^() ]+)/1(2)/i' file


    Explanation



    s/regexp/replacement/flags tries to match input line against regexp, and if it manages, replaces the matched part with replacement.




    1. regexp part:




      • (sins+) (ERE: (sins+)) matches a space followed by in plus one or more spaces, and keeps the matched portion in capture group 1,


      • ([^() ]+) (ERE: ([^() ]+)) matches a group of non-space, non-parenthesis chars and keeps it in capture group 2,



    2. replacement part:




      • 1(2) expands up to <capture group 1>(<capture group 2>),



    3. flags part:




      • i makes the matching case-insensitive.






    share|improve this answer




















    • 1





      Thanks it works Could you please explain the code or please provide some reference, So I can go through?

      – ram
      Nov 15 '18 at 17:37






    • 1





      Apologies I am very new in unix could you please explain /1(2)/. Thank you for your Help

      – ram
      Nov 15 '18 at 17:48






    • 1





      Thank you it helps

      – ram
      Nov 15 '18 at 17:56















    4














    This would be what you are looking for



    sed -i .bak 's/(sins+)([^() ]+)/1(2)/i' file


    Explanation



    s/regexp/replacement/flags tries to match input line against regexp, and if it manages, replaces the matched part with replacement.




    1. regexp part:




      • (sins+) (ERE: (sins+)) matches a space followed by in plus one or more spaces, and keeps the matched portion in capture group 1,


      • ([^() ]+) (ERE: ([^() ]+)) matches a group of non-space, non-parenthesis chars and keeps it in capture group 2,



    2. replacement part:




      • 1(2) expands up to <capture group 1>(<capture group 2>),



    3. flags part:




      • i makes the matching case-insensitive.






    share|improve this answer




















    • 1





      Thanks it works Could you please explain the code or please provide some reference, So I can go through?

      – ram
      Nov 15 '18 at 17:37






    • 1





      Apologies I am very new in unix could you please explain /1(2)/. Thank you for your Help

      – ram
      Nov 15 '18 at 17:48






    • 1





      Thank you it helps

      – ram
      Nov 15 '18 at 17:56













    4












    4








    4







    This would be what you are looking for



    sed -i .bak 's/(sins+)([^() ]+)/1(2)/i' file


    Explanation



    s/regexp/replacement/flags tries to match input line against regexp, and if it manages, replaces the matched part with replacement.




    1. regexp part:




      • (sins+) (ERE: (sins+)) matches a space followed by in plus one or more spaces, and keeps the matched portion in capture group 1,


      • ([^() ]+) (ERE: ([^() ]+)) matches a group of non-space, non-parenthesis chars and keeps it in capture group 2,



    2. replacement part:




      • 1(2) expands up to <capture group 1>(<capture group 2>),



    3. flags part:




      • i makes the matching case-insensitive.






    share|improve this answer















    This would be what you are looking for



    sed -i .bak 's/(sins+)([^() ]+)/1(2)/i' file


    Explanation



    s/regexp/replacement/flags tries to match input line against regexp, and if it manages, replaces the matched part with replacement.




    1. regexp part:




      • (sins+) (ERE: (sins+)) matches a space followed by in plus one or more spaces, and keeps the matched portion in capture group 1,


      • ([^() ]+) (ERE: ([^() ]+)) matches a group of non-space, non-parenthesis chars and keeps it in capture group 2,



    2. replacement part:




      • 1(2) expands up to <capture group 1>(<capture group 2>),



    3. flags part:




      • i makes the matching case-insensitive.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 19 '18 at 6:09

























    answered Nov 15 '18 at 17:18









    oguzismailoguzismail

    3,75031125




    3,75031125







    • 1





      Thanks it works Could you please explain the code or please provide some reference, So I can go through?

      – ram
      Nov 15 '18 at 17:37






    • 1





      Apologies I am very new in unix could you please explain /1(2)/. Thank you for your Help

      – ram
      Nov 15 '18 at 17:48






    • 1





      Thank you it helps

      – ram
      Nov 15 '18 at 17:56












    • 1





      Thanks it works Could you please explain the code or please provide some reference, So I can go through?

      – ram
      Nov 15 '18 at 17:37






    • 1





      Apologies I am very new in unix could you please explain /1(2)/. Thank you for your Help

      – ram
      Nov 15 '18 at 17:48






    • 1





      Thank you it helps

      – ram
      Nov 15 '18 at 17:56







    1




    1





    Thanks it works Could you please explain the code or please provide some reference, So I can go through?

    – ram
    Nov 15 '18 at 17:37





    Thanks it works Could you please explain the code or please provide some reference, So I can go through?

    – ram
    Nov 15 '18 at 17:37




    1




    1





    Apologies I am very new in unix could you please explain /1(2)/. Thank you for your Help

    – ram
    Nov 15 '18 at 17:48





    Apologies I am very new in unix could you please explain /1(2)/. Thank you for your Help

    – ram
    Nov 15 '18 at 17:48




    1




    1





    Thank you it helps

    – ram
    Nov 15 '18 at 17:56





    Thank you it helps

    – ram
    Nov 15 '18 at 17:56













    2














    Lets take it step by step.



    I guess the best rule would be:



    • IN (case insensetive) IN

    • followed by: a number [0-9]+

    • optionally followed by multiple (*): comma and number ,[0-9]

    This would result in the regex:
    IN[0-9]+(,[0-9]+)*



    Next step is to add mandatory (s) and optional (s*) whitespace:



    sINs*[0-9]+s*(,s*[0-9]+s*)*


    Now to replace sINs* [0-9]+s*(,s*[0-9]+s*)* with 1(2) it you need capture groups.



    (sINs*)([0-9]+s*(,s*[0-9]+s*)*)


    Now the regex has to go inside s//1(2)/i (the i means case insensitive)



    s/(sINs*)([0-9]+s*(,s*[0-9]+s*)*)/1(2)/i


    And at last we have a shell command, including a backup in case of mistakes.



    sed -i.bak 's/(sINs*)([0-9]+s*(,s*[0-9]+s*)*)/1(2)/i' filename


    I tested it on your example data and the output is:



    COLUMN_NAME NOT IN (1234534)
    COLUMN_NAME1 NOT IN (34252)
    COLUMN_NAME_2 not in (67496575)
    COLUMN_NAME NOT in (1234534)
    foo COLUMN_NAME NOT IN (1234534,453535) rest of the line
    COLUMN_NAME NOT IN (1234534,453535)
    columnsd not in (23123124232,6464777) rest on the line
    COLUMN_NAME NOT IN (1234534)
    COLUMN_NAME NOT IN (1234534)
    fdfsdf COLUMN_NAME not in (1234534)
    COLUMN_NAME not in (1234534)
    column NOT IN (6764577,434545)
    COLUMN_NAME not in (1234534)





    share|improve this answer



























      2














      Lets take it step by step.



      I guess the best rule would be:



      • IN (case insensetive) IN

      • followed by: a number [0-9]+

      • optionally followed by multiple (*): comma and number ,[0-9]

      This would result in the regex:
      IN[0-9]+(,[0-9]+)*



      Next step is to add mandatory (s) and optional (s*) whitespace:



      sINs*[0-9]+s*(,s*[0-9]+s*)*


      Now to replace sINs* [0-9]+s*(,s*[0-9]+s*)* with 1(2) it you need capture groups.



      (sINs*)([0-9]+s*(,s*[0-9]+s*)*)


      Now the regex has to go inside s//1(2)/i (the i means case insensitive)



      s/(sINs*)([0-9]+s*(,s*[0-9]+s*)*)/1(2)/i


      And at last we have a shell command, including a backup in case of mistakes.



      sed -i.bak 's/(sINs*)([0-9]+s*(,s*[0-9]+s*)*)/1(2)/i' filename


      I tested it on your example data and the output is:



      COLUMN_NAME NOT IN (1234534)
      COLUMN_NAME1 NOT IN (34252)
      COLUMN_NAME_2 not in (67496575)
      COLUMN_NAME NOT in (1234534)
      foo COLUMN_NAME NOT IN (1234534,453535) rest of the line
      COLUMN_NAME NOT IN (1234534,453535)
      columnsd not in (23123124232,6464777) rest on the line
      COLUMN_NAME NOT IN (1234534)
      COLUMN_NAME NOT IN (1234534)
      fdfsdf COLUMN_NAME not in (1234534)
      COLUMN_NAME not in (1234534)
      column NOT IN (6764577,434545)
      COLUMN_NAME not in (1234534)





      share|improve this answer

























        2












        2








        2







        Lets take it step by step.



        I guess the best rule would be:



        • IN (case insensetive) IN

        • followed by: a number [0-9]+

        • optionally followed by multiple (*): comma and number ,[0-9]

        This would result in the regex:
        IN[0-9]+(,[0-9]+)*



        Next step is to add mandatory (s) and optional (s*) whitespace:



        sINs*[0-9]+s*(,s*[0-9]+s*)*


        Now to replace sINs* [0-9]+s*(,s*[0-9]+s*)* with 1(2) it you need capture groups.



        (sINs*)([0-9]+s*(,s*[0-9]+s*)*)


        Now the regex has to go inside s//1(2)/i (the i means case insensitive)



        s/(sINs*)([0-9]+s*(,s*[0-9]+s*)*)/1(2)/i


        And at last we have a shell command, including a backup in case of mistakes.



        sed -i.bak 's/(sINs*)([0-9]+s*(,s*[0-9]+s*)*)/1(2)/i' filename


        I tested it on your example data and the output is:



        COLUMN_NAME NOT IN (1234534)
        COLUMN_NAME1 NOT IN (34252)
        COLUMN_NAME_2 not in (67496575)
        COLUMN_NAME NOT in (1234534)
        foo COLUMN_NAME NOT IN (1234534,453535) rest of the line
        COLUMN_NAME NOT IN (1234534,453535)
        columnsd not in (23123124232,6464777) rest on the line
        COLUMN_NAME NOT IN (1234534)
        COLUMN_NAME NOT IN (1234534)
        fdfsdf COLUMN_NAME not in (1234534)
        COLUMN_NAME not in (1234534)
        column NOT IN (6764577,434545)
        COLUMN_NAME not in (1234534)





        share|improve this answer













        Lets take it step by step.



        I guess the best rule would be:



        • IN (case insensetive) IN

        • followed by: a number [0-9]+

        • optionally followed by multiple (*): comma and number ,[0-9]

        This would result in the regex:
        IN[0-9]+(,[0-9]+)*



        Next step is to add mandatory (s) and optional (s*) whitespace:



        sINs*[0-9]+s*(,s*[0-9]+s*)*


        Now to replace sINs* [0-9]+s*(,s*[0-9]+s*)* with 1(2) it you need capture groups.



        (sINs*)([0-9]+s*(,s*[0-9]+s*)*)


        Now the regex has to go inside s//1(2)/i (the i means case insensitive)



        s/(sINs*)([0-9]+s*(,s*[0-9]+s*)*)/1(2)/i


        And at last we have a shell command, including a backup in case of mistakes.



        sed -i.bak 's/(sINs*)([0-9]+s*(,s*[0-9]+s*)*)/1(2)/i' filename


        I tested it on your example data and the output is:



        COLUMN_NAME NOT IN (1234534)
        COLUMN_NAME1 NOT IN (34252)
        COLUMN_NAME_2 not in (67496575)
        COLUMN_NAME NOT in (1234534)
        foo COLUMN_NAME NOT IN (1234534,453535) rest of the line
        COLUMN_NAME NOT IN (1234534,453535)
        columnsd not in (23123124232,6464777) rest on the line
        COLUMN_NAME NOT IN (1234534)
        COLUMN_NAME NOT IN (1234534)
        fdfsdf COLUMN_NAME not in (1234534)
        COLUMN_NAME not in (1234534)
        column NOT IN (6764577,434545)
        COLUMN_NAME not in (1234534)






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 15 '18 at 17:51









        Samuel KirschnerSamuel Kirschner

        9251717




        9251717



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53323979%2fhow-to-add-opening-and-closing-brackets-in-the-condition-using-unix-shell-script%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Top Tejano songwriter Luis Silva dead of heart attack at 64

            政党

            天津地下鉄3号線