search column 2 in csv file for value, if value, then insert “invalid” and shift cells right

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











up vote
3
down vote

favorite












I have csv file that is auto generated by a script but for some of the records (line items) received I need to search column 2 and if the values contains "*.app" I need to print "INVALID" into column 2 for all records matching and shift cells to the right.



Example Data File:



 DOM,PROJ,APP,USER,DATE,TIME,STATUS
www,test,biz.app,bob,6-1-18,09:33,OK //Example of good line
www,biz.app,tony,7-11-17,06:22,ok //Example of bad line
...

Wanted output:
DOM,PROJ,APP,USER,DATE,TIME,STATUS
www,test,biz.app,bob,6-1-18,09:33,OK
www,INVALID,biz.app,tony,7-11-17,06:22,ok //Example of fixed line
...


I have unsuccessfully tried awk, sed, and if statement but not getting the results I need



 e.g.

if [ awk -F',' ' print $2 < FILE' ] == "*.app" ; then ; echo "INVALID"; fi


Which is obviously terrible... New to bash thanks all!







share|improve this question

























    up vote
    3
    down vote

    favorite












    I have csv file that is auto generated by a script but for some of the records (line items) received I need to search column 2 and if the values contains "*.app" I need to print "INVALID" into column 2 for all records matching and shift cells to the right.



    Example Data File:



     DOM,PROJ,APP,USER,DATE,TIME,STATUS
    www,test,biz.app,bob,6-1-18,09:33,OK //Example of good line
    www,biz.app,tony,7-11-17,06:22,ok //Example of bad line
    ...

    Wanted output:
    DOM,PROJ,APP,USER,DATE,TIME,STATUS
    www,test,biz.app,bob,6-1-18,09:33,OK
    www,INVALID,biz.app,tony,7-11-17,06:22,ok //Example of fixed line
    ...


    I have unsuccessfully tried awk, sed, and if statement but not getting the results I need



     e.g.

    if [ awk -F',' ' print $2 < FILE' ] == "*.app" ; then ; echo "INVALID"; fi


    Which is obviously terrible... New to bash thanks all!







    share|improve this question























      up vote
      3
      down vote

      favorite









      up vote
      3
      down vote

      favorite











      I have csv file that is auto generated by a script but for some of the records (line items) received I need to search column 2 and if the values contains "*.app" I need to print "INVALID" into column 2 for all records matching and shift cells to the right.



      Example Data File:



       DOM,PROJ,APP,USER,DATE,TIME,STATUS
      www,test,biz.app,bob,6-1-18,09:33,OK //Example of good line
      www,biz.app,tony,7-11-17,06:22,ok //Example of bad line
      ...

      Wanted output:
      DOM,PROJ,APP,USER,DATE,TIME,STATUS
      www,test,biz.app,bob,6-1-18,09:33,OK
      www,INVALID,biz.app,tony,7-11-17,06:22,ok //Example of fixed line
      ...


      I have unsuccessfully tried awk, sed, and if statement but not getting the results I need



       e.g.

      if [ awk -F',' ' print $2 < FILE' ] == "*.app" ; then ; echo "INVALID"; fi


      Which is obviously terrible... New to bash thanks all!







      share|improve this question













      I have csv file that is auto generated by a script but for some of the records (line items) received I need to search column 2 and if the values contains "*.app" I need to print "INVALID" into column 2 for all records matching and shift cells to the right.



      Example Data File:



       DOM,PROJ,APP,USER,DATE,TIME,STATUS
      www,test,biz.app,bob,6-1-18,09:33,OK //Example of good line
      www,biz.app,tony,7-11-17,06:22,ok //Example of bad line
      ...

      Wanted output:
      DOM,PROJ,APP,USER,DATE,TIME,STATUS
      www,test,biz.app,bob,6-1-18,09:33,OK
      www,INVALID,biz.app,tony,7-11-17,06:22,ok //Example of fixed line
      ...


      I have unsuccessfully tried awk, sed, and if statement but not getting the results I need



       e.g.

      if [ awk -F',' ' print $2 < FILE' ] == "*.app" ; then ; echo "INVALID"; fi


      Which is obviously terrible... New to bash thanks all!









      share|improve this question












      share|improve this question




      share|improve this question








      edited Jun 25 at 19:51









      Jeff Schaller

      30.8k846104




      30.8k846104









      asked Jun 25 at 19:45









      SSDdude

      544




      544




















          3 Answers
          3






          active

          oldest

          votes

















          up vote
          0
          down vote













          awk -F, -vOFS=, '$2 ~ /.app$/ for (i = NF + 1; i > 2; --i) $i = $(i-1); $2 = "INVALID" 1' file >newfile


          This would create newfile from file. The awk command sets both the input and output field delimiter to a comma, then tests the value of column two against a regular expression which matches the string .app at the end of the value. If the test succeeds, the record's fields are shifted right one step to make place for the string INVALID as the new second field.



          The trailing 1 could be replaced by print (it causes every line to be outputted).



          With the given example data, the output file would contain



          DOM,PROJ,APP,USER,DATE,TIME,STATUS
          www,test,biz.app,bob,6-1-18,09:33,OK
          www,INVALID,biz.app,tony,7-11-17,06:22,ok





          share|improve this answer























          • Thanks so much Kusalananda, this works great! Can you tell me how to modify this command so that when the value ".app" is found in column 2 it does not shift the cells right but rather replaces .app with INVALID?
            – SSDdude
            Jun 26 at 16:15










          • @SSDdude Just remove the for loop: $2 = "INVALID" . Keep the rest as is.
            – Kusalananda
            Jun 26 at 16:16











          • Sorry I don't fully understand... if I remove $2 = "INVALID" how will the code replace .app with INVALID?
            – SSDdude
            Jun 26 at 16:20










          • @SSDdude No, that's what you are left with if you remove the for loop. The complete command: awk -F, -vOFS=, '$2 ~ /.app$/ $2 = "INVALID 1'
            – Kusalananda
            Jun 26 at 17:26

















          up vote
          0
          down vote













          Awk approach:



          awk 'BEGIN FS = OFS = "," NR > 1 && $2 ~ /.*.app/ $2 = "INVALID" OFS $2 1' file



          • NR > 1 && $2 ~ /.*.app/ - if record number is greater than 1 (all but first) and the 2nd field $2 matchs the pattern /.*.app/

          The output:



          DOM,PROJ,APP,USER,DATE,TIME,STATUS
          www,test,biz.app,bob,6-1-18,09:33,OK
          www,INVALID,biz.app,tony,7-11-17,06:22,ok





          share|improve this answer























          • Hey Roman thanks for the answer but this doesn't shift biz.app to the right it replaces the value with INVALD but does not align the other columns. Any idea how I can do this? Thanks!
            – SSDdude
            Jun 25 at 20:08










          • @SSDdude, see my update
            – RomanPerekhrest
            Jun 25 at 20:10










          • Hey Roman thanks again... this works great for everything but the first record (new test had bad line as first line).. I tried modifying your awk statement but couldn't figure out how to NOT IGNORE the first records if the second cell contains .app... Thoughts? THANKS!
            – SSDdude
            Jun 25 at 20:26










          • @SSDdude, your new conditions seem to be unclear. My approach provides the current expected output. Extend your question with full input fragment and respective output.
            – RomanPerekhrest
            Jun 26 at 5:13

















          up vote
          0
          down vote













          Using the GNU sed tool we can approach this problem as follows:



           sed -e '
          1!s/,/n&/2
          /.appn/s/,/,INVALID,/
          s/n//
          ' file.csv


          To be read as:



           ° Only for lines that are not the first, meaning, skip the header from being considered for processing, whilst for the others, place a marker at the second occurrence of the comma.
          ° Any line that has the 2nd field terminating in a .app append the string INVALID after the first field.
          ° Now take away the marker.
          ° N. B. Lines whose 2nd field doesn't comprise *.app are passed on unmodified.





          share|improve this answer























            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
            );



            );








             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f451838%2fsearch-column-2-in-csv-file-for-value-if-value-then-insert-invalid-and-shift%23new-answer', 'question_page');

            );

            Post as a guest






























            3 Answers
            3






            active

            oldest

            votes








            3 Answers
            3






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote













            awk -F, -vOFS=, '$2 ~ /.app$/ for (i = NF + 1; i > 2; --i) $i = $(i-1); $2 = "INVALID" 1' file >newfile


            This would create newfile from file. The awk command sets both the input and output field delimiter to a comma, then tests the value of column two against a regular expression which matches the string .app at the end of the value. If the test succeeds, the record's fields are shifted right one step to make place for the string INVALID as the new second field.



            The trailing 1 could be replaced by print (it causes every line to be outputted).



            With the given example data, the output file would contain



            DOM,PROJ,APP,USER,DATE,TIME,STATUS
            www,test,biz.app,bob,6-1-18,09:33,OK
            www,INVALID,biz.app,tony,7-11-17,06:22,ok





            share|improve this answer























            • Thanks so much Kusalananda, this works great! Can you tell me how to modify this command so that when the value ".app" is found in column 2 it does not shift the cells right but rather replaces .app with INVALID?
              – SSDdude
              Jun 26 at 16:15










            • @SSDdude Just remove the for loop: $2 = "INVALID" . Keep the rest as is.
              – Kusalananda
              Jun 26 at 16:16











            • Sorry I don't fully understand... if I remove $2 = "INVALID" how will the code replace .app with INVALID?
              – SSDdude
              Jun 26 at 16:20










            • @SSDdude No, that's what you are left with if you remove the for loop. The complete command: awk -F, -vOFS=, '$2 ~ /.app$/ $2 = "INVALID 1'
              – Kusalananda
              Jun 26 at 17:26














            up vote
            0
            down vote













            awk -F, -vOFS=, '$2 ~ /.app$/ for (i = NF + 1; i > 2; --i) $i = $(i-1); $2 = "INVALID" 1' file >newfile


            This would create newfile from file. The awk command sets both the input and output field delimiter to a comma, then tests the value of column two against a regular expression which matches the string .app at the end of the value. If the test succeeds, the record's fields are shifted right one step to make place for the string INVALID as the new second field.



            The trailing 1 could be replaced by print (it causes every line to be outputted).



            With the given example data, the output file would contain



            DOM,PROJ,APP,USER,DATE,TIME,STATUS
            www,test,biz.app,bob,6-1-18,09:33,OK
            www,INVALID,biz.app,tony,7-11-17,06:22,ok





            share|improve this answer























            • Thanks so much Kusalananda, this works great! Can you tell me how to modify this command so that when the value ".app" is found in column 2 it does not shift the cells right but rather replaces .app with INVALID?
              – SSDdude
              Jun 26 at 16:15










            • @SSDdude Just remove the for loop: $2 = "INVALID" . Keep the rest as is.
              – Kusalananda
              Jun 26 at 16:16











            • Sorry I don't fully understand... if I remove $2 = "INVALID" how will the code replace .app with INVALID?
              – SSDdude
              Jun 26 at 16:20










            • @SSDdude No, that's what you are left with if you remove the for loop. The complete command: awk -F, -vOFS=, '$2 ~ /.app$/ $2 = "INVALID 1'
              – Kusalananda
              Jun 26 at 17:26












            up vote
            0
            down vote










            up vote
            0
            down vote









            awk -F, -vOFS=, '$2 ~ /.app$/ for (i = NF + 1; i > 2; --i) $i = $(i-1); $2 = "INVALID" 1' file >newfile


            This would create newfile from file. The awk command sets both the input and output field delimiter to a comma, then tests the value of column two against a regular expression which matches the string .app at the end of the value. If the test succeeds, the record's fields are shifted right one step to make place for the string INVALID as the new second field.



            The trailing 1 could be replaced by print (it causes every line to be outputted).



            With the given example data, the output file would contain



            DOM,PROJ,APP,USER,DATE,TIME,STATUS
            www,test,biz.app,bob,6-1-18,09:33,OK
            www,INVALID,biz.app,tony,7-11-17,06:22,ok





            share|improve this answer















            awk -F, -vOFS=, '$2 ~ /.app$/ for (i = NF + 1; i > 2; --i) $i = $(i-1); $2 = "INVALID" 1' file >newfile


            This would create newfile from file. The awk command sets both the input and output field delimiter to a comma, then tests the value of column two against a regular expression which matches the string .app at the end of the value. If the test succeeds, the record's fields are shifted right one step to make place for the string INVALID as the new second field.



            The trailing 1 could be replaced by print (it causes every line to be outputted).



            With the given example data, the output file would contain



            DOM,PROJ,APP,USER,DATE,TIME,STATUS
            www,test,biz.app,bob,6-1-18,09:33,OK
            www,INVALID,biz.app,tony,7-11-17,06:22,ok






            share|improve this answer















            share|improve this answer



            share|improve this answer








            edited Jun 25 at 20:10


























            answered Jun 25 at 19:55









            Kusalananda

            101k13199312




            101k13199312











            • Thanks so much Kusalananda, this works great! Can you tell me how to modify this command so that when the value ".app" is found in column 2 it does not shift the cells right but rather replaces .app with INVALID?
              – SSDdude
              Jun 26 at 16:15










            • @SSDdude Just remove the for loop: $2 = "INVALID" . Keep the rest as is.
              – Kusalananda
              Jun 26 at 16:16











            • Sorry I don't fully understand... if I remove $2 = "INVALID" how will the code replace .app with INVALID?
              – SSDdude
              Jun 26 at 16:20










            • @SSDdude No, that's what you are left with if you remove the for loop. The complete command: awk -F, -vOFS=, '$2 ~ /.app$/ $2 = "INVALID 1'
              – Kusalananda
              Jun 26 at 17:26
















            • Thanks so much Kusalananda, this works great! Can you tell me how to modify this command so that when the value ".app" is found in column 2 it does not shift the cells right but rather replaces .app with INVALID?
              – SSDdude
              Jun 26 at 16:15










            • @SSDdude Just remove the for loop: $2 = "INVALID" . Keep the rest as is.
              – Kusalananda
              Jun 26 at 16:16











            • Sorry I don't fully understand... if I remove $2 = "INVALID" how will the code replace .app with INVALID?
              – SSDdude
              Jun 26 at 16:20










            • @SSDdude No, that's what you are left with if you remove the for loop. The complete command: awk -F, -vOFS=, '$2 ~ /.app$/ $2 = "INVALID 1'
              – Kusalananda
              Jun 26 at 17:26















            Thanks so much Kusalananda, this works great! Can you tell me how to modify this command so that when the value ".app" is found in column 2 it does not shift the cells right but rather replaces .app with INVALID?
            – SSDdude
            Jun 26 at 16:15




            Thanks so much Kusalananda, this works great! Can you tell me how to modify this command so that when the value ".app" is found in column 2 it does not shift the cells right but rather replaces .app with INVALID?
            – SSDdude
            Jun 26 at 16:15












            @SSDdude Just remove the for loop: $2 = "INVALID" . Keep the rest as is.
            – Kusalananda
            Jun 26 at 16:16





            @SSDdude Just remove the for loop: $2 = "INVALID" . Keep the rest as is.
            – Kusalananda
            Jun 26 at 16:16













            Sorry I don't fully understand... if I remove $2 = "INVALID" how will the code replace .app with INVALID?
            – SSDdude
            Jun 26 at 16:20




            Sorry I don't fully understand... if I remove $2 = "INVALID" how will the code replace .app with INVALID?
            – SSDdude
            Jun 26 at 16:20












            @SSDdude No, that's what you are left with if you remove the for loop. The complete command: awk -F, -vOFS=, '$2 ~ /.app$/ $2 = "INVALID 1'
            – Kusalananda
            Jun 26 at 17:26




            @SSDdude No, that's what you are left with if you remove the for loop. The complete command: awk -F, -vOFS=, '$2 ~ /.app$/ $2 = "INVALID 1'
            – Kusalananda
            Jun 26 at 17:26












            up vote
            0
            down vote













            Awk approach:



            awk 'BEGIN FS = OFS = "," NR > 1 && $2 ~ /.*.app/ $2 = "INVALID" OFS $2 1' file



            • NR > 1 && $2 ~ /.*.app/ - if record number is greater than 1 (all but first) and the 2nd field $2 matchs the pattern /.*.app/

            The output:



            DOM,PROJ,APP,USER,DATE,TIME,STATUS
            www,test,biz.app,bob,6-1-18,09:33,OK
            www,INVALID,biz.app,tony,7-11-17,06:22,ok





            share|improve this answer























            • Hey Roman thanks for the answer but this doesn't shift biz.app to the right it replaces the value with INVALD but does not align the other columns. Any idea how I can do this? Thanks!
              – SSDdude
              Jun 25 at 20:08










            • @SSDdude, see my update
              – RomanPerekhrest
              Jun 25 at 20:10










            • Hey Roman thanks again... this works great for everything but the first record (new test had bad line as first line).. I tried modifying your awk statement but couldn't figure out how to NOT IGNORE the first records if the second cell contains .app... Thoughts? THANKS!
              – SSDdude
              Jun 25 at 20:26










            • @SSDdude, your new conditions seem to be unclear. My approach provides the current expected output. Extend your question with full input fragment and respective output.
              – RomanPerekhrest
              Jun 26 at 5:13














            up vote
            0
            down vote













            Awk approach:



            awk 'BEGIN FS = OFS = "," NR > 1 && $2 ~ /.*.app/ $2 = "INVALID" OFS $2 1' file



            • NR > 1 && $2 ~ /.*.app/ - if record number is greater than 1 (all but first) and the 2nd field $2 matchs the pattern /.*.app/

            The output:



            DOM,PROJ,APP,USER,DATE,TIME,STATUS
            www,test,biz.app,bob,6-1-18,09:33,OK
            www,INVALID,biz.app,tony,7-11-17,06:22,ok





            share|improve this answer























            • Hey Roman thanks for the answer but this doesn't shift biz.app to the right it replaces the value with INVALD but does not align the other columns. Any idea how I can do this? Thanks!
              – SSDdude
              Jun 25 at 20:08










            • @SSDdude, see my update
              – RomanPerekhrest
              Jun 25 at 20:10










            • Hey Roman thanks again... this works great for everything but the first record (new test had bad line as first line).. I tried modifying your awk statement but couldn't figure out how to NOT IGNORE the first records if the second cell contains .app... Thoughts? THANKS!
              – SSDdude
              Jun 25 at 20:26










            • @SSDdude, your new conditions seem to be unclear. My approach provides the current expected output. Extend your question with full input fragment and respective output.
              – RomanPerekhrest
              Jun 26 at 5:13












            up vote
            0
            down vote










            up vote
            0
            down vote









            Awk approach:



            awk 'BEGIN FS = OFS = "," NR > 1 && $2 ~ /.*.app/ $2 = "INVALID" OFS $2 1' file



            • NR > 1 && $2 ~ /.*.app/ - if record number is greater than 1 (all but first) and the 2nd field $2 matchs the pattern /.*.app/

            The output:



            DOM,PROJ,APP,USER,DATE,TIME,STATUS
            www,test,biz.app,bob,6-1-18,09:33,OK
            www,INVALID,biz.app,tony,7-11-17,06:22,ok





            share|improve this answer















            Awk approach:



            awk 'BEGIN FS = OFS = "," NR > 1 && $2 ~ /.*.app/ $2 = "INVALID" OFS $2 1' file



            • NR > 1 && $2 ~ /.*.app/ - if record number is greater than 1 (all but first) and the 2nd field $2 matchs the pattern /.*.app/

            The output:



            DOM,PROJ,APP,USER,DATE,TIME,STATUS
            www,test,biz.app,bob,6-1-18,09:33,OK
            www,INVALID,biz.app,tony,7-11-17,06:22,ok






            share|improve this answer















            share|improve this answer



            share|improve this answer








            edited Jun 25 at 20:10


























            answered Jun 25 at 19:57









            RomanPerekhrest

            22.4k12144




            22.4k12144











            • Hey Roman thanks for the answer but this doesn't shift biz.app to the right it replaces the value with INVALD but does not align the other columns. Any idea how I can do this? Thanks!
              – SSDdude
              Jun 25 at 20:08










            • @SSDdude, see my update
              – RomanPerekhrest
              Jun 25 at 20:10










            • Hey Roman thanks again... this works great for everything but the first record (new test had bad line as first line).. I tried modifying your awk statement but couldn't figure out how to NOT IGNORE the first records if the second cell contains .app... Thoughts? THANKS!
              – SSDdude
              Jun 25 at 20:26










            • @SSDdude, your new conditions seem to be unclear. My approach provides the current expected output. Extend your question with full input fragment and respective output.
              – RomanPerekhrest
              Jun 26 at 5:13
















            • Hey Roman thanks for the answer but this doesn't shift biz.app to the right it replaces the value with INVALD but does not align the other columns. Any idea how I can do this? Thanks!
              – SSDdude
              Jun 25 at 20:08










            • @SSDdude, see my update
              – RomanPerekhrest
              Jun 25 at 20:10










            • Hey Roman thanks again... this works great for everything but the first record (new test had bad line as first line).. I tried modifying your awk statement but couldn't figure out how to NOT IGNORE the first records if the second cell contains .app... Thoughts? THANKS!
              – SSDdude
              Jun 25 at 20:26










            • @SSDdude, your new conditions seem to be unclear. My approach provides the current expected output. Extend your question with full input fragment and respective output.
              – RomanPerekhrest
              Jun 26 at 5:13















            Hey Roman thanks for the answer but this doesn't shift biz.app to the right it replaces the value with INVALD but does not align the other columns. Any idea how I can do this? Thanks!
            – SSDdude
            Jun 25 at 20:08




            Hey Roman thanks for the answer but this doesn't shift biz.app to the right it replaces the value with INVALD but does not align the other columns. Any idea how I can do this? Thanks!
            – SSDdude
            Jun 25 at 20:08












            @SSDdude, see my update
            – RomanPerekhrest
            Jun 25 at 20:10




            @SSDdude, see my update
            – RomanPerekhrest
            Jun 25 at 20:10












            Hey Roman thanks again... this works great for everything but the first record (new test had bad line as first line).. I tried modifying your awk statement but couldn't figure out how to NOT IGNORE the first records if the second cell contains .app... Thoughts? THANKS!
            – SSDdude
            Jun 25 at 20:26




            Hey Roman thanks again... this works great for everything but the first record (new test had bad line as first line).. I tried modifying your awk statement but couldn't figure out how to NOT IGNORE the first records if the second cell contains .app... Thoughts? THANKS!
            – SSDdude
            Jun 25 at 20:26












            @SSDdude, your new conditions seem to be unclear. My approach provides the current expected output. Extend your question with full input fragment and respective output.
            – RomanPerekhrest
            Jun 26 at 5:13




            @SSDdude, your new conditions seem to be unclear. My approach provides the current expected output. Extend your question with full input fragment and respective output.
            – RomanPerekhrest
            Jun 26 at 5:13










            up vote
            0
            down vote













            Using the GNU sed tool we can approach this problem as follows:



             sed -e '
            1!s/,/n&/2
            /.appn/s/,/,INVALID,/
            s/n//
            ' file.csv


            To be read as:



             ° Only for lines that are not the first, meaning, skip the header from being considered for processing, whilst for the others, place a marker at the second occurrence of the comma.
            ° Any line that has the 2nd field terminating in a .app append the string INVALID after the first field.
            ° Now take away the marker.
            ° N. B. Lines whose 2nd field doesn't comprise *.app are passed on unmodified.





            share|improve this answer



























              up vote
              0
              down vote













              Using the GNU sed tool we can approach this problem as follows:



               sed -e '
              1!s/,/n&/2
              /.appn/s/,/,INVALID,/
              s/n//
              ' file.csv


              To be read as:



               ° Only for lines that are not the first, meaning, skip the header from being considered for processing, whilst for the others, place a marker at the second occurrence of the comma.
              ° Any line that has the 2nd field terminating in a .app append the string INVALID after the first field.
              ° Now take away the marker.
              ° N. B. Lines whose 2nd field doesn't comprise *.app are passed on unmodified.





              share|improve this answer

























                up vote
                0
                down vote










                up vote
                0
                down vote









                Using the GNU sed tool we can approach this problem as follows:



                 sed -e '
                1!s/,/n&/2
                /.appn/s/,/,INVALID,/
                s/n//
                ' file.csv


                To be read as:



                 ° Only for lines that are not the first, meaning, skip the header from being considered for processing, whilst for the others, place a marker at the second occurrence of the comma.
                ° Any line that has the 2nd field terminating in a .app append the string INVALID after the first field.
                ° Now take away the marker.
                ° N. B. Lines whose 2nd field doesn't comprise *.app are passed on unmodified.





                share|improve this answer















                Using the GNU sed tool we can approach this problem as follows:



                 sed -e '
                1!s/,/n&/2
                /.appn/s/,/,INVALID,/
                s/n//
                ' file.csv


                To be read as:



                 ° Only for lines that are not the first, meaning, skip the header from being considered for processing, whilst for the others, place a marker at the second occurrence of the comma.
                ° Any line that has the 2nd field terminating in a .app append the string INVALID after the first field.
                ° Now take away the marker.
                ° N. B. Lines whose 2nd field doesn't comprise *.app are passed on unmodified.






                share|improve this answer















                share|improve this answer



                share|improve this answer








                edited Jun 26 at 2:40


























                answered Jun 26 at 2:34









                Rakesh Sharma

                11013




                11013






















                     

                    draft saved


                    draft discarded


























                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f451838%2fsearch-column-2-in-csv-file-for-value-if-value-then-insert-invalid-and-shift%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?