Home > Software design >  Powershell loop to store any line in variable from multiple files
Powershell loop to store any line in variable from multiple files

Time:12-05

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...

Exemple :

  • mobile.txt
0606060606
0607070707
0608080808
  • contract.txt
123654
456985
152364
  • name.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 :

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.

CodePudding user response:

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

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

CodePudding user response:

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.

$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

$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
  • Related