简体   繁体   English

Powershell 循环存储来自多个文件的变量中的任何行

[英]Powershell loop to store any line in variable from multiple files

I have a little situation here which might very simple for you.我这里有一个小情况,对你来说可能很简单。 Context:语境:
I have 3 files: mobile.txt , contract.txt and name.txt All files are formatted like each line should refer to the same line on the other files...我有 3 个文件: mobile.txtcontract.txtname.txt所有文件的格式都像每一行应该引用其他文件的同一行...

Exemple:示例:

  • mobile.txt手机.txt
0606060606
0607070707
0608080808
  • contract.txt合同.txt
123654
456985
152364
  • name.txt名称.txt
John Doe
Miss 1
Mister 2

I would like a foreach like loop to extract line by line in a variable and create table as:我想要一个类似foreach的循环来逐行提取变量中的数据并将表创建为:

0606060606;123654;John Doe
0607070707;456985;Miss 1
0608080808;152364;Mister 2

Sure it's really simple for you.当然,这对你来说真的很简单。 I only manage to store the entire file to a variable and it's not creating a table.我只设法将整个文件存储到一个变量中,而不是创建一个表。

Cheers all.祝大家欢呼。

Question #2 (updated after answering question #1):问题 #2(在回答问题 #1 后更新):

Hello this is working fine with this method Thank you guys.您好,这种方法效果很好,谢谢大家。

I now have a file containing 115 lines like this thanks to you:感谢您,我现在有一个包含 115 行的文件:

0606060606;123654;John Doe
0607070707;456985;Miss 1
0608080808;152364;Mister 2

I have another file containing this type of content:我有另一个包含此类内容的文件:

Smart Pro 5h 3Go ;44,00
OCC-Remise sur Smart Pro 5h 3Go ;-17,60
Abonnement Options-accès international;00,00
Abonnement Options-Option Voyage;00,00
Abonnement Options-ILLIMITE INTRA CPTE;3,00
OCC-option illimité intra-compte offerte;-3,00
----------------------- Page 30----------------------
Smart Pro 5h 3Go ;44,00
OCC-Remise sur Smart Pro 5h 3Go ;-17,60
Abonnement Options-accès international;00,00
Abonnement Options-Option Voyage;00,00
Abonnement Options-ILLIMITE INTRA CPTE;3,00
OCC-option illimité intra-compte offerte;-3,00
----------------------- Page 31----------------------
Smart Pro 5h 3Go;44,00
OCC-Remise sur Smart Pro 5h 3Go ;-17,60
Abonnement Options-ILLIMITE INTRA CPTE;3,00
OCC-option illimité intra-compte offerte ;-3,00
----------------------- Page 32----------------------

Now i have to match them like:现在我必须像这样匹配它们:
Line 1 of file 1 is copied to the first content between the delimiters文件 1 的第 1 行被复制到分隔符之间的第一个内容
Line 2 of file 1 is copied to the second content between the delimiters Line 3 of file 1 is copied to the third content between the delimiters文件 1 的第 2 行复制到分隔符之间的第二个内容 文件 1 的第 3 行复制到分隔符之间的第三个内容
The delimiter can be another string than the one presented with Page numbers.分隔符可以是另一个字符串,而不是带有页码的字符串。

I want this type of output in the final file:我想在最终文件中使用这种类型的 output:

0606060606;123654;John Doe;Smart Pro 5h 3Go ;44,00
0606060606;123654;John Doe;OCC-Remise sur Smart Pro 5h 3Go ;-17,60
0606060606;123654;John Doe;Abonnement Options-accès international;00,00
0606060606;123654;John Doe;Abonnement Options-Option Voyage;00,00
0606060606;123654;John Doe;Abonnement Options-ILLIMITE INTRA CPTE;3,00
0606060606;123654;John Doe;OCC-option illimité intra-compte offerte;-3,00
0607070707;456985;Miss 1;Smart Pro 5h 3Go ;44,00
0607070707;456985;Miss 1;OCC-Remise sur Smart Pro 5h 3Go ;-17,60
0607070707;456985;Miss 1;Abonnement Options-accès international;00,00
0607070707;456985;Miss 1;Abonnement Options-Option Voyage;00,00
0607070707;456985;Miss 1;Abonnement Options-ILLIMITE INTRA CPTE;3,00
0607070707;456985;Miss 1;OCC-option illimité intra-compte offerte;-3,00
0608080808;152364;Mister 2;Smart Pro 5h 3Go;44,00
0608080808;152364;Mister 2;OCC-Remise sur Smart Pro 5h 3Go ;-17,60
0608080808;152364;Mister 2;Abonnement Options-ILLIMITE INTRA CPTE;3,00
0608080808;152364;Mister 2;OCC-option illimité intra-compte offerte ;-3,00

I am stuck in a neverending loop...我陷入了一个无休止的循环......

You use你用

$mobile_txt   = @( Get-Content mobile.txt )
$contract_txt = @( Get-Content contract.txt )
$name_txt     = @( Get-Content name.txt )

instead of hard-coded files:而不是硬编码文件:

$mobile_txt = @'
0606060606
0607070707
0608080808
'@ -split [System.Environment]::NewLine
$contract_txt = @'
123654
456985
152364
'@ -split [System.Environment]::NewLine
$name_txt = @'
John Doe
Miss 1
Mister 2
'@ -split [System.Environment]::NewLine

for ( $ii = 0;
      $ii -lt ( ( $mobile_txt.Count,
                $contract_txt.Count,
                $name_txt.Count | Measure-Object -Minimum).Minimum);
      $ii++ )
{
    [PSCustomObject] @{
        mobile   = $mobile_txt[$ii];
        contract = $contract_txt[$ii];
        name     = $name_txt[$ii]
    };
}

Result : .\SO\70215879.ps1结果.\SO\70215879.ps1

mobile     contract name    
------     -------- ----    
0606060606 123654   John Doe
0607070707 456985   Miss 1  
0608080808 152364   Mister 2

The files may not all have the same number of lines and in that case it is easy to lose information.这些文件可能并不都具有相同的行数,在这种情况下很容易丢失信息。
To allow for that, and create output for all lines, but with possible empty fieds, you could read the files in and create Hashtables from the lines.为此,并为所有行创建 output,但可能存在空字段,您可以读取文件并从行创建哈希表。

$i = 0
$mobile = @{}
Get-Content -Path 'X:\mobile.txt' | ForEach-Object { $mobile[$i++] = $_ }

$i = 0
$contract = @{}
Get-Content -Path 'X:\contract.txt' | ForEach-Object { $contract[$i++] = $_ }

$i = 0
$name = @{}
Get-Content -Path 'X:\name.txt' | ForEach-Object { $name[$i++] = $_ }

# determine the maximum number of lines found in tthe input files
$maxItems = ($mobile.Count, $name.Count, $contract.Count | Measure-Object -Maximum).Maximum

# next, combine and output.
# this won't throw an exception if an item is not present 
# in any of the files, but insert an empty field instead.
for ($i = 0; $i -lt $maxItems; $i++) {
    output a line as shown in your question
    '{0};{1};{2}' -f $mobile[$i], $contract[$i], $name[$i]
}

Or if I may suggest, combine as object , to later save as CSV file或者,如果我可以建议,合并为object ,稍后另存为 CSV 文件

$result = for ($i = 0; $i -lt $maxItems; $i++) {
    # output an object that gets collected in variable $result
    [PsCustomObject]@{
        Mobile   = $mobile[$i]
        Contract = $contract[$i]
        Name     = $name[$i]
    }
}
# view on console
$result

# write to CSV file
$result | Export-Csv -Path 'X:\MobileCombined.csv' -Delimiter ';' -NoTypeInformation

Hello this is working fine with this method Thank you guys.您好,这种方法效果很好,谢谢大家。

I now have a file containing 115 lines like this thanks to you:感谢您,我现在有一个包含 115 行的文件:

0606060606;123654;John Doe
0607070707;456985;Miss 1
0608080808;152364;Mister 2

I have another file containing this type of content:我有另一个包含此类内容的文件:

Smart Pro 5h 3Go ;44,00
OCC-Remise sur Smart Pro 5h 3Go ;-17,60
Abonnement Options-accès international;00,00
Abonnement Options-Option Voyage;00,00
Abonnement Options-ILLIMITE INTRA CPTE;3,00
OCC-option illimité intra-compte offerte;-3,00
----------------------- Page 30----------------------
Smart Pro 5h 3Go ;44,00
OCC-Remise sur Smart Pro 5h 3Go ;-17,60
Abonnement Options-accès international;00,00
Abonnement Options-Option Voyage;00,00
Abonnement Options-ILLIMITE INTRA CPTE;3,00
OCC-option illimité intra-compte offerte;-3,00
----------------------- Page 31----------------------
Smart Pro 5h 3Go;44,00
OCC-Remise sur Smart Pro 5h 3Go ;-17,60
Abonnement Options-ILLIMITE INTRA CPTE;3,00
OCC-option illimité intra-compte offerte ;-3,00
----------------------- Page 32----------------------

Now i have to match them like:现在我必须像这样匹配它们:
Line 1 of file 1 is copied to the first content between the delimiters文件 1 的第 1 行被复制到分隔符之间的第一个内容
Line 2 of file 1 is copied to the second content between the delimiters文件1的第2行被复制到分隔符之间的第二个内容
Line 3 of file 1 is copied to the third content between the delimiters文件1的第3行被复制到分隔符之间的第三个内容
The delimiter can be another string than the one presented with Page numbers.分隔符可以是另一个字符串,而不是带有页码的字符串。

I want this type of output in the final file:我想在最终文件中使用这种类型的 output:

0606060606;123654;John Doe;Smart Pro 5h 3Go ;44,00
0606060606;123654;John Doe;OCC-Remise sur Smart Pro 5h 3Go ;-17,60
0606060606;123654;John Doe;Abonnement Options-accès international;00,00
0606060606;123654;John Doe;Abonnement Options-Option Voyage;00,00
0606060606;123654;John Doe;Abonnement Options-ILLIMITE INTRA CPTE;3,00
0606060606;123654;John Doe;OCC-option illimité intra-compte offerte;-3,00
0607070707;456985;Miss 1;Smart Pro 5h 3Go ;44,00
0607070707;456985;Miss 1;OCC-Remise sur Smart Pro 5h 3Go ;-17,60
0607070707;456985;Miss 1;Abonnement Options-accès international;00,00
0607070707;456985;Miss 1;Abonnement Options-Option Voyage;00,00
0607070707;456985;Miss 1;Abonnement Options-ILLIMITE INTRA CPTE;3,00
0607070707;456985;Miss 1;OCC-option illimité intra-compte offerte;-3,00
0608080808;152364;Mister 2;Smart Pro 5h 3Go;44,00
0608080808;152364;Mister 2;OCC-Remise sur Smart Pro 5h 3Go ;-17,60
0608080808;152364;Mister 2;Abonnement Options-ILLIMITE INTRA CPTE;3,00
0608080808;152364;Mister 2;OCC-option illimité intra-compte offerte ;-3,00

I am stuck in a neverending loop...我陷入了一个无休止的循环......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM