我有一个条件,即程序从文件'assign.txt'中读取行,然后从最后6个下划线中获取数据,然后将该数据写入另一个文件line.txt
。
但在将数据写入line.txt
之前,我必须检查数据是否已存在于line.txt
中,如果是,则跳过或替换现有数据,但line.txt
中的每个数据都应该是唯一的文件assign.txt
可以包含多行
foreach (string fileLine in File.ReadAllLines("F:\\assign.txt"))
{
string tel_in = "TELENOR_BTS_INCOMING";
string telenor = fileLine.ToUpper();
var delimeter = "_";
if (telenor.Contains(tel_in))
{
var lineParts = fileLine.Split(new[] { delimeter }, StringSplitOptions.None);
if (lineParts.Length > 1) lineParts = lineParts.Skip(1).ToArray();
var data = string.Join(delimeter, lineParts.Skip(lineParts.Length - 7));
lineData.Add(data);
File.WriteAllLines(@"C:\Users\Adnan Haider\Desktop\line.txt", lineData);
}
else
{
var lineParts = fileLine.Split(new[] { delimeter }, StringSplitOptions.None);
if (lineParts.Length > 1) lineParts = lineParts.Skip(1).ToArray();
var data = string.Join(delimeter, lineParts.Skip(lineParts.Length - 6));
lineData.Add(data);
File.WriteAllLines(@"C:\Users\Adnan Haider\Desktop\line.txt", lineData);
}
}
这是line.txt示例输出文件
Mobilink_User_dpo_bahawalpur_0506768768685465367658717_1205 42.out Telenor_bts_outgoing_dpo_chakwal_030617_15201696981.out Telenor_bts_outgoing_dpo_chakwal_030617_15201696981.out Telenor_bts_incoming_dpo_chakwal_030617_15201696981.out Telenor_bts_incoming_dpo_chakwal_030617_15201696981.out Ufone_User_dpo_chakwal_030617_15235869694.out
这是assign.txt示例
dpo.chakwal63201732033PM_FL_Telenor_bts_outgoing_dpo_chakwal _030617_15201696981.出 dpo.chakwal63201732033PM_FL_Telenor_bts_outgoing_dpo_chakwal _030617_15201696981.出
上面的行是输出样本line.txt
样本,下面的行是输入行assign.txt
我想在line.txt
中写输出,但首先我必须检查数据是否已存在于{{ 1}}如果是,则跳过或用现有行替换输出,否则将数据写入line.txt
答案 0 :(得分:-1)
Loops有不同的方法。这是我建议的那个。没有Try / Catch也没有使用(){},但你明白了。
string[] m_fileLine = File.ReadAllLines(@"F:\\assign.txt");
StreamWriter m_results = new StreamWriter(@"F:\\results.txt", true);
string tel_in = "TELENOR_BTS_INCOMING";
for (int i = 0; i < m_fileLine.Length; i++)
{
if (m_fileLine[i].ToUpper().Contains(tel_in))
{
// some logic here...
// then write here...
m_results.WriteLine(m_fileLine[i].ToString());
m_results.Flush();
}
else
{
// else logic and write here...
}
}