Remove line from a file [duplicate] - c#

This question already has answers here:
How to delete a line from a text file in C#?
(11 answers)
Closed 8 years ago.
I tried to remove a line from text file which contains a particular word. However I need a better approach to delete the line.
Here is the code I used:
var oldLines = System.IO.File.ReadAllLines(filepath);
var newLines = oldLines.Where(line1 => !line1.Contains("Value"));
System.IO.File.WriteAllLines(filepath, newLines);
By using this my task was completed, but with the following performance issues: If the file contains 10 lakhs lines it must read 10lakhs line and write 10 lakhs lines, so it takes more time.

string line = null;
string line_to_delete = "sample line i want to delete";
using (StreamReader reader = new StreamReader("C:\\input")) {
using (StreamWriter writer = new StreamWriter("C:\\output")) {
while ((line = reader.ReadLine()) != null) {
if (String.Compare(line, line_to_delete) == 0)
continue;
writer.WriteLine(line);
}
}
}

Related

How to make String.Split() work on the new line? [duplicate]

This question already has answers here:
split a string on newlines in .NET
(17 answers)
Closed 3 months ago.
string candidates;
string[] candidatesSplit = { };
string line;
int countLines = 0;
StreamReader sr = new StreamReader("..\\..\\..\\candidates.txt"); // Read candidates from file
candidates = sr.ReadToEnd();
sr.Close();
candidatesSplit = candidates.Split(','); // Split the file with ','
Console.WriteLine(candidatesSplit[30]);
Using this code, I wanted to split every ',' and get specific words out from my text file.
My candidates file looks like this:
100,Esra Tarak,90,D1,D4,D2,A,B,D,C, ,C,A,D,B,C,D,B,A, ,B,A,C,D,C,D,A,D,B,C,D
101,Cem Ak,84,D1,D5, ,A,C,D,C,C,C,A,C,B,C,D,B,A,C,B,A,C,D,C,C,A,D,B,C,D
Code works perfectly for the first line in candidates.txt, however when it comes to the second line on the text file, the output comes out like this:
D
101
I need it to show only like this
101
I cannot put a ',' at the end of my lines. Is there any way to fix this?
Just Split(Environment.NewLine) on the entire input first and then Split(',') again on each line.
using var sr = new StreamReader("..\\..\\..\\candidates.txt");
var candidates = sr.ReadToEnd();
foreach (var line in candidates.Split(Environment.NewLine))
{
var candidatesSplit = line.Split(',');
Console.WriteLine(candidatesSplit[30]);
}

Searching for a word in a text file is failing in C# [duplicate]

This question already has answers here:
Return StreamReader to Beginning
(8 answers)
Closed 5 years ago.
Please have a look at the below code.
DirectoryInfo directory = new DirectoryInfo(#"XXXXXX \IN");
FileInfo[] files = directory.GetFiles("*.*");
foreach (var f in files) //FETCHING FILES FROM THE BULK FOLDER (IN)
{
string path_f = f.FullName;
StreamReader myfile = new StreamReader(path_f);
StreamReader basis = new StreamReader(#"C:XXXXXXXX\SCH.TXT");
while (basis.EndOfStream == false)
{
string canon = basis.ReadLine(); //CANON STORES EACH WORD FROM SCH.TXT
canon = canon.Trim();
while (myfile.EndOfStream == false)
{
string line = myfile.ReadLine();
if (line.Contains(canon))
DISCH_COUNT++;
} // END OF WHILE
} // END OF WHILE
basis.Close();
}
myfile.close();
SCH.TXT contains some words. I need to search how many times these words appeared in the a file. I am picking up each word from the SCH.txt and searching in the file.My issue is for the first word, the search is done finley. From the second word onwards the condition myfile.EndOfStream == false is getting failed. Can you please help me regarding this..??
try this for reading text content line by line
int counter = 0;
string line;
// Read the file and display it line by line.
System.IO.StreamReader file =
new System.IO.StreamReader("c:\\test.txt");
while((line = file.ReadLine()) != null)
{
Console.WriteLine (line);
counter++;
}
file.Close();
I haven't test the code, but it will give you the basic idea that how can you achieve the search functionality. Good luck

CSV file contents to two dimensional array [duplicate]

This question already has answers here:
Parsing CSV files in C#, with header
(19 answers)
Closed 8 years ago.
This question has been asked several times with different inputs so I thought of reposting it with my requirement.
I have a CSV file which contents string fields in the way given below.
idnum,name1, name2,groupid
idnum,name1, name2,groupid
idnum,name1, name2,groupid
example
s001,sahil,payap,gid0
s002,Amir,Khan,gid02
d003,hrithik,roshan,gid03
I have two dimensional string array. I want to read row by row to my two dimensional array.
When it read it should be like this
arr[0][0]=s001
arr[0][1]=name1
arr[0][2]=name2
arr[0][3]=gid01
arr[1][0]=s002
arr[1][1]=Amir
arr[1][2]=Khan
arr[1][3]=gid04
there are 40 records in a file and it should read till the end of the file.
I need to implement this in C#
Any code sample or any explanation would be great help.
I have no knowledge in csv file handling so please don't ask what did you try, at least if you could give me a code sample for reading just one string for a variable it would be a great help.
And please don't ask to go for another solution.
Thanks.
The simplest way to read a csv file in the way you suggest is probably:
var rows = File.ReadAllLines("myfile.csv").Select(l => l.Split(',').ToArray()).ToArray();
Then:
Console.WriteLine(rows[0][0]); // Will output s001
Console.WriteLine(rows[0][1]); // Will output sahil
Console.WriteLine(rows[0][2]); // Will output payap
Console.WriteLine(rows[0][3]); // Will output gid0
Console.WriteLine(rows[1][0]); // Will output s002
Console.WriteLine(rows[2][0]); // Will output d003
The file would have to be read in line-wise. Each line would have to be separated using String.Split. Then the resulting strings would have to be trimmed using Trim, and finally would have to be written into the respective columns of the current row. However I totally second the comments above; more convenient would be to use some class or struct called Person and then to parse into a List<Person>.
The reading could be done as follows:
String line = String.Empty;
System.IO.StreamReader file = new System.IO.StreamReader("c:\\file.txt");
while((line = file.ReadLine()) != null)
{
String[] parts_of_line = line.Split(',')
for ( int i = 0; i < parts_of_line.Length; i++ )
parts_of_line[i] = parts_of_line[i].Trim();
// do with the parts of the line whatever you like
}
You can do that using the CsvHelper library:
const string Csv = #"s001,sahil,payap,gid0
s002,Amir,Khan,gid02
d003,hrithik,roshan,gid03";
var rows = new List<string[]>();
string[] row;
using (var stringReader = new StringReader(Csv))
using (var parser = new CsvParser(stringReader))
while ((row = parser.Read()) != null)
{
rows.Add(row);
}
Console.WriteLine(rows[0][0]); // Will output s001
Console.WriteLine(rows[0][1]); // Will output sahil
Console.WriteLine(rows[0][2]); // Will output payap
Console.WriteLine(rows[0][3]); // Will output gid0
Console.WriteLine(rows[1][0]); // Will output s002
Console.WriteLine(rows[2][0]); // Will output d003
For a working example, check out this .NET fiddle: http://dotnetfiddle.net/PLPXo8
If you want to read directly from file, you can do this:
var rows = new List<string[]>();
string[] row;
using (var parser = new CsvParser(File.OpenText(#"c:\test.csv")))
while ((row = parser.Read()) != null)
{
rows.Add(row);
}

Read and extract from file

I have a huge file with ~3 mill rows. Every line contains record like this:
1|2|3|4|5|6|7|8|9
Exactly 8 separators like '|' on every line. I am looking for a way to read this file then extract last '9' number only from every line and store it into another file.
edit:
Ok here is what i done already.
using (StreamReader sr = new StreamReader(filepath))
using (StreamWriter sw = new StreamWriter(filepath1))
{
string line = null;
while ((line = sr.ReadLine()) != null)
sw.WriteLine(line.Split('|')[8]);
}
File.WriteAllLines("filepath", File.ReadAllLines(filepath).Where(l => !string.IsNullOrWhiteSpace(l)));
Read file, extract last digits then write in new file and clear blank lines. Last digit is 10-15 symbols and I want to extract first 6. I continue to read and try some and when I'm done or have some question I'll edit again.
Thanks
Edit 2:
Ok, here I take first 8 digits from the number:
sw.WriteLine(line.Substring(0, Math.Min(line.Length, 8)));
Edit 3:
I have no idea how can I match now every numbers that left in file. I want to match them and to see witch number how many times is in the file.
Any help?
I am looking for a way to read this file then extract last [..] number only from every line and store it into another file.
What part exactly are you having trouble with? In psuedo code, this is what you want:
fileReader = OpenFile("input")
fileWriter = OpenFile("output")
while !fileReader.EndOfFile
line = fileReader.ReadLine
records[] = line.Split('|')
value = records[8]
fileWriter.WriteLine(value)
do
So start implementing it and feel free to ask a question on any specific line you're having trouble with. Each line of code I posted contains enough pointers to figure out the C# code or the terms to do a web search for it.
You don't say where you are stuck. Break the problem down:
Write and run minimal C# program
Read lines from file
Break up one line
write result line to a file
Are you stuck on any one of those? Then ask a specific question about that. This decomposition technique is key to many programming tasks, and indeed complex tasks in general.
You might find the string split capability useful.
Because it's a huge file you must read it line by line!
public IEnumerable ReadFileIterator(String filePath)
{
using (StreamReader streamReader = new StreamReader(filePath, Encoding.Default))
{
String line;
while ((line = streamReader.ReadLine()) != null)
{
yield return line;
}
yield break;
}
}
public void WriteToFile(String inputFilePath, String outputFilePath)
{
using (StreamWriter streamWriter = new StreamWriter(outputFilePath, true, Encoding.Default))
{
foreach (String line in ReadFileIterator(inputFilePath))
{
String[] subStrings = line.Split('|');
streamWriter.WriteLine(subStrings[8]);
}
streamWriter.Flush();
streamWriter.Close();
}
}
using (StreamReader sr = new StreamReader("input"))
using (StreamWriter sw = new StreamWriter("output"))
{
string line = null;
while ((line=sr.ReadLine())!=null)
sw.WriteLine(line.Split('|')[8]);
}
Some pointer to start from: StreamReader.Readline() and String.Split(). There are examples on both pages.
With LINQ you could do a thing like the following to filter the numbers:
var numbers = from l in File.ReadLines(fileName)
let p = l.Split('|')
select p[8];
and then write them into a new file like that:
File.WriteAllText(newFileName, String.Join("\r\n", numbers));
Use String.Split() to get the line inside an array and get the last element and store it into another file. Repeat the process for each line.
Try this...
// Read the file and display it line by line.
System.IO.StreamReader file =
new System.IO.StreamReader("c:\\test.txt");
while((line = file.ReadLine()) != null)
{
string[] words = s.Split('|');
string value = words [8]
Console.WriteLine (value);
}
file.Close();

How to delete string from txt file? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Efficient way to delete a line from a text file
I have a multithreaded app, and a text file containing a list of proxy servers. If a proxy is not valid, I need to delete it from text file.
How to do it, if I don't want to lose speed of app?
For example I need to delete 62.109.29.58:8085
from
62.109.7.22:8085
62.109.0.35:8085
92.63.106.111:8085
78.24.216.163:8085
92.63.97.156:8085
82.146.56.156:8085
62.109.29.58:8085
78.24.220.173:8085
78.24.220.111:8085
92.63.106.124:8085
Since your file appears small, you can read the whole file in, remove the line that you must remove, and then write the file back:
File.WriteAllLines("myfile.txt"
, File.ReadLines("myfile.txt").Where(s => s != "62.109.29.58:8085").ToList()
);
string[] lines = System.IO.File.ReadAllLines("filename.txt");
for (int i = 0; i < lines.Length; i++)
{
string line = lines[i];
if (line == "what you are looking for")
lines[i] = "";
}
string[] newLines = lines.Where(str => !string.IsNullOrEmpty(str)).ToArray();
using (Stream stream = File.OpenWrite("filename.txt"))
{
using (StreamWriter sw = new StreamWriter(stream))
{
foreach (string line in newLines)
{
sw.WriteLine(line);
}
}
}

Categories

Resources