现有两个文本文件,a.txt和b.txt
a.txt 的内容形式为:
123456 aabbcc aaa
456789 bbccdd bbb
456123 ddfss bb
685233 cscs ccc
b.txt 的内容形式为:
123456 aaaaaa
456789 bbbb
452235 ccccc
523656 ddddddd
想把b.txt中第一列(如123456,456789)的数值逐条在a.txt中检索,列与列之间都用空格分隔。一旦发现a.txt中第一列的某一条含有这个数值,则用b.txt中的第二列替换(如aaaaaa,bbbb),比如b.txt中的123456在a.txt中的第一条出现,则替换成aaaaaa aabbcc aaa,并将替换后的行(如aaaaaa aabbcc aaa)输出到一个TXT文件中!有位大侠给了段代码
Dim fs, f, s, s1, n
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.OpenTextFile("D:\a.txt", 1)
s = f.ReadAll '读入 a.txt 文件
f.Close
Set f = fs.OpenTextFile("D:\b.txt", 1)
Do
s1 = f.ReadLine '行读 b.txt 文件
n = InStr(1, s, Split(s1, " ")(0)) ' 在 s 中查找 前六位的位置
If n > 0 Then s = Replace(s, Split(Mid(s, n), vbCrLf, 2)(0), s1) '找到后替换
Loop Until f.AtEndOfStream
f.Close
Set f = Nothing: Set fs = Nothing
Debug.Print s运行后是将123456 aabbcc aaa 整行替换成123456 aaaaaa了,请问该如何修改啊,或者哪位大侠帮忙重新写过一段~~万分感谢了!
a.txt 的内容形式为:
123456 aabbcc aaa
456789 bbccdd bbb
456123 ddfss bb
685233 cscs ccc
b.txt 的内容形式为:
123456 aaaaaa
456789 bbbb
452235 ccccc
523656 ddddddd
想把b.txt中第一列(如123456,456789)的数值逐条在a.txt中检索,列与列之间都用空格分隔。一旦发现a.txt中第一列的某一条含有这个数值,则用b.txt中的第二列替换(如aaaaaa,bbbb),比如b.txt中的123456在a.txt中的第一条出现,则替换成aaaaaa aabbcc aaa,并将替换后的行(如aaaaaa aabbcc aaa)输出到一个TXT文件中!有位大侠给了段代码
Dim fs, f, s, s1, n
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.OpenTextFile("D:\a.txt", 1)
s = f.ReadAll '读入 a.txt 文件
f.Close
Set f = fs.OpenTextFile("D:\b.txt", 1)
Do
s1 = f.ReadLine '行读 b.txt 文件
n = InStr(1, s, Split(s1, " ")(0)) ' 在 s 中查找 前六位的位置
If n > 0 Then s = Replace(s, Split(Mid(s, n), vbCrLf, 2)(0), s1) '找到后替换
Loop Until f.AtEndOfStream
f.Close
Set f = Nothing: Set fs = Nothing
Debug.Print s运行后是将123456 aabbcc aaa 整行替换成123456 aaaaaa了,请问该如何修改啊,或者哪位大侠帮忙重新写过一段~~万分感谢了!
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string a_txt = @"
123456 aabbcc aaa
456789 bbccdd bbb
456123 ddfss bb
685233 cscs ccc
";
string b_txt = @"
123456 aaaaaa
456789 bbbb
452235 ccccc
523656 ddddddd
";
Dictionary<string, string> a = (from x in a_txt.Trim().Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries) select x.Trim())
.ToDictionary(x => x.Split(' ')[0], y => y.Substring(y.Split(' ')[0].Length + 1));
Dictionary<string, string> b = (from x in b_txt.Trim().Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries) select x.Trim())
.ToDictionary(x => x.Split(' ')[0], y => y.Substring(y.Split(' ')[0].Length + 1));
var query = from x in a join y in b on x.Key equals y.Key select x.Value + y.Value;
string output = "";
query.ToList().ForEach(x => { output += x + "\r\n"; });
Console.WriteLine(output);
}
}
}
aabbcc aaaaaaaaa
bbccdd bbbbbbbPress any key to continue . . .
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string a_txt = @"
123456 aabbcc aaa
456789 bbccdd bbb
456123 ddfss bb
685233 cscs ccc
";
string b_txt = @"
123456 aaaaaa
456789 bbbb
452235 ccccc
523656 ddddddd
";
Dictionary<string, string> a = (from x in a_txt.Trim().Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries) select x.Trim())
.ToDictionary(x => x.Split(' ')[0], y => y.Substring(y.Split(' ')[0].Length + 1));
Dictionary<string, string> b = (from x in b_txt.Trim().Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries) select x.Trim())
.ToDictionary(x => x.Split(' ')[0], y => y.Substring(y.Split(' ')[0].Length + 1));
var query = from x in a join y in b on x.Key equals y.Key select y.Value + " " + x.Value;
string output = "";
query.ToList().ForEach(x => { output += x + "\r\n"; });
Console.WriteLine(output);
}
}
}
aaaaaa aabbcc aaa
bbbb bbccdd bbbPress any key to continue . . .
Dim fs, f, s, s1
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.OpenTextFile("D:\a.txt", 1)
s = f.readall '读入 a.txt 文件
f.Close
Set f = fs.OpenTextFile("D:\b.txt", 1)
Do
s1 = f.readLine '行读 b.txt 文件
s = Replace(s, Split(s1, " ")(0), Split(s1, " ")(1)) '替换
Loop Until f.AtEndOfStream
f.Close
Set f = fs.CreateTextFile("D:\c.txt", True)
f.Write (s) '写入文件
f.Close
Set f = Nothing: Set fs = Nothing结果:
文件 c.txtaaaaaa aabbcc aaa
bbbb bbccdd bbb
456123 ddfss bb
685233 cscs ccc是这意思吧。
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace ConsoleApplication1
{
class Program
{
class MyComparer : IEqualityComparer<KeyValuePair<string, string>>
{
public bool Equals(KeyValuePair<string, string> x, KeyValuePair<string, string> y)
{
return x.Key == y.Key;
} public int GetHashCode(KeyValuePair<string, string> obj)
{
return obj.Key.GetHashCode();
}
} static void Main(string[] args)
{
string a_txt = @"
123456 aabbcc aaa
456789 bbccdd bbb
456123 ddfss bb
685233 cscs ccc
";
string b_txt = @"
123456 aaaaaa
456789 bbbb
452235 ccccc
523656 ddddddd
";
Dictionary<string, string> a = (from x in a_txt.Trim().Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries) select x.Trim())
.ToDictionary(x => x.Split(' ')[0], y => y.Substring(y.Split(' ')[0].Length + 1));
Dictionary<string, string> b = (from x in b_txt.Trim().Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries) select x.Trim())
.ToDictionary(x => x.Split(' ')[0], y => y.Substring(y.Split(' ')[0].Length + 1));
var query = (from x in a join y in b on x.Key equals y.Key select y.Value + " " + x.Value).Union(a.Except(b, new MyComparer()).Select(x => x.Key + " " +x.Value));
string output = "";
query.ToList().ForEach(x => { output += x + "\r\n"; });
Console.WriteLine(output);
}
}
}aaaaaa aabbcc aaa
bbbb bbccdd bbb
456123 ddfss bb
685233 cscs cccPress any key to continue . . .