remove the lines in CSV that contain specific pattern on the second field

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











up vote
0
down vote

favorite












How to remove the lines in .csv file that second field contain the word content?



example :



ams-hbase-log4j,content:n#LicensedtotheApacheSoftwareFoundation(ASF)underonen#ormorecontributorlicenseagreements
ams-log4j,content:n#n#LicensedtotheApacheSoftwareFoundation(ASF)underonen#ormorecontributorlicenseagreements.
ams-site,timeline.metrics.cache.size:150,


expected output



ams-site,timeline.metrics.cache.size:150,









share|improve this question























  • Warning: This specific example does not appear to have any embedded commas in the first field. For example, in "blah, blah",content:n ..., the comma inside the double-quotes is not a field separator. If someone reads this and has to deal with those, the answer provided here will not work correctly. Parsing CSV correctly to deal with such non-separating commas is tricky business.
    – Monty Harder
    Sep 13 at 21:55














up vote
0
down vote

favorite












How to remove the lines in .csv file that second field contain the word content?



example :



ams-hbase-log4j,content:n#LicensedtotheApacheSoftwareFoundation(ASF)underonen#ormorecontributorlicenseagreements
ams-log4j,content:n#n#LicensedtotheApacheSoftwareFoundation(ASF)underonen#ormorecontributorlicenseagreements.
ams-site,timeline.metrics.cache.size:150,


expected output



ams-site,timeline.metrics.cache.size:150,









share|improve this question























  • Warning: This specific example does not appear to have any embedded commas in the first field. For example, in "blah, blah",content:n ..., the comma inside the double-quotes is not a field separator. If someone reads this and has to deal with those, the answer provided here will not work correctly. Parsing CSV correctly to deal with such non-separating commas is tricky business.
    – Monty Harder
    Sep 13 at 21:55












up vote
0
down vote

favorite









up vote
0
down vote

favorite











How to remove the lines in .csv file that second field contain the word content?



example :



ams-hbase-log4j,content:n#LicensedtotheApacheSoftwareFoundation(ASF)underonen#ormorecontributorlicenseagreements
ams-log4j,content:n#n#LicensedtotheApacheSoftwareFoundation(ASF)underonen#ormorecontributorlicenseagreements.
ams-site,timeline.metrics.cache.size:150,


expected output



ams-site,timeline.metrics.cache.size:150,









share|improve this question















How to remove the lines in .csv file that second field contain the word content?



example :



ams-hbase-log4j,content:n#LicensedtotheApacheSoftwareFoundation(ASF)underonen#ormorecontributorlicenseagreements
ams-log4j,content:n#n#LicensedtotheApacheSoftwareFoundation(ASF)underonen#ormorecontributorlicenseagreements.
ams-site,timeline.metrics.cache.size:150,


expected output



ams-site,timeline.metrics.cache.size:150,






linux text-processing sed perl csv






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Sep 13 at 18:10









Jeff Schaller

33.1k849111




33.1k849111










asked Sep 13 at 18:00









yael

2,0391345




2,0391345











  • Warning: This specific example does not appear to have any embedded commas in the first field. For example, in "blah, blah",content:n ..., the comma inside the double-quotes is not a field separator. If someone reads this and has to deal with those, the answer provided here will not work correctly. Parsing CSV correctly to deal with such non-separating commas is tricky business.
    – Monty Harder
    Sep 13 at 21:55
















  • Warning: This specific example does not appear to have any embedded commas in the first field. For example, in "blah, blah",content:n ..., the comma inside the double-quotes is not a field separator. If someone reads this and has to deal with those, the answer provided here will not work correctly. Parsing CSV correctly to deal with such non-separating commas is tricky business.
    – Monty Harder
    Sep 13 at 21:55















Warning: This specific example does not appear to have any embedded commas in the first field. For example, in "blah, blah",content:n ..., the comma inside the double-quotes is not a field separator. If someone reads this and has to deal with those, the answer provided here will not work correctly. Parsing CSV correctly to deal with such non-separating commas is tricky business.
– Monty Harder
Sep 13 at 21:55




Warning: This specific example does not appear to have any embedded commas in the first field. For example, in "blah, blah",content:n ..., the comma inside the double-quotes is not a field separator. If someone reads this and has to deal with those, the answer provided here will not work correctly. Parsing CSV correctly to deal with such non-separating commas is tricky business.
– Monty Harder
Sep 13 at 21:55










3 Answers
3






active

oldest

votes

















up vote
6
down vote



accepted










With sed and to remove the lines that second field contains content, you could do:



sed '/^[^,]*,[^,]*content/d' infile


or to remove the lines which second field stars with content:



sed '/^[^,]*,content/d' infile





share|improve this answer





























    up vote
    4
    down vote













    awk -F, '$2 !~ /content/' file.csv


    If you mean "remove lines where the 2nd field starts with "content", then



    awk -F, '$2 !~ /^content/' file.csv





    share|improve this answer



























      up vote
      -1
      down vote













      basically what you can do in the easiest way is:



      cat "/dir/yourfile.csv" | grep -v ",content:" > newcsvfile.csv


      if you want to use the same file name then you can do:



      cat "/dir/yourfile.csv" | grep -v ",content:" > tmpfile && mv tmpfile "/dir/yourfile.csv"





      share|improve this answer
















      • 2




        What does this do if "content" is in the third position of a line?
        – Christian Gibbons
        Sep 13 at 18:07










      • that's an assumption if "content:" word will be only in the 2nd field, if it is in different field then the solution would not work and would be different
        – Javier Salas
        Sep 13 at 18:10










      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%2f468860%2fremove-the-lines-in-csv-that-contain-specific-pattern-on-the-second-field%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
      6
      down vote



      accepted










      With sed and to remove the lines that second field contains content, you could do:



      sed '/^[^,]*,[^,]*content/d' infile


      or to remove the lines which second field stars with content:



      sed '/^[^,]*,content/d' infile





      share|improve this answer


























        up vote
        6
        down vote



        accepted










        With sed and to remove the lines that second field contains content, you could do:



        sed '/^[^,]*,[^,]*content/d' infile


        or to remove the lines which second field stars with content:



        sed '/^[^,]*,content/d' infile





        share|improve this answer
























          up vote
          6
          down vote



          accepted







          up vote
          6
          down vote



          accepted






          With sed and to remove the lines that second field contains content, you could do:



          sed '/^[^,]*,[^,]*content/d' infile


          or to remove the lines which second field stars with content:



          sed '/^[^,]*,content/d' infile





          share|improve this answer














          With sed and to remove the lines that second field contains content, you could do:



          sed '/^[^,]*,[^,]*content/d' infile


          or to remove the lines which second field stars with content:



          sed '/^[^,]*,content/d' infile






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Sep 13 at 19:48

























          answered Sep 13 at 18:06









          αғsнιη

          16k92563




          16k92563






















              up vote
              4
              down vote













              awk -F, '$2 !~ /content/' file.csv


              If you mean "remove lines where the 2nd field starts with "content", then



              awk -F, '$2 !~ /^content/' file.csv





              share|improve this answer
























                up vote
                4
                down vote













                awk -F, '$2 !~ /content/' file.csv


                If you mean "remove lines where the 2nd field starts with "content", then



                awk -F, '$2 !~ /^content/' file.csv





                share|improve this answer






















                  up vote
                  4
                  down vote










                  up vote
                  4
                  down vote









                  awk -F, '$2 !~ /content/' file.csv


                  If you mean "remove lines where the 2nd field starts with "content", then



                  awk -F, '$2 !~ /^content/' file.csv





                  share|improve this answer












                  awk -F, '$2 !~ /content/' file.csv


                  If you mean "remove lines where the 2nd field starts with "content", then



                  awk -F, '$2 !~ /^content/' file.csv






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Sep 13 at 18:23









                  glenn jackman

                  48.2k365105




                  48.2k365105




















                      up vote
                      -1
                      down vote













                      basically what you can do in the easiest way is:



                      cat "/dir/yourfile.csv" | grep -v ",content:" > newcsvfile.csv


                      if you want to use the same file name then you can do:



                      cat "/dir/yourfile.csv" | grep -v ",content:" > tmpfile && mv tmpfile "/dir/yourfile.csv"





                      share|improve this answer
















                      • 2




                        What does this do if "content" is in the third position of a line?
                        – Christian Gibbons
                        Sep 13 at 18:07










                      • that's an assumption if "content:" word will be only in the 2nd field, if it is in different field then the solution would not work and would be different
                        – Javier Salas
                        Sep 13 at 18:10














                      up vote
                      -1
                      down vote













                      basically what you can do in the easiest way is:



                      cat "/dir/yourfile.csv" | grep -v ",content:" > newcsvfile.csv


                      if you want to use the same file name then you can do:



                      cat "/dir/yourfile.csv" | grep -v ",content:" > tmpfile && mv tmpfile "/dir/yourfile.csv"





                      share|improve this answer
















                      • 2




                        What does this do if "content" is in the third position of a line?
                        – Christian Gibbons
                        Sep 13 at 18:07










                      • that's an assumption if "content:" word will be only in the 2nd field, if it is in different field then the solution would not work and would be different
                        – Javier Salas
                        Sep 13 at 18:10












                      up vote
                      -1
                      down vote










                      up vote
                      -1
                      down vote









                      basically what you can do in the easiest way is:



                      cat "/dir/yourfile.csv" | grep -v ",content:" > newcsvfile.csv


                      if you want to use the same file name then you can do:



                      cat "/dir/yourfile.csv" | grep -v ",content:" > tmpfile && mv tmpfile "/dir/yourfile.csv"





                      share|improve this answer












                      basically what you can do in the easiest way is:



                      cat "/dir/yourfile.csv" | grep -v ",content:" > newcsvfile.csv


                      if you want to use the same file name then you can do:



                      cat "/dir/yourfile.csv" | grep -v ",content:" > tmpfile && mv tmpfile "/dir/yourfile.csv"






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Sep 13 at 18:06









                      Javier Salas

                      1115




                      1115







                      • 2




                        What does this do if "content" is in the third position of a line?
                        – Christian Gibbons
                        Sep 13 at 18:07










                      • that's an assumption if "content:" word will be only in the 2nd field, if it is in different field then the solution would not work and would be different
                        – Javier Salas
                        Sep 13 at 18:10












                      • 2




                        What does this do if "content" is in the third position of a line?
                        – Christian Gibbons
                        Sep 13 at 18:07










                      • that's an assumption if "content:" word will be only in the 2nd field, if it is in different field then the solution would not work and would be different
                        – Javier Salas
                        Sep 13 at 18:10







                      2




                      2




                      What does this do if "content" is in the third position of a line?
                      – Christian Gibbons
                      Sep 13 at 18:07




                      What does this do if "content" is in the third position of a line?
                      – Christian Gibbons
                      Sep 13 at 18:07












                      that's an assumption if "content:" word will be only in the 2nd field, if it is in different field then the solution would not work and would be different
                      – Javier Salas
                      Sep 13 at 18:10




                      that's an assumption if "content:" word will be only in the 2nd field, if it is in different field then the solution would not work and would be different
                      – Javier Salas
                      Sep 13 at 18:10

















                       

                      draft saved


                      draft discarded















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f468860%2fremove-the-lines-in-csv-that-contain-specific-pattern-on-the-second-field%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?