Removing specified text from CSV fileHow to create Excel (.XLS and .XLSX) file in C# without installing Ms Office?How to concatenate text from multiple rows into a single text string in SQL server?How to output MySQL query results in CSV format?How do I get a consistent byte representation of strings in C# without manually specifying an encoding?Dealing with commas in a CSV fileGet int value from enum in C#Save PL/pgSQL output from PostgreSQL to a CSV fileHow to import CSV file data into a PostgreSQL table?Excel to CSV with UTF8 encodingPandas writing dataframe to CSV file

Can a monster with multiattack use this ability if they are missing a limb?

Cynical novel that describes an America ruled by the media, arms manufacturers, and ethnic figureheads

How does it work when somebody invests in my business?

Your magic is very sketchy

How can I use the arrow sign in my bash prompt?

Why does John Bercow say “unlock” after reading out the results of a vote?

Where in the Bible does the greeting ("Dominus Vobiscum") used at Mass come from?

Is there a problem with hiding "forgot password" until it's needed?

Coordinate position not precise

Trouble understanding overseas colleagues

Was the picture area of a CRT a parallelogram (instead of a true rectangle)?

Is there any easy technique written in Bhagavad GITA to control lust?

Increase performance creating Mandelbrot set in python

Is the destination of a commercial flight important for the pilot?

Should my PhD thesis be submitted under my legal name?

How can I replace every global instance of "x[2]" with "x_2"

How do I keep an essay about "feeling flat" from feeling flat?

Time travel short story where a man arrives in the late 19th century in a time machine and then sends the machine back into the past

What would happen if the UK refused to take part in EU Parliamentary elections?

Go Pregnant or Go Home

Is there an Impartial Brexit Deal comparison site?

Efficiently merge handle parallel feature branches in SFDX

Why "be dealt cards" rather than "be dealing cards"?

How does residential electricity work?



Removing specified text from CSV file


How to create Excel (.XLS and .XLSX) file in C# without installing Ms Office?How to concatenate text from multiple rows into a single text string in SQL server?How to output MySQL query results in CSV format?How do I get a consistent byte representation of strings in C# without manually specifying an encoding?Dealing with commas in a CSV fileGet int value from enum in C#Save PL/pgSQL output from PostgreSQL to a CSV fileHow to import CSV file data into a PostgreSQL table?Excel to CSV with UTF8 encodingPandas writing dataframe to CSV file













2















it's my first attempt at doing this and I have no idea if I'm on the right lines.



Basically I want to remove text from a CSV file that contains a specific keyword but I can't figure out how to remove the line.



 static void Main(string[] args)

var searchItem = "running";

var lines = File.ReadLines("C://Users//Pete//Desktop//testdata.csv");

foreach (string line in lines)

if (line.Contains(searchItem))

//Remove line here?












share|improve this question






















  • Would you like to remove whole line?

    – koviroli
    Mar 8 at 9:50















2















it's my first attempt at doing this and I have no idea if I'm on the right lines.



Basically I want to remove text from a CSV file that contains a specific keyword but I can't figure out how to remove the line.



 static void Main(string[] args)

var searchItem = "running";

var lines = File.ReadLines("C://Users//Pete//Desktop//testdata.csv");

foreach (string line in lines)

if (line.Contains(searchItem))

//Remove line here?












share|improve this question






















  • Would you like to remove whole line?

    – koviroli
    Mar 8 at 9:50













2












2








2








it's my first attempt at doing this and I have no idea if I'm on the right lines.



Basically I want to remove text from a CSV file that contains a specific keyword but I can't figure out how to remove the line.



 static void Main(string[] args)

var searchItem = "running";

var lines = File.ReadLines("C://Users//Pete//Desktop//testdata.csv");

foreach (string line in lines)

if (line.Contains(searchItem))

//Remove line here?












share|improve this question














it's my first attempt at doing this and I have no idea if I'm on the right lines.



Basically I want to remove text from a CSV file that contains a specific keyword but I can't figure out how to remove the line.



 static void Main(string[] args)

var searchItem = "running";

var lines = File.ReadLines("C://Users//Pete//Desktop//testdata.csv");

foreach (string line in lines)

if (line.Contains(searchItem))

//Remove line here?









c# csv foreach






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 8 at 9:45









p.lowers2p.lowers2

132




132












  • Would you like to remove whole line?

    – koviroli
    Mar 8 at 9:50

















  • Would you like to remove whole line?

    – koviroli
    Mar 8 at 9:50
















Would you like to remove whole line?

– koviroli
Mar 8 at 9:50





Would you like to remove whole line?

– koviroli
Mar 8 at 9:50












4 Answers
4






active

oldest

votes


















0














The simple way if you'd like to remove a whole line:



 var searchItem = "running";
var pathToYourFile = @"C://Users//Pete//Desktop//testdata.csv";
var lines = File.ReadAllLines(pathToYourFile);
lines = lines.Where(line => !line.Contains(searchItem)).ToArray();
File.WriteAllLines(pathToYourFile, lines);


For multiple search items:



 var searchItems = "running;walking;waiting;any";
var pathToYourFile = @"....items.csv";
var lines = File.ReadAllLines(pathToYourFile);
// split with your separator, actually is ';' character
foreach(var searchItem in searchItems.Split(';'))
lines = lines.Where(line =>!line.Contains(searchItem)).ToArray();

File.WriteAllLines(pathToYourFile, lines);





share|improve this answer

























  • Hi, can I then modify this to say do multiple pharases? E..g. search item = "running", "walking"

    – p.lowers2
    Mar 8 at 10:10











  • It wouldn't matter, it's just to remove multiple words at once.

    – p.lowers2
    Mar 8 at 10:20











  • Perfect thanks! One last question, how would I go about numbering. Would it just be var searchItem = 0-9 ?

    – p.lowers2
    Mar 8 at 10:26











  • yes, I modified my answer with multiple search items, check it @p.lowers2

    – koviroli
    Mar 8 at 10:26












  • Hi, so for example it will say in my csv "jogging":20192 and I just want to remove the numbers.

    – p.lowers2
    Mar 8 at 10:33


















1














Try this one to remove one or a few multiple words.



static void sd(string[] args)

string contents = File.ReadAllText("C://Users//Pete//Desktop//testdata.csv");

string output = contents.Replace("running", string.Empty).Replace("replaceThis", string.Empty).Replace("replaceThisToo", string.Empty);
//string output = contents.Replace("a", "b").Replace("b", "c").Replace("c", "d");



To remove multiple string, you can use this...



static void Main(string[] args)

string[] removeTheseWords = "aaa", "bbb", "ccc" ;

string contents = File.ReadAllText("C://Users//Pete//Desktop//testdata.csv");
string output = string.Empty;

foreach (string value in removeTheseWords)

output = contents.Replace(value, string.Empty);




More info: https://docs.microsoft.com/en-us/dotnet/api/system.string.replace






share|improve this answer




















  • 1





    what if OP want to remove 100 search term? Is OP need to write .Replace function 100 times?

    – er-sho
    Mar 8 at 10:32












  • @er-sho Updated. :)

    – DxTx
    Mar 8 at 10:43


















0














if you are using foreach and removing from lines its will through an exception called collection modified exception so go with for



for(int i=lines.Count - 1; i > -1; i--)

if (lines[i].Contains(searchItem))

lines.RemoveAt(i);







share|improve this answer
































    0














    You don't need to remove line just skip those lines that contain your search term



    foreach (string line in lines)

    if (!line.Contains(searchItem)) //<= Notice here I added exclamation mark (!)

    //Do your work when line does not contains search term

    else

    //Do something if line contains search term




    Or alternative is to filtered your lines that does not contains your search term before loop like



    lines = lines.Where(line => !line.Contains(searchItem));

    foreach (string line in lines)

    //Here are those line that does not contain search term



    If your search term contains multiple words separated with comma(,) then you can skip those line by



    lines = lines.Where(line => searchItem.Split(',').All(term => !line.Contains(term)));





    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%2f55060515%2fremoving-specified-text-from-csv-file%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      The simple way if you'd like to remove a whole line:



       var searchItem = "running";
      var pathToYourFile = @"C://Users//Pete//Desktop//testdata.csv";
      var lines = File.ReadAllLines(pathToYourFile);
      lines = lines.Where(line => !line.Contains(searchItem)).ToArray();
      File.WriteAllLines(pathToYourFile, lines);


      For multiple search items:



       var searchItems = "running;walking;waiting;any";
      var pathToYourFile = @"....items.csv";
      var lines = File.ReadAllLines(pathToYourFile);
      // split with your separator, actually is ';' character
      foreach(var searchItem in searchItems.Split(';'))
      lines = lines.Where(line =>!line.Contains(searchItem)).ToArray();

      File.WriteAllLines(pathToYourFile, lines);





      share|improve this answer

























      • Hi, can I then modify this to say do multiple pharases? E..g. search item = "running", "walking"

        – p.lowers2
        Mar 8 at 10:10











      • It wouldn't matter, it's just to remove multiple words at once.

        – p.lowers2
        Mar 8 at 10:20











      • Perfect thanks! One last question, how would I go about numbering. Would it just be var searchItem = 0-9 ?

        – p.lowers2
        Mar 8 at 10:26











      • yes, I modified my answer with multiple search items, check it @p.lowers2

        – koviroli
        Mar 8 at 10:26












      • Hi, so for example it will say in my csv "jogging":20192 and I just want to remove the numbers.

        – p.lowers2
        Mar 8 at 10:33















      0














      The simple way if you'd like to remove a whole line:



       var searchItem = "running";
      var pathToYourFile = @"C://Users//Pete//Desktop//testdata.csv";
      var lines = File.ReadAllLines(pathToYourFile);
      lines = lines.Where(line => !line.Contains(searchItem)).ToArray();
      File.WriteAllLines(pathToYourFile, lines);


      For multiple search items:



       var searchItems = "running;walking;waiting;any";
      var pathToYourFile = @"....items.csv";
      var lines = File.ReadAllLines(pathToYourFile);
      // split with your separator, actually is ';' character
      foreach(var searchItem in searchItems.Split(';'))
      lines = lines.Where(line =>!line.Contains(searchItem)).ToArray();

      File.WriteAllLines(pathToYourFile, lines);





      share|improve this answer

























      • Hi, can I then modify this to say do multiple pharases? E..g. search item = "running", "walking"

        – p.lowers2
        Mar 8 at 10:10











      • It wouldn't matter, it's just to remove multiple words at once.

        – p.lowers2
        Mar 8 at 10:20











      • Perfect thanks! One last question, how would I go about numbering. Would it just be var searchItem = 0-9 ?

        – p.lowers2
        Mar 8 at 10:26











      • yes, I modified my answer with multiple search items, check it @p.lowers2

        – koviroli
        Mar 8 at 10:26












      • Hi, so for example it will say in my csv "jogging":20192 and I just want to remove the numbers.

        – p.lowers2
        Mar 8 at 10:33













      0












      0








      0







      The simple way if you'd like to remove a whole line:



       var searchItem = "running";
      var pathToYourFile = @"C://Users//Pete//Desktop//testdata.csv";
      var lines = File.ReadAllLines(pathToYourFile);
      lines = lines.Where(line => !line.Contains(searchItem)).ToArray();
      File.WriteAllLines(pathToYourFile, lines);


      For multiple search items:



       var searchItems = "running;walking;waiting;any";
      var pathToYourFile = @"....items.csv";
      var lines = File.ReadAllLines(pathToYourFile);
      // split with your separator, actually is ';' character
      foreach(var searchItem in searchItems.Split(';'))
      lines = lines.Where(line =>!line.Contains(searchItem)).ToArray();

      File.WriteAllLines(pathToYourFile, lines);





      share|improve this answer















      The simple way if you'd like to remove a whole line:



       var searchItem = "running";
      var pathToYourFile = @"C://Users//Pete//Desktop//testdata.csv";
      var lines = File.ReadAllLines(pathToYourFile);
      lines = lines.Where(line => !line.Contains(searchItem)).ToArray();
      File.WriteAllLines(pathToYourFile, lines);


      For multiple search items:



       var searchItems = "running;walking;waiting;any";
      var pathToYourFile = @"....items.csv";
      var lines = File.ReadAllLines(pathToYourFile);
      // split with your separator, actually is ';' character
      foreach(var searchItem in searchItems.Split(';'))
      lines = lines.Where(line =>!line.Contains(searchItem)).ToArray();

      File.WriteAllLines(pathToYourFile, lines);






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Mar 8 at 10:26

























      answered Mar 8 at 9:56









      kovirolikoviroli

      683516




      683516












      • Hi, can I then modify this to say do multiple pharases? E..g. search item = "running", "walking"

        – p.lowers2
        Mar 8 at 10:10











      • It wouldn't matter, it's just to remove multiple words at once.

        – p.lowers2
        Mar 8 at 10:20











      • Perfect thanks! One last question, how would I go about numbering. Would it just be var searchItem = 0-9 ?

        – p.lowers2
        Mar 8 at 10:26











      • yes, I modified my answer with multiple search items, check it @p.lowers2

        – koviroli
        Mar 8 at 10:26












      • Hi, so for example it will say in my csv "jogging":20192 and I just want to remove the numbers.

        – p.lowers2
        Mar 8 at 10:33

















      • Hi, can I then modify this to say do multiple pharases? E..g. search item = "running", "walking"

        – p.lowers2
        Mar 8 at 10:10











      • It wouldn't matter, it's just to remove multiple words at once.

        – p.lowers2
        Mar 8 at 10:20











      • Perfect thanks! One last question, how would I go about numbering. Would it just be var searchItem = 0-9 ?

        – p.lowers2
        Mar 8 at 10:26











      • yes, I modified my answer with multiple search items, check it @p.lowers2

        – koviroli
        Mar 8 at 10:26












      • Hi, so for example it will say in my csv "jogging":20192 and I just want to remove the numbers.

        – p.lowers2
        Mar 8 at 10:33
















      Hi, can I then modify this to say do multiple pharases? E..g. search item = "running", "walking"

      – p.lowers2
      Mar 8 at 10:10





      Hi, can I then modify this to say do multiple pharases? E..g. search item = "running", "walking"

      – p.lowers2
      Mar 8 at 10:10













      It wouldn't matter, it's just to remove multiple words at once.

      – p.lowers2
      Mar 8 at 10:20





      It wouldn't matter, it's just to remove multiple words at once.

      – p.lowers2
      Mar 8 at 10:20













      Perfect thanks! One last question, how would I go about numbering. Would it just be var searchItem = 0-9 ?

      – p.lowers2
      Mar 8 at 10:26





      Perfect thanks! One last question, how would I go about numbering. Would it just be var searchItem = 0-9 ?

      – p.lowers2
      Mar 8 at 10:26













      yes, I modified my answer with multiple search items, check it @p.lowers2

      – koviroli
      Mar 8 at 10:26






      yes, I modified my answer with multiple search items, check it @p.lowers2

      – koviroli
      Mar 8 at 10:26














      Hi, so for example it will say in my csv "jogging":20192 and I just want to remove the numbers.

      – p.lowers2
      Mar 8 at 10:33





      Hi, so for example it will say in my csv "jogging":20192 and I just want to remove the numbers.

      – p.lowers2
      Mar 8 at 10:33













      1














      Try this one to remove one or a few multiple words.



      static void sd(string[] args)

      string contents = File.ReadAllText("C://Users//Pete//Desktop//testdata.csv");

      string output = contents.Replace("running", string.Empty).Replace("replaceThis", string.Empty).Replace("replaceThisToo", string.Empty);
      //string output = contents.Replace("a", "b").Replace("b", "c").Replace("c", "d");



      To remove multiple string, you can use this...



      static void Main(string[] args)

      string[] removeTheseWords = "aaa", "bbb", "ccc" ;

      string contents = File.ReadAllText("C://Users//Pete//Desktop//testdata.csv");
      string output = string.Empty;

      foreach (string value in removeTheseWords)

      output = contents.Replace(value, string.Empty);




      More info: https://docs.microsoft.com/en-us/dotnet/api/system.string.replace






      share|improve this answer




















      • 1





        what if OP want to remove 100 search term? Is OP need to write .Replace function 100 times?

        – er-sho
        Mar 8 at 10:32












      • @er-sho Updated. :)

        – DxTx
        Mar 8 at 10:43















      1














      Try this one to remove one or a few multiple words.



      static void sd(string[] args)

      string contents = File.ReadAllText("C://Users//Pete//Desktop//testdata.csv");

      string output = contents.Replace("running", string.Empty).Replace("replaceThis", string.Empty).Replace("replaceThisToo", string.Empty);
      //string output = contents.Replace("a", "b").Replace("b", "c").Replace("c", "d");



      To remove multiple string, you can use this...



      static void Main(string[] args)

      string[] removeTheseWords = "aaa", "bbb", "ccc" ;

      string contents = File.ReadAllText("C://Users//Pete//Desktop//testdata.csv");
      string output = string.Empty;

      foreach (string value in removeTheseWords)

      output = contents.Replace(value, string.Empty);




      More info: https://docs.microsoft.com/en-us/dotnet/api/system.string.replace






      share|improve this answer




















      • 1





        what if OP want to remove 100 search term? Is OP need to write .Replace function 100 times?

        – er-sho
        Mar 8 at 10:32












      • @er-sho Updated. :)

        – DxTx
        Mar 8 at 10:43













      1












      1








      1







      Try this one to remove one or a few multiple words.



      static void sd(string[] args)

      string contents = File.ReadAllText("C://Users//Pete//Desktop//testdata.csv");

      string output = contents.Replace("running", string.Empty).Replace("replaceThis", string.Empty).Replace("replaceThisToo", string.Empty);
      //string output = contents.Replace("a", "b").Replace("b", "c").Replace("c", "d");



      To remove multiple string, you can use this...



      static void Main(string[] args)

      string[] removeTheseWords = "aaa", "bbb", "ccc" ;

      string contents = File.ReadAllText("C://Users//Pete//Desktop//testdata.csv");
      string output = string.Empty;

      foreach (string value in removeTheseWords)

      output = contents.Replace(value, string.Empty);




      More info: https://docs.microsoft.com/en-us/dotnet/api/system.string.replace






      share|improve this answer















      Try this one to remove one or a few multiple words.



      static void sd(string[] args)

      string contents = File.ReadAllText("C://Users//Pete//Desktop//testdata.csv");

      string output = contents.Replace("running", string.Empty).Replace("replaceThis", string.Empty).Replace("replaceThisToo", string.Empty);
      //string output = contents.Replace("a", "b").Replace("b", "c").Replace("c", "d");



      To remove multiple string, you can use this...



      static void Main(string[] args)

      string[] removeTheseWords = "aaa", "bbb", "ccc" ;

      string contents = File.ReadAllText("C://Users//Pete//Desktop//testdata.csv");
      string output = string.Empty;

      foreach (string value in removeTheseWords)

      output = contents.Replace(value, string.Empty);




      More info: https://docs.microsoft.com/en-us/dotnet/api/system.string.replace







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Mar 8 at 10:42

























      answered Mar 8 at 10:30









      DxTxDxTx

      1,5431822




      1,5431822







      • 1





        what if OP want to remove 100 search term? Is OP need to write .Replace function 100 times?

        – er-sho
        Mar 8 at 10:32












      • @er-sho Updated. :)

        – DxTx
        Mar 8 at 10:43












      • 1





        what if OP want to remove 100 search term? Is OP need to write .Replace function 100 times?

        – er-sho
        Mar 8 at 10:32












      • @er-sho Updated. :)

        – DxTx
        Mar 8 at 10:43







      1




      1





      what if OP want to remove 100 search term? Is OP need to write .Replace function 100 times?

      – er-sho
      Mar 8 at 10:32






      what if OP want to remove 100 search term? Is OP need to write .Replace function 100 times?

      – er-sho
      Mar 8 at 10:32














      @er-sho Updated. :)

      – DxTx
      Mar 8 at 10:43





      @er-sho Updated. :)

      – DxTx
      Mar 8 at 10:43











      0














      if you are using foreach and removing from lines its will through an exception called collection modified exception so go with for



      for(int i=lines.Count - 1; i > -1; i--)

      if (lines[i].Contains(searchItem))

      lines.RemoveAt(i);







      share|improve this answer





























        0














        if you are using foreach and removing from lines its will through an exception called collection modified exception so go with for



        for(int i=lines.Count - 1; i > -1; i--)

        if (lines[i].Contains(searchItem))

        lines.RemoveAt(i);







        share|improve this answer



























          0












          0








          0







          if you are using foreach and removing from lines its will through an exception called collection modified exception so go with for



          for(int i=lines.Count - 1; i > -1; i--)

          if (lines[i].Contains(searchItem))

          lines.RemoveAt(i);







          share|improve this answer















          if you are using foreach and removing from lines its will through an exception called collection modified exception so go with for



          for(int i=lines.Count - 1; i > -1; i--)

          if (lines[i].Contains(searchItem))

          lines.RemoveAt(i);








          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Mar 8 at 10:00

























          answered Mar 8 at 9:54









          AvinashAvinash

          1129




          1129





















              0














              You don't need to remove line just skip those lines that contain your search term



              foreach (string line in lines)

              if (!line.Contains(searchItem)) //<= Notice here I added exclamation mark (!)

              //Do your work when line does not contains search term

              else

              //Do something if line contains search term




              Or alternative is to filtered your lines that does not contains your search term before loop like



              lines = lines.Where(line => !line.Contains(searchItem));

              foreach (string line in lines)

              //Here are those line that does not contain search term



              If your search term contains multiple words separated with comma(,) then you can skip those line by



              lines = lines.Where(line => searchItem.Split(',').All(term => !line.Contains(term)));





              share|improve this answer





























                0














                You don't need to remove line just skip those lines that contain your search term



                foreach (string line in lines)

                if (!line.Contains(searchItem)) //<= Notice here I added exclamation mark (!)

                //Do your work when line does not contains search term

                else

                //Do something if line contains search term




                Or alternative is to filtered your lines that does not contains your search term before loop like



                lines = lines.Where(line => !line.Contains(searchItem));

                foreach (string line in lines)

                //Here are those line that does not contain search term



                If your search term contains multiple words separated with comma(,) then you can skip those line by



                lines = lines.Where(line => searchItem.Split(',').All(term => !line.Contains(term)));





                share|improve this answer



























                  0












                  0








                  0







                  You don't need to remove line just skip those lines that contain your search term



                  foreach (string line in lines)

                  if (!line.Contains(searchItem)) //<= Notice here I added exclamation mark (!)

                  //Do your work when line does not contains search term

                  else

                  //Do something if line contains search term




                  Or alternative is to filtered your lines that does not contains your search term before loop like



                  lines = lines.Where(line => !line.Contains(searchItem));

                  foreach (string line in lines)

                  //Here are those line that does not contain search term



                  If your search term contains multiple words separated with comma(,) then you can skip those line by



                  lines = lines.Where(line => searchItem.Split(',').All(term => !line.Contains(term)));





                  share|improve this answer















                  You don't need to remove line just skip those lines that contain your search term



                  foreach (string line in lines)

                  if (!line.Contains(searchItem)) //<= Notice here I added exclamation mark (!)

                  //Do your work when line does not contains search term

                  else

                  //Do something if line contains search term




                  Or alternative is to filtered your lines that does not contains your search term before loop like



                  lines = lines.Where(line => !line.Contains(searchItem));

                  foreach (string line in lines)

                  //Here are those line that does not contain search term



                  If your search term contains multiple words separated with comma(,) then you can skip those line by



                  lines = lines.Where(line => searchItem.Split(',').All(term => !line.Contains(term)));






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Mar 8 at 10:28

























                  answered Mar 8 at 9:50









                  er-shoer-sho

                  6,6502619




                  6,6502619



























                      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%2f55060515%2fremoving-specified-text-from-csv-file%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

                      Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme

                      Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

                      2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived