更新时间:2022-12-17 11:14:25
取决于具体情况的许多可能性:
- 如果要删除的文本在行首时始终相同。只需跳过前23个字符。
这样的东西甚至可以解决问题。var result = line.SubString( 23 );
- 如果要删除的部分更多是模式,RegEx(正则表达式)更有可能提供帮助。
[更新]
格式化输入后,事情看起来有点复杂,RegEx肯定是有序的。
我会构建一个RegEx来识别有趣的部分和使用replace来重构结果。
开始解决方案的示例:string pattern = @ ^ * Left Rulegd *([^] +)*([^] +)\ n([^] +) 跨度>;
string input = Left Rulegd ATTR001。 PDST.MPDTMP01(A001PMTR)PROTY(01),BRPKY(A0Y)\ nAA001PMTR 0 00AF 123456A11 2011/12/14 ENT tmp;
string replacement =
1,
2,
I am trying to parse a txt file into a desired o/p so that I can open it as a CSV once parse is completed. With a little knowledge of Linq, I tried to code around parse process but I am missing something that I myself unable to figure it out. Any help is appreciated.!
Input file:
Left Rulegd ATTR001.PDST.MPDTMP01(A001PMTR) PROTY(01),BRPKY(A0Y)
A001PMTR 0 00AF 123456A11 2011/12/14 ENT tmp
Left Rulegd ATTR001.PDST.MPDTMP01(B001PRTY) PROTY(01),BRPKY(A0Y)
B001PRTY 1 0 12AB 7891P12 2010/08/25 AMP tmp
B234561 2 0 0 7891P12 2011/09/12 URTST tmp
Left Rulegd ATTR001.PDST.MPDTMP01(C001AMEF) PROTY(01),BRPKY(A0Y)
C001AMEF 0 PZ89 123456A11 2013/11/02 AMP tmp
Left Rulegd ATTR001.PDST.MPDTMP01(D001AAM) PROTY(01),BRPKY(A0Y)
D001AAM 0 OP25 123456A11 2009/02/14 ENT tmp
Desired output:
ATTR001.PDST.MPDTMP01(A001PMTR),PROTY(01),BRPKY(A0Y),123456A11,2011/12/14,ENT tmp
ATTR001.PDST.MPDTMP01(B001PRTY),PROTY(01),BRPKY(A0Y),7891P12,2010/08/25,AMP tmp
ATTR001.PDST.MPDTMP01(C001AMEF),PROTY(01),BRPKY(A0Y),123456A11,2013/11/02,AMP tmp
ATTR001.PDST.MPDTMP01(D001AAM),PROTY(01),BRPKY(A0Y),123456A11,2009/02/14,ENT tmp
What I have tried:
void Main()
{
int counter = 0;
string line;
string line1;
string srchBegin = "Left Rulegd";
char delim = ' ';
IEnumerable<string> allRecords;
string filePath = @"C:\Users\vgruber\Desktop\input.TXT";
if (File.Exists(filePath))
{
allRecords = File.ReadLines(filePath);
}
else
{
Console.WriteLine("File do not exist!");
}
StreamReader reader = File.OpenText(filePath);
try
{
while ((line = reader.ReadLine()) != null)
{
if (line.Contains(srchBegin))
{
var values = line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
var result = String.Join(",", values).Remove(0, 12);
Console.WriteLine(result);
}
else
{
var values1 = line.Substring(30);
var result1 = string.Join(",", values1);
Console.WriteLine(result1);
}
}
reader.Close();
}
catch(Exception er)
{
}
}
// Define other methods and classes here
Many possibilities depending on situation:
- if the text to remove is always the same at beginning of line. Just skip the first 23 chars.
Something like this can even do the trick.var result = line.SubString(23);
- if the part to remove is more of a pattern, RegEx (Regular Expressions) are more likely to help.
[Update]
After formatting the input, it appear that things are a little more complicated and RegEx are definitely on order.
I would build a RegEx to identify interesting parts with groups and use replace to recompose the result.
Example with beginning of solution:string pattern = @"^ *Left Rulegd *([^ ]+) *([^ ]+)\n([^ ]+)"; string input = " Left Rulegd ATTR001.PDST.MPDTMP01(A001PMTR) PROTY(01),BRPKY(A0Y)\nA001PMTR 0 00AF 123456A11 2011/12/14 ENT tmp"; string replacement = "
1,
2,