Reputation: 505
I have a file like this
DeviceID : 3
FriendlyName : SAMSUNG MZVLW1T0HMLH-000H1
DeviceID : 0
FriendlyName : SAMSUNG MZNLN256HAJQ-000H7
DeviceID : 1
FriendlyName : ST1000LM049-2GH172
And I have other file like this
DAY JUNE1=3
DAY JUNE2=0
DAY JUNE3=1
I want to apend second file to first file, to be like this
JUNE1
DeviceID : 3
FriendlyName : SAMSUNG MZVLW1T0HMLH-000H1
JUNE2
DeviceID : 0
FriendlyName : SAMSUNG MZNLN256HAJQ-000H7
JUNE3
DeviceID : 1
FriendlyName : ST1000LM049-2GH172
I tried this
$file1 = Get-Content .\file1
$file2 = Get-Content .\file2
$output = $file, $file2 | Out-File .\outputfile.txt
my result is like this
DAY JUNE1=3
DAY JUNE2=0
DAY JUNE3=1
DeviceID : 3
FriendlyName : SAMSUNG MZVLW1T0HMLH-000H1
DeviceID : 0
FriendlyName : SAMSUNG MZNLN256HAJQ-000H7
DeviceID : 1
FriendlyName : ST1000LM049-2GH172
My expectation, I can create a new file with my expectation output like I mentioned above. Please help any idea
I tried this
$file1 = Get-Content .\file1
$file2 = Get-Content .\file2
$i=1
$arr=@()
Foreach($item in $file1)
{
$str= 'JUNE'+ $i + '=' + $item
$arr += $str
$i=$i+1
}
$arr | out-file Output.txt
It return me like this
JUNE1=
JUNE2=
JUNE3=DeviceID : 0
JUNE5=FriendlyName : INTEL SSDSC2BF180A4H SED
JUNE6=
JUNE7=DeviceID : 1
JUNE8=FriendlyName : INTEL SSDSC2BF180A4H
JUNE9=
JUNE10=DeviceID : 2
JUNE11=FriendlyName : TOSHIBA DT01ACA100
JUNE12=
JUNE13=
JUNE14=
Upvotes: 0
Views: 417
Reputation: 61028
Looking at the input files, I think you may need to match the values for the dates to the DeviceID's in the other file.
You can do that like this:
# read the devices as array of Hashtables
$hardware = (Get-Content -Path 'D:\devices.txt' -Raw).Trim() -split '\r?\n\r?\n' -replace ':', '=' | ConvertFrom-StringData
# read the dates as Hashtable
$dates = (Get-Content -Path 'D:\devicedates.txt' -Raw).Trim() -replace 'DAY\s*' | ConvertFrom-StringData
# build the output strings, matching the values for the dates with the DeviceID's
$result = foreach ($device in $hardware) {
$dte = $dates.Keys | Where-Object { $dates[$_] -eq $device['DeviceID'] }
# output as formatted string
"$dte{0}DeviceID : {1}{0}FriendlyName : {2}{0}" -f [Environment]::NewLine, $device['DeviceID'], $device['FriendlyName']
}
$result | Set-Content -Path 'D:\devicescomplete.txt' -Force
Output:
JUNE1 DeviceID : 3 FriendlyName : SAMSUNG MZVLW1T0HMLH-000H1 JUNE2 DeviceID : 0 FriendlyName : SAMSUNG MZNLN256HAJQ-000H7 JUNE3 DeviceID : 1 FriendlyName : ST1000LM049-2GH172
Upvotes: 1
Reputation:
Provided the number of sections in file1 matches the number of lines in file2,
you could read file1 with the -raw
parameter and
split after each empty line, similarly split the lines of file2 at space and equal sign:
## Q:\Test\2019\08\15\SO_57505745.ps1
$file1 = (Get-Content .\file1 -raw) -split '(?<=\n)\r?\n'
$file2 = Get-Content .\file2
$arr = for($i=0;$i -lt $file1.Count;$i++){
($file2[$i] -split ' |=')[1]
$file1[$i]
}
$arr #| out-file Output.txt
Sample output:
> Q:\Test\2019\08\15\SO_57505745.ps1
JUNE1
DeviceID : 3
FriendlyName : SAMSUNG MZVLW1T0HMLH-000H1
JUNE2
DeviceID : 0
FriendlyName : SAMSUNG MZNLN256HAJQ-000H7
JUNE3
DeviceID : 1
FriendlyName : ST1000LM049-2GH172
Upvotes: 2