删除除特定字符串外的所有文本 [英] remove all text except specific string

查看:70
本文介绍了删除除特定字符串外的所有文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的程序员,

您能否建议我如何删除除特定行之外的所有行,就像下面的示例一样

当前文本
"
,10.0.0.0/8,是可变地划分子网的,39,子网,4,掩码
C,10.12.12.0/30透明接口GigabitEthernet0/0.1
C,10.100.100.19/32clear接口Loopback0
C,10.100.100.221/32clear接口Loopback2
,130.193.0.0/16,可变地被子网划分,1115,子网,11,掩码
C,130.193.157.222/32clear接口Virtual-Access569
C,130.193.152.217/32clear接口Virtual-Access578
C,130.193.157.221/32clear接口Virtual-Access1109
清除界面Virtual-Access1265
C,130.193.154.138/32clear接口Virtual-Access854
C,130.193.153.137/32clear接口Virtual-Access255
C,130.193.152.136/32clear接口Virtual-Access179
C,130.193.159.136/32clear接口Virtual-Access7
清除界面Virtual-Access5689
"

而我想要的只是这样的结果
"
清除界面Virtual-Access1265
清除界面Virtual-Access5689

"


任何建议都将有所帮助.

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.

推荐答案

只需逐行读取文件,并忽略所有不以单词"clear"开头或第一个字符为'a'的文件.双引号.将所有其他行写到新文件中.
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.


因此,您需要首先创建包含大文本的文件"mytext.txt",然后创建文件"myresult.txt" !这两个文件必须在可执行文件附近...
之后,使用以下代码创建项目:

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:

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();
        }
    }
}



最后运行它,所需的输出在"myresult.txt"文件中.

问候,
KZ



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

Regards,
KZ


这篇关于删除除特定字符串外的所有文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆