基于C#实现文本行移位:删首行及按xyz=true条件删行后移位
Hey there! Let's break down these two text file manipulation tasks clearly:
Depending on your needs, you can use either command-line tools for quick fixes or write C# code for programmatic control:
Command-line quick solutions
- Linux/macOS: Use the
sedcommand to directly modify the file:
Thesed -i '1d' yourfile.txt1dflag tellssedto delete the first line, and-iedits the file in-place. - Windows PowerShell: Skip the first line and overwrite the file:
Get-Content yourfile.txt | Select-Object -Skip 1 | Set-Content yourfile.txt
C# implementation
For small files (simple approach)
Read all lines at once, skip the first one, then write back:
using System.IO; using System.Linq; // ... string[] allLines = File.ReadAllLines("yourfile.txt"); // Skip the first line and convert to array string[] updatedLines = allLines.Skip(1).ToArray(); File.WriteAllLines("yourfile.txt", updatedLines);
For large files (memory-efficient stream approach)
Avoid loading the entire file into memory by using streams:
using System.IO; // ... // Use separate input/output files to avoid issues during writing using (StreamReader reader = new StreamReader("input.txt")) using (StreamWriter writer = new StreamWriter("output.txt")) { // Read and discard the first line reader.ReadLine(); string currentLine; while ((currentLine = reader.ReadLine()) != null) { writer.WriteLine(currentLine); } } // Replace the original file with the updated one File.Delete("input.txt"); File.Move("output.txt", "input.txt");
xyz=true条件的行,再将剩余行向上移位 This requires filtering out lines that match your condition, then writing the remaining lines back. Here are two common approaches:
For small files (concise approach)
Read all lines, filter out unwanted ones, then overwrite the file:
using System.IO; using System.Linq; // ... string[] allLines = File.ReadAllLines("yourfile.txt"); // Filter out lines containing "xyz=true" (case-sensitive) string[] updatedLines = allLines.Where(line => !line.Contains("xyz=true")).ToArray(); File.WriteAllLines("yourfile.txt", updatedLines);
If you need exact line matching (e.g., the entire line is xyz=true, ignoring leading/trailing spaces), adjust the condition:
.Where(line => line.Trim() != "xyz=true")
For large files (stream-based filtering)
Use streams to process the file line by line without loading everything into memory:
using System.IO; // ... using (StreamReader reader = new StreamReader("input.txt")) using (StreamWriter writer = new StreamWriter("output.txt")) { string currentLine; while ((currentLine = reader.ReadLine()) != null) { // Skip lines that meet the condition if (!currentLine.Contains("xyz=true")) { writer.WriteLine(currentLine); } } } // Replace original file File.Delete("input.txt"); File.Move("output.txt", "input.txt");
Bonus: Flexible matching with regex
If you need to handle variations like xyz = true or XYZ=TRUE, use a regular expression:
using System.IO; using System.Text.RegularExpressions; // ... using (StreamReader reader = new StreamReader("input.txt")) using (StreamWriter writer = new StreamWriter("output.txt")) { string currentLine; // Match "xyz" (case-insensitive), optional spaces, =, optional spaces, "true" Regex pattern = new Regex(@"xyz\s*=\s*true", RegexOptions.IgnoreCase); while ((currentLine = reader.ReadLine()) != null) { if (!pattern.IsMatch(currentLine)) { writer.WriteLine(currentLine); } } } File.Delete("input.txt"); File.Move("output.txt", "input.txt");
内容的提问来源于stack exchange,提问作者swifty

