Click here to Skip to main content
15,892,517 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Dear Programmers,

could you please advice me how to remove all of the lines except a specific lines just like the example below

current text
"
,10.0.0.0/8,is,variably,subnetted,39,subnets,4,masks
C,10.12.12.0/30clear interface GigabitEthernet0/0.1
C,10.100.100.19/32clear interface Loopback0
C,10.100.100.221/32clear interface Loopback2
,130.193.0.0/16,is,variably,subnetted,1115,subnets,11,masks
C,130.193.157.222/32clear interface Virtual-Access569
C,130.193.152.217/32clear interface Virtual-Access578
C,130.193.157.221/32clear interface Virtual-Access1109
clear interface Virtual-Access1265
C,130.193.154.138/32clear interface Virtual-Access854
C,130.193.153.137/32clear interface Virtual-Access255
C,130.193.152.136/32clear interface Virtual-Access179
C,130.193.159.136/32clear interface Virtual-Access7
clear interface Virtual-Access5689
"

and what i want is to be the result like this only
"
clear interface Virtual-Access1265
clear interface Virtual-Access5689

"


any advice will be helpful.
Posted

Just read the file line by line and ignore any that do not begin with the word "clear", or whose first character is a double quote. Write out all other lines to a new file.
 
Share this answer
 
So you need to create first the file "mytext.txt" that contains the big text, and then you create the file "myresult.txt"! The two files must be near the executable file...
After that you create the project with the folowing code:

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace MyCodeProjectAnswer
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string line = "";
            string[] substr = new string[1000];
            StreamReader reader = new StreamReader("mytext.txt");
            StreamWriter writer = new StreamWriter("myresult.txt",true);

            while (!reader.EndOfStream)
            {
                line = reader.ReadLine();
                substr = line.Split(' ');

                if (substr[0] == "clear")
                    writer.WriteLine(line);
            }
            reader.Close();
            writer.Close();
        }
    }
}


Finally run it, and the output you want is in the "myresult.txt" file.

Regards,
KZ
 
Share this answer
 
Comments
Richard MacCutchan 13-Sep-12 5:46am    
Why does this need to be a Windows Forms project? A console application would be much simpler.
Killzone DeathMan 13-Sep-12 5:48am    
I haven't think about it! xD
Only because I like... You are right!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900