I have a file that I have trimmed to only keep some of the data in it. This file is the file I would like to add to the end of a second file. Here it what the first file looks like:
147221
147486
147514-1
147502
147503
...
I would like to concat it to the end of a file that looks like this:
some text ... more text
text text: Description
Ln PPN PD Qty Units Comment MNAME MfMCODE
some comment
1 EC5547 PCB: SB5500 LCD DISPLAY CTRL +RHS 1.00 EA REV 07 OR LATER
2 EC0303 FERRITE BEAD: 200MA 1000Z0.8 SM0603 +RHS 2.00 EA FB2 FB4 MURATA BLM18RK102SN1
3 EC0304 FERRITE BEAD: 2000MA 220Z0.05 SM0805 +RHS 2.00 EA FB1 FB5 MURATA BLM21PG221SN1
4 EC7727 IC: LTC2851 RS422 XCVR SO8 +RHS 1.00 EA U10 LINEAR TECHNOLOGIES LTC2851CS8#PBF
LINEAR TECHNOLOGIES LTC2851IS8#PBF
MAXIM MAX3077EASA+
MAXIM MAX3077EESA+
DNP C20 C24 C25 C27 C44 C60 C62 J1 J5 J6 J7 J8 J11 R2 R20
R29 R33 R53 R54 R89 R91 R94 R96 R107 R108 R109 C63 R61
J12 J13 U7 TP1-20 TP22-28 TP34-36 TP38-39 TP41 TP43-54
TP56-96
5 EC5071 CONN: HEADER RA 0.1x10 0.025 SQ +RHS 1.00 EQ J2 SAMTEC TSW-110-08-S-S-RA
...
By matching to see if the second file line starts with a 1,2,3,4,5, etc... and then adding the lines from the first file in the same order.
What I mean for it to look like would be something like this:
some text ... more text
text text: Description
Ln PPN PD Qty Units Comment MNAME MfMCODE
some comment
1 EC5547 PCB: SB5500 LCD DISPLAY CTRL +RHS 1.00 EA REV 07 OR LATER 1开发者_C百科47221
2 EC0303 FERRITE BEAD: 200MA 1000Z0.8 SM0603 +RHS 2.00 EA FB2 FB4 MURATA BLM18RK102SN1 147486
3 EC0304 FERRITE BEAD: 2000MA 220Z0.05 SM0805 +RHS 2.00 EA FB1 FB5 MURATA BLM21PG221SN1 147514-1
4 EC7727 IC: LTC2851 RS422 XCVR SO8 +RHS 1.00 EA U10 LINEAR TECHNOLOGIES LTC2851CS8#PBF 147502
LINEAR TECHNOLOGIES LTC2851IS8#PBF
MAXIM MAX3077EASA+
MAXIM MAX3077EESA+
DNP C20 C24 C25 C27 C44 C60 C62 J1 J5 J6 J7 J8 J11 R2 R20
R29 R33 R53 R54 R89 R91 R94 R96 R107 R108 R109 C63 R61
J12 J13 U7 TP1-20 TP22-28 TP34-36 TP38-39 TP41 TP43-54
TP56-96
5 EC5071 CONN: HEADER RA 0.1x10 0.025 SQ +RHS 1.00 EQ J2 SAMTEC TSW-110-08-S-S-RA 147503
....
THOUGHTS
- Right now I think the best way to go about this is to trim the original file (which I have already done) and then search through the second file looking for lines that begin with a digit. (I would think to do this with regex expression: ...@"^[\d]+\s+"...). However, using that regex, I would only grab the number on the line and not the entire line? But if I was able to grab the whole line (I am not sure how to), if the that line was found, I would put it in a string and then add the first line of previous file to the end of that string with a delimiter "\t".
So pretty much:
- Trim the first file to the necessary numbers needed to concat to end of 2nd file (DONE)
- Grab each line in the second file (one by one?)
- Check if the line starts with a number (using regex?)
- If it does, add the 1st line in the 1st file to the end of the match in the 2nd file (do this for all matches.. 2nd line with 2nd match, 3rd line with 3rd match, etc.) and store it as a new string. (problem with this is that there would have to be a whole bunch of strings... and I am not familiar with lists..)
- Check if the line starts with a number (using regex?)
- Now go back through each line of the 2nd file and compare the new string (with the first file ending) to every line in the 2nd file. If there is a match, replace that line with the line in the string.
- Output it to a rich text box/.txt file.
QUESTION
- How do I find a line that starts with a 1,2,3,4,5,etc in the second file and then grab the first file line by line and add the first line to the first line. In this case, adding 147221 to the end of the entire line that begins with a 1 (147486 to the end of the line that starts with a 2, etc)?
- Does anyone know of an easier way to do this?
This will read from the two input files and write to a third. Regarding the regex pattern, this will work if a line starts with any positive integer (not including zero) immediately followed by a whitespace character (e.g., A line like "10 some non-numeric text" will be a match).
using (StreamReader sr1 = new StreamReader(@"C:\Temp\Content.txt"))
using (StreamReader sr2 = new StreamReader(@"C:\Temp\Numbers.txt"))
using (StreamWriter sw = new StreamWriter(@"C:\Temp\Combined.txt"))
{
string curLine;
while ((curLine = sr1.ReadLine()) != null)
{
if (Regex.IsMatch(curLine, "^[1-9][0-9]*\s"))
{
sw.WriteLine(curLine + " " + sr2.ReadLine());
}
else
{
sw.WriteLine(curLine);
}
}
}
Assuming the lines that you are interested in will always start with some number with no leading spaces, I'd just use a regular expression just to verify that the line is what you're looking for (or even just a simple check if the first character of the line is a digit using Char.IsDigit()
), then do any processing after you've found it.
using (var dataReader = File.OpenText(@"path\to\data\file"))
using (var labelReader = File.OpenText(@"path\to\label\file"))
using (var writer = File.CreateText(@"path\to\output\file"))
{
string line;
while ((line = dataReader.ReadLine()) != null)
{
if (Regex.IsMatch(line, @"^\d+"))
{ // found the line (append label)
writer.WriteLine(line + " " + labelReader.ReadLine());
}
else
{ // not the line (pass through)
writer.WriteLine(line);
}
}
}
Not sure about regex but you can use sting functions to do this with a if-else statement like (may not be a solid one but works though)
string str = "1 EC5547 PCB: SB5500 LCD DISPLAY CTRL +RHS 1.00 EA REV 07 OR LATER";
string str1 = "147221";
if (str.StartsWith("1"))
{
str += str1;
}
Output is easy, we can worry about that later. As a general approach, I would load your first file numbers into an array (you can do some sort of list or vector if you want but I don't think it is necessary). Have a counter as you parse your second file which starts at 0. When you find a line you want to output to, then use the counter as the index to your array of strings to grab the next string in order. After writing out the number, increment your counter. Writing it back out to the file can be done as you iterate through. Something like this(not tested!!! this is just to show the idea.) should work:
StringBuilder newFile = new StringBuilder();
string[] file = File.ReadAllLines(@"file2path");
foreach (string line in file)
{
if (!regex evaluation here!)
{
//append your number and increment counter here
string temp = line.Replace(oldString, appendedString);
newFile.Append(temp + "\r\n");
continue;
}
newFile.Append(line + "\r\n");
}
File.WriteAllText(@""file2path", newFile.ToString());
精彩评论