我有两个.txt文件,我想在其中一个中搜索secoond文件中的密钥。如果找到了密钥,我想将匹配替换为第二个文件中密钥后面的文本。另外,我想根据文件中的数字添加“T”或“B”。
以下是第一个文件的示例:
1 0010 SOMETEXT/NUMBERS SOME TEXT, TEXT
.2 0020 SOMETEXT/NUMBERS SOME TEXT, TEXT, TEXT
1 0020A SOMETEXT/NUMBERS SOME TEXT,TEXT
.1 2000 SOMETEXT/NUMBERS SOME TEXT,TEXT, TEXT
..3 9123A SOMETEXT/NUMBERS SOME TEXT
... ... ... ...
以下是第二个文件的示例:
0010 ABCD EFG
0020 C01
0020A C02, C3, C004, C9901, C938, C12
2000 R123, R13, D34
9123A SOMETEXT
... ...
从这两个文件中,我想使用第二个文件中的“键”(在每个新行的开头)。因此在该示例中,密钥将是:0010,0020,0020A,2000,9123A。我有密钥后,我想搜索第二列中的第一个文本文档,看看是否匹配。如果匹配(在这种情况下它们都匹配)我想替换第一个文档中的文本并使用更新的文本创建一个新文档。此外,如果第二个文件中的键后面有多个值,我想将这些行复制下来。看下面看看我的意思。
新文件如下所示:
1 ABCD EFG SOMETEXT/NUMBERS SOMETEXT, TEXT
.2 C01 SOMETEXT/NUMBERS SOME TEXT, TEXT, TEXT
1 C02 SOMETEXT/NUMBERS SOME TEXT,TEXT //--------------------
1 C3 SOMETEXT/NUMBERS SOME TEXT,TEXT // Copied the lines
1 C004 SOMETEXT/NUMBERS SOME TEXT,TEXT // down if there are
1 C9901 SOMETEXT/NUMBERS SOME TEXT,TEXT // multiple values in
1 C938 SOMETEXT/NUMBERS SOME TEXT,TEXT // document 2.
1 C12 SOMETEXT/NUMBERS SOMETEXT,TEXT //--------------------
.1 R123 SOMETEXT/NUMBERS SOME TEXT,TEXT, TEXT //--------------
.1 R13 SOMETEXT/NUMBERS SOME TEXT,TEXT, TEXT // COPIED ALSO
.1 D34 SOMETEXT/NUMBERS SOME TEXT,TEXT, TEXT //--------------
..3 SOMETEXT SOMETEXT/NUMBERS SOME TEXT
... ... ... ...
此外,如果键以“A”结尾,我想在上面的字符串末尾添加“T”。如果密钥没有以“A”结尾,我想在上面的字符串末尾添加一个“B”。
几行示例:
.1 R123 SOMETEXT/NUMBERS SOME TEXT,TEXT, TEXT B //key was 2000
.1 R13 SOMETEXT/NUMBERS SOME TEXT,TEXT, TEXT B //key was 2000
.1 D34 SOMETEXT/NUMBERS SOME TEXT,TEXT, TEXT B //key was 2000
..3 SOMETEXT SOMETEXT/NUMBERS SOME TEXT T //key was 9123A
第一个文件
1 0010 142717 PCB, PCA-0320
1 0020 CAP-00010G CAP-C,1uF,10%,X7R,0603
1 0020A CAP-00010G CAP-C,1uF,10%,X7R,0603
1 0030A CAP-00127G CAP,4.7uF,10%,X5R,0805
1 0040A CAP-00126G CAP,12pF,10%,50V
1 0050A CAP-00131G CAP,1000pF,10%,100V
1 0060 134920 CAP-C,0.1uF,10%,10V,X7R
1 0060A 134920 CAP-C,0.1uF,10%,10V,X7R
1 0070 CAP-00129G CAP,0.01uF,10%,25V
1 0070A CAP-00129G CAP,0.01uF,10%,25V
1 0080 CAP-00128G CAP,15pF,10%,25V
1 0080A CAP-00128G CAP,15pF,10%,25V
1 0090A CAP-00132G CAP,0.47uF,10%,10V
1 0100 RES-00466G RES,2.7K,1%,1/16W
1 0110 RES-00464G RES,1.54K,1%,1/16W
1 0120 RES-00461G RES,2K,5%,1/16W
1 0130 RES-00460G RES,12.1K,1%,1/16W
1 0140 RES-00455G RES,330,5%,1/16W
1 0150A RES-00454G RES,51.1,1%,1/16W
1 0160 RES-00453G RES,47,1%,1/16W
1 0160A RES-00453G RES,47,1%,1/16W
1 0170 RES-00458G RES,0,1/16W,0402
1 0170A RES-00458G RES,0,1/16W,0402
1 0180 RES-00457G RES,15K,1%,1/16W
1 0190 RES-00456G RES,1K,1%,1/16W,0402
1 0200 RES-00463G RES,10K,1%,1/16W,0402
1 0200A RES-00463G RES,10K,1%,1/16W,0402
1 0210A RES-00462G RES,1.5M,5%,1/16W
1 0220 CAP-00133G CAP,100pF,5%,50V
1 0230 LED-00011G LED ARRAY,GY,.02A
1 0240 IC-00199G IC,MICRO ARM,NS7520
1 0250A IC-00198G IC,VOLT MONITOR RES
1 0270A IC-00134G IC,TRAN,RS232,3DX5R
1 0280A IC-00196G IC,TRANS,ICS1893BF
1 0290A ICP-01006G IC,PLD,NIM-PX-1
1 0300 IC-00194G IC,MEM,SDRAM,2MX32
1 0310A IC-00083G IC,LOGIC,2 INPUT
1 0320A IC-00193G IC,LOGIC,2 INPUT
1 0330 IC-00192G IC,VOLT REG,.150A@1.5V
1 0340A IND-00009G IND,120nH,100MHz,5%
1 0350A IND-00008G IND,25%,.37A,ESR=.3
1 0360A IND-00007G IND,120OHM@100MHz
1 0370A INT-00112G HDR,2X8,1.27mm,ST,1A
1 0380A INT-00113G HDR,2X4,1.27mm,ST,1A
1 0390A XTL-00011G CRYSTAL,18.432MHz
1 0400 XTL-00012G CRYSTAL,25.000MHz
1 0410A CON-00205G CONN,RJ45,SIDE ENTRY
1 0420A CON-00181G CONN,RJ45,RA,1.02mm
1 0422A ICP-01042G IC,FLASH,NIM-BLT
1 0423 122261 ADH, CYANOARYLATE
1 0424 70476 ADH,ACCELERANT
1 0425 143298 ADH, LOCTITE 3609
1 2000 98658-1 LBL,IUC,PCA,S/N
.2 0010 74320-27 LBL,IUI,1.0x.25
..3 9000 74320 DWG-A,SPEC,LBL,IUI
.2 9000 98658 DWG-A,FAB,LBL,IMP
.2 9010 SC12279-1 RIBBON-LBL,PRT,4.33,POLY
1 9000 143239 DWG-C,ASSY,ELEC
1 9010 143240 DWG-B,SCHEMATIC
1 9020 139045 TEST,ICT
1 9030 139046 TEST,FCT
1 9620 143241 CUSTOMER P/N
1 9630 98982-4A PCA REV: 4A
第二个文件
0010 BARE PCB
0020 C40
0020A C5, C45, C48
0030A C25
0040A C17, C18
0050A C15
0060 C20, C23,
0060A C6, C7, C8, C9, C10, C11, C12,C31, C32, C33, C34, C35, C36,C37, C38, C39
0070 C16, C21, C22, C24, C41, C42
0070A C3, C19, C43, C44, C47
0080 C13, C14
0080A C1, C2
0090A C26, C27, C28, C29, C30
0100 R65
0110 R35
0120 R34
0130 R33
0140 R21, R22, R29, R30
0150A R28, R31, R32, R37
0160 R17, R19, R26, R47, R50, R51,R53, R57, R58, R59
0160A R18, R25, R42, R48, R49, R52,R54, R55, R56, R60
0170 R23
0170A R10, R43
0180 R8, R9
0190 R13, R14, R15, R61, R62, R63,R64
0200 R27, R38, R39, R40, R41
0200A R2, R3, R11, R44, R45, R46
0210A R1
0220 C4
0230 D1
0240 U1
0250A U10
0270A U6
0280A U5
0290A U4
0300 U2
0310A U7
0320A U8
0330 U9
0340A L2, L3
0350A L8, L9
0360A L1, L4, L5, L6, L7, L10
0370A J1, J2
0380A J3
0390A X1
0400 X2
0410A J4
0420A J5
0422A U3
2000 TRACKING LABEL
0423 ADHESIVE
0424 ACCELERATOR
0425 ADHESIVE
3RD文件(HOPEFULLY
1 BARE PCB 142717 PCB, PCA-0320 B
1 C40 CAP-00010G CAP-C,1uF,10%,X7R,0603 B
1 C5 CAP-00010G CAP-C,1uF,10%,X7R,0603 T
1 C45 CAP-00010G CAP-C,1uF,10%,X7R,0603 T
1 C48 CAP-00010G CAP-C,1uF,10%,X7R,0603 T
1 C25 CAP-00127G CAP,4.7uF,10%,X5R,0805 T
1 C17 CAP-00126G CAP,12pF,10%,50V T
1 C18 CAP-00126G CAP,12pF,10%,50V T
1 C15 CAP-00131G CAP,1000pF,10%,100V T
1 C20 134920 CAP-C,0.1uF,10%,10V,X7R B
1 C23 134920 CAP-C,0.1uF,10%,10V,X7R B
1 C6 134920 CAP-C,0.1uF,10%,10V,X7R T
1 C7 134920 CAP-C,0.1uF,10%,10V,X7R T
1 C8 134920 CAP-C,0.1uF,10%,10V,X7R T
1 C9 134920 CAP-C,0.1uF,10%,10V,X7R T
1 C10 134920 CAP-C,0.1uF,10%,10V,X7R T
1 C11 134920 CAP-C,0.1uF,10%,10V,X7R T
1 C12 134920 CAP-C,0.1uF,10%,10V,X7R T
1 C31 134920 CAP-C,0.1uF,10%,10V,X7R T
1 C32 134920 CAP-C,0.1uF,10%,10V,X7R T
1 C33 134920 CAP-C,0.1uF,10%,10V,X7R T
1 C34 134920 CAP-C,0.1uF,10%,10V,X7R T
1 C35 134920 CAP-C,0.1uF,10%,10V,X7R T
1 C36 134920 CAP-C,0.1uF,10%,10V,X7R T
1 C37 134920 CAP-C,0.1uF,10%,10V,X7R T
1 C38 134920 CAP-C,0.1uF,10%,10V,X7R T
1 C39 134920 CAP-C,0.1uF,10%,10V,X7R T
1 C16 CAP-00129G CAP,0.01uF,10%,25V B
1 C21 CAP-00129G CAP,0.01uF,10%,25V B
1 C22 CAP-00129G CAP,0.01uF,10%,25V B
1 C24 CAP-00129G CAP,0.01uF,10%,25V B
1 C41 CAP-00129G CAP,0.01uF,10%,25V B
1 C42 CAP-00129G CAP,0.01uF,10%,25V B
1 C3 CAP-00129G CAP,0.01uF,10%,25V T
1 C19 CAP-00129G CAP,0.01uF,10%,25V T
1 C43 CAP-00129G CAP,0.01uF,10%,25V T
1 C44 CAP-00129G CAP,0.01uF,10%,25V T
1 C47 CAP-00129G CAP,0.01uF,10%,25V T
1 C13 CAP-00128G CAP,15pF,10%,25V B
1 C14 CAP-00128G CAP,15pF,10%,25V B
1 C1 CAP-00128G CAP,15pF,10%,25V T
1 C2 CAP-00128G CAP,15pF,10%,25V T
1 C26 CAP-00132G CAP,0.47uF,10%,10V T
1 C27 CAP-00132G CAP,0.47uF,10%,10V T
1 C28 CAP-00132G CAP,0.47uF,10%,10V T
1 C29 CAP-00132G CAP,0.47uF,10%,10V T
1 C30 CAP-00132G CAP,0.47uF,10%,10V T
1 R65 RES-00466G RES,2.7K,1%,1/16W B
1 R35 RES-00464G RES,1.54K,1%,1/16W B
1 R34 RES-00461G RES,2K,5%,1/16W B
1 R33 RES-00460G RES,12.1K,1%,1/16W B
1 R21 RES-00455G RES,330,5%,1/16W B
1 R22 RES-00455G RES,330,5%,1/16W B
1 R29 RES-00455G RES,330,5%,1/16W B
1 R30 RES-00455G RES,330,5%,1/16W B
1 R28 RES-00454G RES,51.1,1%,1/16W T
1 R31 RES-00454G RES,51.1,1%,1/16W T
1 R32 RES-00454G RES,51.1,1%,1/16W T
1 R37 RES-00454G RES,51.1,1%,1/16W T
1 R17 RES-00453G RES,47,1%,1/16W B
1 R19 RES-00453G RES,47,1%,1/16W B
1 R26 RES-00453G RES,47,1%,1/16W B
1 R47 RES-00453G RES,47,1%,1/16W B
1 R50 RES-00453G RES,47,1%,1/16W B
1 R51 RES-00453G RES,47,1%,1/16W B
1 R53 RES-00453G RES,47,1%,1/16W B
1 R57 RES-00453G RES,47,1%,1/16W B
1 R58 RES-00453G RES,47,1%,1/16W B
1 R59 RES-00453G RES,47,1%,1/16W B
1 R18 RES-00453G RES,47,1%,1/16W T
1 R25 RES-00453G RES,47,1%,1/16W T
1 R42 RES-00453G RES,47,1%,1/16W T
1 R48 RES-00453G RES,47,1%,1/16W T
1 R49 RES-00453G RES,47,1%,1/16W T
1 R52 RES-00453G RES,47,1%,1/16W T
1 R54 RES-00453G RES,47,1%,1/16W T
1 R55 RES-00453G RES,47,1%,1/16W T
1 R56 RES-00453G RES,47,1%,1/16W T
1 R60 RES-00453G RES,47,1%,1/16W T
1 R23 RES-00458G RES,0,1/16W,0402 B
1 R10 RES-00458G RES,0,1/16W,0402 T
1 R43 RES-00458G RES,0,1/16W,0402 T
1 R8 RES-00457G RES,15K,1%,1/16W B
1 R9 RES-00457G RES,15K,1%,1/16W B
1 R13 RES-00456G RES,1K,1%,1/16W,0402 B
1 R14 RES-00456G RES,1K,1%,1/16W,0402 B
1 R15 RES-00456G RES,1K,1%,1/16W,0402 B
1 R61 RES-00456G RES,1K,1%,1/16W,0402 B
1 R62 RES-00456G RES,1K,1%,1/16W,0402 B
1 R63 RES-00456G RES,1K,1%,1/16W,0402 B
1 R64 RES-00456G RES,1K,1%,1/16W,0402 B
1 R27 RES-00463G RES,10K,1%,1/16W,0402 B
1 R38 RES-00463G RES,10K,1%,1/16W,0402 B
1 R39 RES-00463G RES,10K,1%,1/16W,0402 B
1 R40 RES-00463G RES,10K,1%,1/16W,0402 B
1 R41 RES-00463G RES,10K,1%,1/16W,0402 B
1 R2 RES-00463G RES,10K,1%,1/16W,0402 T
1 R3 RES-00463G RES,10K,1%,1/16W,0402 T
1 R11 RES-00463G RES,10K,1%,1/16W,0402 T
1 R44 RES-00463G RES,10K,1%,1/16W,0402 T
1 R45 RES-00463G RES,10K,1%,1/16W,0402 T
1 R46 RES-00463G RES,10K,1%,1/16W,0402 T
1 R1 RES-00462G RES,1.5M,5%,1/16W T
1 C4 CAP-00133G CAP,100pF,5%,50V B
1 D1 LED-00011G LED ARRAY,GY,.02A B
1 U1 IC-00199G IC,MICRO ARM,NS7520 B
1 U10 IC-00198G IC,VOLT MONITOR RES T
1 U6 IC-00134G IC,TRAN,RS232,3DX5R T
1 U5 IC-00196G IC,TRANS,ICS1893BF T
1 U4 ICP-01006G IC,PLD,NIM-PX-1 T
1 U2 IC-00194G IC,MEM,SDRAM,2MX32 B
1 U7 IC-00083G IC,LOGIC,2 INPUT T
1 U8 IC-00193G IC,LOGIC,2 INPUT T
1 U9 IC-00192G IC,VOLT REG,.150A@1.5V B
1 L2 IND-00009G IND,120nH,100MHz,5% T
1 L3 IND-00009G IND,120nH,100MHz,5% T
1 L8 IND-00008G IND,25%,.37A,ESR=.3 T
1 L9 IND-00008G IND,25%,.37A,ESR=.3 T
1 L1 IND-00007G IND,120OHM@100MHz T
1 L4 IND-00007G IND,120OHM@100MHz T
1 L5 IND-00007G IND,120OHM@100MHz T
1 L6 IND-00007G IND,120OHM@100MHz T
1 L7 IND-00007G IND,120OHM@100MHz T
1 L10 IND-00007G IND,120OHM@100MHz T
1 J1 INT-00112G HDR,2X8,1.27mm,ST,1A T
1 J2 INT-00112G HDR,2X8,1.27mm,ST,1A T
1 J3 INT-00113G HDR,2X4,1.27mm,ST,1A T
1 X1 XTL-00011G CRYSTAL,18.432MHz T
1 X2 XTL-00012G CRYSTAL,25.000MHz B
1 J4 CON-00205G CONN,RJ45,SIDE ENTRY T
1 J5 CON-00181G CONN,RJ45,RA,1.02mm T
1 U3 ICP-01042G IC,FLASH,NIM-BLT T
1 ADHESIVE 122261 ADH, CYANOARYLATE B
1 ACCELERATOR 70476 ADH,ACCELERANT B
1 ADHESIVE 143298 ADH, LOCTITE 3609 B
1 TRACKING LABEL 98658-1 LBL,IUC,PCA,S/N B
.2 0010 74320-27 LBL,IUI,1.0x.25 B
..3 9000 74320 DWG-A,SPEC,LBL,IUI B
.2 9000 98658 DWG-A,FAB,LBL,IMP B
.2 9010 SC12279-1 RIBBON-LBL,PRT,4.33,POLY B
1 9000 143239 DWG-C,ASSY,ELEC B
1 9010 143240 DWG-B,SCHEMATIC B
1 9020 139045 TEST,ICT B
1 9030 139046 TEST,FCT B
1 9620 143241 CUSTOMER P/N B
1 9630 98982-4A PCA REV: 4A B
格式化问题:
1 C10 134920CAP-C,0.1uF,10%,10V,X7R T
1 C11 134920CAP-C,0.1uF,10%,10V,X7R T
1 C12,C31 134920CAP-C,0.1uF,10%,10V,X7R T //THIS LINE SHOULD BE SPLIT INTO 2
1 C32 134920CAP-C,0.1uF,10%,10V,X7R T
答案 0 :(得分:1)
我能够生成一个类似你期望的文件。我不确定这是否会对您的代码有所帮助,因为我对格式化感到困惑,但希望它会。可能有一种清理此代码的方法,但我没有时间深入研究您的问题。我希望这会有所帮助,如果没有,我很抱歉。
我的第一个文件包含数据:
1 0010 xxxxxxxxxxxxxx xxxx Txxx, Txxx
.2 0020 yyyyyyyyyyyyyyyy yyyy yyyy, yyyy, yyyy
1 0020A rrrrrrrrrrrrrrrrrr rrrr rrrr,rrrr
.1 2000 wwwwwwwwwwwwwwwwwww wwww wwww,wwww, wwww
..3 9123A vvvvvvvvvvvvvvvvvvvvv vvvv vvvv
然后第二个文件包含这些数据:
0010 ABCD EFG
0020 C01
0020A C02, C3, C004, C9901, C938, C12
2000 R123, R13, D34
9123A H33333
然后代码生成包含此数据的第三个文件:
1 ABCDEFG xxxxxxxxxxxxxxxxxxTxxx,Txxx
.2 C01 yyyyyyyyyyyyyyyyyyyyyyyy,yyyy,yyyy
1 C02 rrrrrrrrrrrrrrrrrrrrrrrrrr,rrrr T
1 C3 rrrrrrrrrrrrrrrrrrrrrrrrrr,rrrr T
1 C004 rrrrrrrrrrrrrrrrrrrrrrrrrr,rrrr T
1 C9901 rrrrrrrrrrrrrrrrrrrrrrrrrr,rrrr T
1 C938 rrrrrrrrrrrrrrrrrrrrrrrrrr,rrrr T
1 C12 rrrrrrrrrrrrrrrrrrrrrrrrrr,rrrr T
.1 R123 wwwwwwwwwwwwwwwwwwwwwwwwwww,wwww,wwww
.1 R13 wwwwwwwwwwwwwwwwwwwwwwwwwww,wwww,wwww
.1 D34 wwwwwwwwwwwwwwwwwwwwwwwwwww,wwww,wwww
..3 H33333 vvvvvvvvvvvvvvvvvvvvvvvvvvvvv T
代码如下所示:
var strFileName = DisplayFile("choose file 1", "*.txt", null);
var strFileName2 = DisplayFile("choose file 2", "*.txt", null);
StreamReader srInputOne = File.OpenText(strFileName);
StreamReader srInputTwo = File.OpenText(strFileName2);
StreamWriter swOutput = new StreamWriter(@"U:\test\ThirdFile.txt");
Hashtable oneHash = new Hashtable();
Hashtable twoHash = new Hashtable();
List <string> recOne = new List<string>();
List<string> recNew = new List<string>();
while (srInputTwo.Peek() > -1)
{
string line = srInputTwo.ReadLine();
string[] vals = line.Split(' ');
twoHash.Add(vals[0], vals[0]);
}
srInputTwo.Close();
while (srInputOne.Peek() > -1)
{
string line = srInputOne.ReadLine();
string[] parts = line.Split(' ');
if(!twoHash.ContainsKey(parts[1]) && parts[0] != "1")
{
swOutput.WriteLine(line);
}
else if (!twoHash.ContainsKey(parts[1]) && parts[0] != "1")
{
swOutput.WriteLine(line);
}
else if (!twoHash.ContainsKey(parts[1]) && parts[0] == "1")
{
swOutput.WriteLine(line);
}
else
{
var join = string.Empty;
for (int i = 2; i < parts.Length; i++)
{
join = join + parts[i];
}
if (parts[1].Contains('A'))
{
join = join + " T";
}
else
{
join = join + " B";
}
if (!oneHash.ContainsKey(parts[1]))
{
oneHash.Add(parts[1], join);
}
}
}
srInputOne.Close();
srInputTwo = File.OpenText(strFileName2);
while(srInputTwo.Peek() > -1)
{
string line2 = srInputTwo.ReadLine();
string[] parts2 = line2.Split(' ');
string join2 = string.Empty;
for (int i = 1; i < parts2.Length; i++)
{
join2 = join2 + " " + parts2[i];
}
if (oneHash.ContainsKey(parts2[0]))
{
if (join2.Contains(','))
{
for (int j = 1; j < parts2.Length; j++)
{
swOutput.WriteLine("1 " + parts2[j].Trim(',') + " " + oneHash[parts2[0]].ToString());
}
}
else
{
swOutput.WriteLine("1" + join2 + " " + oneHash[parts2[0]].ToString());
}
}
}
srInputTwo.Close();
swOutput.Close();