Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 以最便宜、最快的方式读取包含多行事件的大型文本文件_.net_Powershell_Text - Fatal编程技术网

.net 以最便宜、最快的方式读取包含多行事件的大型文本文件

.net 以最便宜、最快的方式读取包含多行事件的大型文本文件,.net,powershell,text,.net,Powershell,Text,我正在尝试读取具有多行事件的非常大的文本文件(~1.5-2GB),例如: START--random stuff here more random stuff on this new line more stuff and things START--some random things additional random things blah blah START--data data more data START--things blah data 。。。但是以一种快速

我正在尝试读取具有多行事件的非常大的文本文件(~1.5-2GB),例如:

START--random stuff here 
more random stuff on this new line 
more stuff and things 
START--some random things 
additional random things 
blah blah 
START--data data more data 
START--things 
blah data
。。。但是以一种快速和非资源密集的方式。我不确定这种方法是否正确,但似乎仍需要一段时间,而且我无法正确地拆分事件,结果似乎是每行。而且,每一步都需要相当长的时间才能完成,我想知道我是否可以在一个时间内完成每一步

我正在建造一座新的大楼

带有
-File
选项的语句允许您逐个读取文件行,从而保持内存使用率低且恒定;选项
-Regex
允许分支处理程序为正则表达式

$eventLines = ''
switch -Regex -File 'E:\Scripts\Logs\First1000' {
  '^START--' { # Note that the regex is case-INsensitive
    if ($eventLines) { 
        # send previous events' lines to syslog
    }
    # Current line is the start of a new event.
    $eventLines = $_
  }
  default { 
    # Event-interior line, append it.
    $eventLines += [Envirionment]::NewLine + $_
  }
}
# Process last event.
if ($eventLines) { 
    # send last event's lines to syslog
}
或者,您可以类似地修改
System.IO.File.ReadLines()
解决方案,并使用
if
语句和string-literal
string.StartsWith()
方法,这比正则表达式匹配更快-但是,在我的非正式测试中,
开关
解决方案总体上看起来还是更快

$eventLines = ''
foreach ($line in [System.IO.File]::ReadLines('E:\Scripts\Logs\First1000')) {
  if ($line.StartsWith('START--')) {
    if ($eventLines) { 
        # send previous events' lines to syslog
    }
    # Current line is the start of a new event.
    $eventLines = $line
  } else {
    # Event-interior line, append it.
    $eventLines += [Environment]::NewLine + $line
  }
}
# Process last event.
if ($eventLines) { 
    # send last event's lines to syslog
}

为了什么?你想对这个“大文本文件”的内容做什么?我计划通过syslog发送发现的事件。我已经准备好了一个函数。所谓“发现”是指遇到的每一个事件/行,还是要过滤每一行,如果过滤器匹配,则“发送”?最终目标是分离多行事件并通过syslog发送。因此,您希望结果是一个多行字符串列表,从
开始--
开始,直到下一次出现
开始--
?@mrmrmr:很抱歉听到这个消息。不幸的是,我对syslog一无所知,但我鼓励您提出一个新问题(这个问题有一个定义良好的范围,答案也是如此)。
$eventLines = ''
foreach ($line in [System.IO.File]::ReadLines('E:\Scripts\Logs\First1000')) {
  if ($line.StartsWith('START--')) {
    if ($eventLines) { 
        # send previous events' lines to syslog
    }
    # Current line is the start of a new event.
    $eventLines = $line
  } else {
    # Event-interior line, append it.
    $eventLines += [Environment]::NewLine + $line
  }
}
# Process last event.
if ($eventLines) { 
    # send last event's lines to syslog
}