Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.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
使用Powershell拆分文本文件_Powershell - Fatal编程技术网

使用Powershell拆分文本文件

使用Powershell拆分文本文件,powershell,Powershell,我正在尝试使用Powershell将基于多个字符串的文本文件拆分为两个文件。文件大小从5KB到15KB不等 文件数据的格式如下所示: 18600-ABCD 2204 2020-04-11 00:00:00 18600-ABCD 2204 2020-04-11 00:00:00 18600-ABCD 2204 2020-04-11 00:00:00 18113-ABCD 2204 2020-04-11 00:00:00 18113-ABCD 2204 2020-04-11 00:00:00 198

我正在尝试使用Powershell将基于多个字符串的文本文件拆分为两个文件。文件大小从5KB到15KB不等

文件数据的格式如下所示:

18600-ABCD 2204 2020-04-11 00:00:00

18600-ABCD 2204 2020-04-11 00:00:00

18600-ABCD 2204 2020-04-11 00:00:00

18113-ABCD 2204 2020-04-11 00:00:00

18113-ABCD 2204 2020-04-11 00:00:00

19873-ABCD 2204 2020-04-11 00:00:00

18764-ABCD 2204 2020-04-11 00:00:00

19000-ABCD 2204 2020-04-11 00:00:00

我需要将以18600、18113、19000等开头的所有行(或任何指定的5位数字集)拆分为一个文件,并将不以这些数字开头的所有剩余数据行(否则)拆分为第二个文件

因此,逻辑是,对于文件中的每一行,如果它以这些指定的数字集开始,则写入“file1”,否则写入“file2”


除powershell外,我也愿意接受任何其他建议。非常感谢您的帮助。

假设您希望将以数字开头的行分隔到一个文件中,而将不以数字开头的行分隔到另一个文件中,您可以使用
-match
操作符并传递正则表达式来扫描文本文件中的所有行,并分隔以数字开头的行

代码片段如下所示:

$processText = $fileData.Split([Environment]::NewLine,[StringSplitOptions]::RemoveEmptyEntries)
{
     if($row -match "\d") #Regex to check whether the first character of $row is a digit
     {
         $row | Out-File -FilePath "D:\DataStartingWithNum.text"
     }
     else
     {
         $row | Out-File -FilePath "D:\DataStartingWithText.text"
     }
}
如果您还有任何其他条件(您可能没有在上面的问题中解释),您可以使用类似的方法,使用合适的正则表达式和
-match
运算符过滤掉任何初始数据模式


希望这能有所帮助。

假设您需要所有以18000..18999范围内的数字开头的行,这就完成了。。。[咧嘴笑]

它的作用

  • 设置常量
  • 创建要使用的文件
    准备好对数据执行此操作后,用调用
    获取内容
    替换整个
    #region/#endregion
  • 加载输入文件
  • 遍历该集合
  • 拆分当前行以获取第一个空格之前的零件
  • 将其转换为
    [int]
  • 检查是否在所需范围内
  • 如果是,则将其发送到
    18
    文件
  • 如果否,则将其发送到not-18文件
此代码

  • 缺少任何重要的错误处理
  • 不跟踪所做的事情
  • 没有显示发生了什么
代码

$SourceDir = "$env:TEMP\WBCha"
$TargetNumberRange = 18000..18999
$InFile = Join-Path -Path $SourceDir -ChildPath 'InFile.txt'
$18OutFile = Join-Path -Path $SourceDir -ChildPath '18_OutFile.txt'
$Not_18OutFile = Join-Path -Path $SourceDir -ChildPath 'Not_18OutFile.txt'

#region >>> create a file to work with
#    when ready to do this for real, replace the whole "region" block with a Get-Contnet call
if (-not (Test-Path -LiteralPath $SourceDir))
    {
    $Null = New-Item -Path $SourceDir -ItemType 'Directory' -ErrorAction 'SilentlyContinue'
    }
$HowManyLines = 1e1
$Content = foreach ($Line in 0..$HowManyLines)
    {
    $Prefix = @(18,19)[(Get-Random -InputObject @(0, 1))]
    '{0}{1:d3} - {2}' -f $Prefix, $Line, [datetime]::Now.ToString('yyyyy-MM-dd HH:mm:ss:ffff')
    }
$Content |
    Set-Content -LiteralPath $InFile -ErrorAction 'SilentlyContinue'
#endregion >>> create a file to work with


foreach ($IF_Item in (Get-Content -LiteralPath $InFile))
    {
    if ([int]$IF_Item.Split(' ')[0] -in $TargetNumberRange)
        {
        Add-Content -LiteralPath $18OutFile -Value $IF_Item
        }
        else
        {
        Add-Content -LiteralPath $Not_18OutFile -Value $IF_Item
        }
    }
18
文件内容

18000 - 02020-07-10 12:29:45:6736
18001 - 02020-07-10 12:29:45:6736
18004 - 02020-07-10 12:29:45:6746
18005 - 02020-07-10 12:29:45:6756
18006 - 02020-07-10 12:29:45:6756
18008 - 02020-07-10 12:29:45:6766
18010 - 02020-07-10 12:29:45:6766
19002 - 02020-07-10 12:29:45:6746
19003 - 02020-07-10 12:29:45:6746
19007 - 02020-07-10 12:29:45:6756
19009 - 02020-07-10 12:29:45:6766
不是18
文件内容

18000 - 02020-07-10 12:29:45:6736
18001 - 02020-07-10 12:29:45:6736
18004 - 02020-07-10 12:29:45:6746
18005 - 02020-07-10 12:29:45:6756
18006 - 02020-07-10 12:29:45:6756
18008 - 02020-07-10 12:29:45:6766
18010 - 02020-07-10 12:29:45:6766
19002 - 02020-07-10 12:29:45:6746
19003 - 02020-07-10 12:29:45:6746
19007 - 02020-07-10 12:29:45:6756
19009 - 02020-07-10 12:29:45:6766

所以您想将所有以
18
开头的行发送到一个文件,并将其他任何内容发送到另一个文件?好的,所有以完整数字字符串开头的行,而不仅仅是“18”,因为某些以18开头的字符串将需要转到第二个文件。因此。。。您如何确定将哪些线路发送到何处?您尚未完全指定…确定值基于前5个数字。所有带有“this”组数字的人都应该在“this”文件中。所有以数字以外的任何数字开头的铰线(else)应写入单独的文件。谢谢你下面的回复。库尔![咧嘴笑]这意味着一个射程可以工作。。。这就是我如何设置我发布的答案。在PoSh中,范围可以是非连续的,因此
1..88333..400
将是一个有效的范围。非常感谢。@WBCha-非常欢迎您!很高兴偶尔帮点忙。。。[咧嘴笑]谢谢你的回复,这很有帮助。@WBCha很高兴它有所帮助。请对答案进行投票,如果它解决了您的问题,请将其标记为已接受,以便它也能帮助其他人。