0

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

Updated

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=
2
  • 1
    You need to parse each file, then combine the results, then create new formatted output. Take a look at ConvertFrom-StringData. Commented Aug 15, 2019 at 7:35
  • $i = 0 $file2[$i] + "rn" + $file1[$i] + "rn" + $file1[$i + 1] $i++ Its not a solution, but might be on the right direction Commented Aug 15, 2019 at 8:44

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

Comments

1

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

5 Comments

Hi @Theo, thank you. I have one problem, how if I have more than 2 item not only DeviceID and FriendlyName? How do I add it?
@Job Simply add them to the 'devices' file and also to the output string.
Okay. Got it. Thanks @Theo
Hi @Job , could you please check it out if you dont mind, my friend and I did homework and still stuck stackoverflow.com/q/57509091/11099245 Thanks
@Job If you mean you would like to sort the output on any of the devices properties, then just add | Sort-Object { $_.DeviceID } at the end of the line that starts with $hardware = . Here I'm using the DeviceID property, but you can sort on any property the devices have. If you want the sort to go the other way, append the -Descending switch. Sort-Object

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.