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
1
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
    5 mins ago














up vote
1
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
    5 mins ago












up vote
1
down vote

favorite









up vote
1
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 3 hours ago









Jeff Schaller

32.3k849109




32.3k849109










asked 4 hours ago









yael

2,0361145




2,0361145











  • 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
    5 mins ago
















  • 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
    5 mins ago















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
5 mins ago




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
5 mins ago










3 Answers
3






active

oldest

votes

















up vote
5
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
        3 hours ago










      • 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
        3 hours ago










      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
      5
      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
        5
        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
          5
          down vote



          accepted







          up vote
          5
          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 2 hours ago

























          answered 3 hours ago









          αғsнιη

          15.6k92563




          15.6k92563






















              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 3 hours ago









                  glenn jackman

                  47.4k265103




                  47.4k265103




















                      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
                        3 hours ago










                      • 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
                        3 hours ago














                      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
                        3 hours ago










                      • 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
                        3 hours ago












                      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 3 hours ago









                      Javier Salas

                      1115




                      1115







                      • 2




                        What does this do if "content" is in the third position of a line?
                        – Christian Gibbons
                        3 hours ago










                      • 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
                        3 hours ago












                      • 2




                        What does this do if "content" is in the third position of a line?
                        – Christian Gibbons
                        3 hours ago










                      • 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
                        3 hours ago







                      2




                      2




                      What does this do if "content" is in the third position of a line?
                      – Christian Gibbons
                      3 hours ago




                      What does this do if "content" is in the third position of a line?
                      – Christian Gibbons
                      3 hours ago












                      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
                      3 hours ago




                      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
                      3 hours ago

















                       

                      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













































































                      Comments

                      Popular posts from this blog

                      Long meetings (6-7 hours a day): Being “babysat” by supervisor

                      Is the Concept of Multiple Fantasy Races Scientifically Flawed? [closed]

                      Confectionery