Modify File Contents based on starting element/string <p>I have a HL7 File like:</p> <pre><code>MSH|^~\&amp;|ABC|000|ABC|ABC|0000||ABC|000|A|00 PID|1|000|||ABC||000|A|||||||||| OBR|1|||00||00|00|||||||||||ABC|00|0|0||||A|||||00||ABC|7ABC||ABC OBX|1|ABC|ABC|1|SGVsbG8= OBX|2|ABC|ABC|1|XYTA OBX|3|ABC|ABC|1|UYYA </code></pre> <p>I have to read only OBX segments and get the text after 5th pipe (|).</p> <p>Currently I am doing this with:</p> <pre><code> StreamReader reader = new StreamReader(@"C:\Users\vemarajs\Desktop\HL7\Ministry\HSHS.txt"); string strTest = reader.ReadToEnd(); string OBX1 = strTest.Split('\n')[3]; File.AppendAllText(@"C:\Users\vemarajs\Desktop\Test\OBX.txt", OBX1 + Environment.NewLine); List&lt;string&gt; list = new List&lt;string&gt;(); using (reader) { string line; while ((line = reader.ReadLine()) != null) { list.Add(line); if (line.StartsWith("OBX|") == true) { string txt = line.Split('|')[5]; File.AppendAllText(@"C:\Users\vemarajs\Desktop\Test\test.txt", txt+Environment.NewLine); } else { //string x = line + Environment.NewLine + OBX1.Distinct(); File.AppendAllText(@"C:\Users\vemarajs\Desktop\Test\newtest.txt", line + Environment.NewLine); } </code></pre> <p>File.AppendAllText(@"C:\Users\vemarajs\Desktop\Test\newtest.txt", OBX1.Distinct().ToList() + Environment.NewLine);</p> <p>This extracts the contents of each OBX segment at element 5 (after 5 pipes) and writes out a file called test.text, in my else statement I am trying to modify the original HL7 file by deleting OBX|2 and OBX|3 to have only One OBX|1 as we expect the number of OBX segments inside the HL7 file to reach 40 or more and we don't wan't to keep those many while returning the message to its messagebox.</p> <p>How do I get the first occurrence of OBX|1 without saying the line number is 4, this might change.</p> <p>This is the working code:</p> <pre><code> StreamReader reader = new StreamReader(@"C:\Users\vemarajs\Desktop\HL7\Ministry\HSHS.txt"); string strTest = reader.ReadToEnd(); reader.DiscardBufferedData(); reader.BaseStream.Seek(0, SeekOrigin.Begin); reader.BaseStream.Position = 0; string OBXstr = string.Empty; string x = null; string fileName = Guid.NewGuid().ToString() + ".txt"; StringBuilder sb = new StringBuilder(); List&lt;string&gt; list = new List&lt;string&gt;(); using (reader) { string line; while ((line = reader.ReadLine()) != null) { list.Add(line); if (line.StartsWith("OBX|") == true) { string txt = line.Split('|')[5]; File.AppendAllText(@"C:\Users\vemarajs\Desktop\Test\"+fileName, txt + Environment.NewLine); } else { sb.AppendLine(line); } } int obx1Index = 0; int obx2Index = 0; var obx1IDR = "\r" + "OBX" + "|" + "1"; var obx1IDN = "\n" + "OBX" + "|" + "1"; var obx2IDR = "\r" + "OBX" + "|" + "2"; var obx2IDN = "\n" + "OBX" + "|" + "2"; obx1Index = strTest.IndexOf(obx1IDN); if (obx1Index &lt; 1) obx1Index = strTest.IndexOf(obx1IDR); obx2Index = strTest.IndexOf(obx2IDN); if (obx2Index &lt; 1) obx2Index = strTest.IndexOf(obx2IDR); if (obx1Index &gt; 0) { OBXstr = strTest.Substring(obx1Index, obx2Index - obx1Index - 1); OBXstr = OBXstr.Replace(strTest.Substring(obx1Index, obx2Index - obx1Index - 1).Split('|')[5], fileName); } } sb.Append(OBXstr); x = sb.ToString(); File.WriteAllText(@"C:\Users\vemarajs\Desktop\Test\newtest.txt", x); reader.Close(); </code></pre>