Output two values from a line to a single line [closed]












1














I've got a log file that may have zero or more occurrences of string1, followed by string 2.



I have regex1 that pulls a value from string 1, and a regex2, that pulls a value from string 2 on the same line. there is other text in the same line as well, but I only want the substrings from the two regexes.



What I have so far:



$input_file = "D:Logs*.log"
$output_file = "D:LogsExecuted_Scans.txt"

$regex1 = ‘(?<=Command ScanPath = )(.*)(?=)(Total Files)’
$regex2 = ‘(?<=Start Time = )(.*?)(?=))’

$path = @(Get-Content $input_file |
Select-String -Pattern $regex1 -AllMatches |
ForEach-Object {$_.Matches} |
ForEach-Object {$_.Value})
$starttime = @(Get-Content $input_file |
Select-String Select-String -Pattern $regex2)

Write-Output ($path, $starttime) | Out-File $output_file -Append;


The first regex pulls out a UNC path, the second pulls out out a date/time. The script does pull out the UNC paths, but doesn't pull out the dates. I'd like the pair to be on the same line, with a separator (;).



A few lines of the log file are here. I put "Some Text" in places where there could be random, non-pertinent text. You can see the line I'm trying to work with at the top, and there could be many lines similar to this in the file. I should note, that each line that contains both strings will be start with the text I replaced with "Line of interest".



Message........: 2018-11-30 12:06:12: Line of interest: {"Some Text.","Some Text","0"} {Summary : (Command ScanPath = \hostnameshareProgram Files (x86)FolderFolder)(Total Files = 3)(Total Bytes = 223324428)(Start Time = 11/30/2018 12:04:28 PM)(End Time = 11/30/2018 12:06:12 PM)(Errors = 0)}.
Message........: 2018-11-30 12:06:12: Some Text 2018-11-30 13:00:00.
Message........: 2018-11-30 12:06:12: Some Text.
Message........: 2018-11-30 12:06:16: Some Text. {"Some Text.","Some Text","0"}.
Message........: 2018-11-30 12:06:16: Some Text is 2018-11-30 13:00:44.


Update:



Through some more googling, and banging my head against a wall, I ame up with this. It seems a bit clumsy and inefficient, and I'm sure there is a better way to do it, but it yields the results I am looking for.



$input_file = "D:Logs*.log"
$output_file = "D:LogsExecuted_Scans.txt"

$regex1 = "(?<=Command ScanPath = )(.*)(?=)(Total Files)"
$regex2 = "(?<=Start Time = )(.*?)(?=))"
$completed_scans = @()

$scan_matches = Get-ChildItem $input_file | Select-String -Pattern $regex1
$scan_dates_matches = Get-ChildItem $input_file | Select-String -Pattern $regex2

$scans = $scan_matches.matches.Value
$scan_dates = $scan_dates_matches.matches.Value

#$scans
#$scan_dates

0..($scans.Count - 1) | ForEach-Object {$completed_scans += @("$($scans[$_]) ; $($scan_dates[$_])")}

$completed_scans > $output_file

#Add-Content -Path $output_file -Value "$completed_scans `r`n"









share|improve this question















closed as off-topic by LotPings, tink, Machavity, eyllanesc, Makyen Dec 28 '18 at 6:12


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – LotPings, tink, Machavity, eyllanesc

If this question can be reworded to fit the rules in the help center, please edit the question.









  • 1




    Don't use typographic quotes ‘/’ in scripts. Also show a sample input file (if neccessary neutralized).
    – LotPings
    Dec 27 '18 at 20:40






  • 1




    please post a sample of the input you are working with. two or three lines should be enuf to work with.
    – Lee_Dailey
    Dec 27 '18 at 20:58










  • Thanks guys! Edited the original post with a log file snippet.
    – Higgies
    Dec 28 '18 at 13:21










  • OK, by some more googling and banging my head against the wall, I came up with this. It seems a little clumsy, and I'm sure there is a more elegant way to do it, but it yields the results I am looking for.
    – Higgies
    Dec 28 '18 at 16:01
















1














I've got a log file that may have zero or more occurrences of string1, followed by string 2.



I have regex1 that pulls a value from string 1, and a regex2, that pulls a value from string 2 on the same line. there is other text in the same line as well, but I only want the substrings from the two regexes.



What I have so far:



$input_file = "D:Logs*.log"
$output_file = "D:LogsExecuted_Scans.txt"

$regex1 = ‘(?<=Command ScanPath = )(.*)(?=)(Total Files)’
$regex2 = ‘(?<=Start Time = )(.*?)(?=))’

$path = @(Get-Content $input_file |
Select-String -Pattern $regex1 -AllMatches |
ForEach-Object {$_.Matches} |
ForEach-Object {$_.Value})
$starttime = @(Get-Content $input_file |
Select-String Select-String -Pattern $regex2)

Write-Output ($path, $starttime) | Out-File $output_file -Append;


The first regex pulls out a UNC path, the second pulls out out a date/time. The script does pull out the UNC paths, but doesn't pull out the dates. I'd like the pair to be on the same line, with a separator (;).



A few lines of the log file are here. I put "Some Text" in places where there could be random, non-pertinent text. You can see the line I'm trying to work with at the top, and there could be many lines similar to this in the file. I should note, that each line that contains both strings will be start with the text I replaced with "Line of interest".



Message........: 2018-11-30 12:06:12: Line of interest: {"Some Text.","Some Text","0"} {Summary : (Command ScanPath = \hostnameshareProgram Files (x86)FolderFolder)(Total Files = 3)(Total Bytes = 223324428)(Start Time = 11/30/2018 12:04:28 PM)(End Time = 11/30/2018 12:06:12 PM)(Errors = 0)}.
Message........: 2018-11-30 12:06:12: Some Text 2018-11-30 13:00:00.
Message........: 2018-11-30 12:06:12: Some Text.
Message........: 2018-11-30 12:06:16: Some Text. {"Some Text.","Some Text","0"}.
Message........: 2018-11-30 12:06:16: Some Text is 2018-11-30 13:00:44.


Update:



Through some more googling, and banging my head against a wall, I ame up with this. It seems a bit clumsy and inefficient, and I'm sure there is a better way to do it, but it yields the results I am looking for.



$input_file = "D:Logs*.log"
$output_file = "D:LogsExecuted_Scans.txt"

$regex1 = "(?<=Command ScanPath = )(.*)(?=)(Total Files)"
$regex2 = "(?<=Start Time = )(.*?)(?=))"
$completed_scans = @()

$scan_matches = Get-ChildItem $input_file | Select-String -Pattern $regex1
$scan_dates_matches = Get-ChildItem $input_file | Select-String -Pattern $regex2

$scans = $scan_matches.matches.Value
$scan_dates = $scan_dates_matches.matches.Value

#$scans
#$scan_dates

0..($scans.Count - 1) | ForEach-Object {$completed_scans += @("$($scans[$_]) ; $($scan_dates[$_])")}

$completed_scans > $output_file

#Add-Content -Path $output_file -Value "$completed_scans `r`n"









share|improve this question















closed as off-topic by LotPings, tink, Machavity, eyllanesc, Makyen Dec 28 '18 at 6:12


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – LotPings, tink, Machavity, eyllanesc

If this question can be reworded to fit the rules in the help center, please edit the question.









  • 1




    Don't use typographic quotes ‘/’ in scripts. Also show a sample input file (if neccessary neutralized).
    – LotPings
    Dec 27 '18 at 20:40






  • 1




    please post a sample of the input you are working with. two or three lines should be enuf to work with.
    – Lee_Dailey
    Dec 27 '18 at 20:58










  • Thanks guys! Edited the original post with a log file snippet.
    – Higgies
    Dec 28 '18 at 13:21










  • OK, by some more googling and banging my head against the wall, I came up with this. It seems a little clumsy, and I'm sure there is a more elegant way to do it, but it yields the results I am looking for.
    – Higgies
    Dec 28 '18 at 16:01














1












1








1







I've got a log file that may have zero or more occurrences of string1, followed by string 2.



I have regex1 that pulls a value from string 1, and a regex2, that pulls a value from string 2 on the same line. there is other text in the same line as well, but I only want the substrings from the two regexes.



What I have so far:



$input_file = "D:Logs*.log"
$output_file = "D:LogsExecuted_Scans.txt"

$regex1 = ‘(?<=Command ScanPath = )(.*)(?=)(Total Files)’
$regex2 = ‘(?<=Start Time = )(.*?)(?=))’

$path = @(Get-Content $input_file |
Select-String -Pattern $regex1 -AllMatches |
ForEach-Object {$_.Matches} |
ForEach-Object {$_.Value})
$starttime = @(Get-Content $input_file |
Select-String Select-String -Pattern $regex2)

Write-Output ($path, $starttime) | Out-File $output_file -Append;


The first regex pulls out a UNC path, the second pulls out out a date/time. The script does pull out the UNC paths, but doesn't pull out the dates. I'd like the pair to be on the same line, with a separator (;).



A few lines of the log file are here. I put "Some Text" in places where there could be random, non-pertinent text. You can see the line I'm trying to work with at the top, and there could be many lines similar to this in the file. I should note, that each line that contains both strings will be start with the text I replaced with "Line of interest".



Message........: 2018-11-30 12:06:12: Line of interest: {"Some Text.","Some Text","0"} {Summary : (Command ScanPath = \hostnameshareProgram Files (x86)FolderFolder)(Total Files = 3)(Total Bytes = 223324428)(Start Time = 11/30/2018 12:04:28 PM)(End Time = 11/30/2018 12:06:12 PM)(Errors = 0)}.
Message........: 2018-11-30 12:06:12: Some Text 2018-11-30 13:00:00.
Message........: 2018-11-30 12:06:12: Some Text.
Message........: 2018-11-30 12:06:16: Some Text. {"Some Text.","Some Text","0"}.
Message........: 2018-11-30 12:06:16: Some Text is 2018-11-30 13:00:44.


Update:



Through some more googling, and banging my head against a wall, I ame up with this. It seems a bit clumsy and inefficient, and I'm sure there is a better way to do it, but it yields the results I am looking for.



$input_file = "D:Logs*.log"
$output_file = "D:LogsExecuted_Scans.txt"

$regex1 = "(?<=Command ScanPath = )(.*)(?=)(Total Files)"
$regex2 = "(?<=Start Time = )(.*?)(?=))"
$completed_scans = @()

$scan_matches = Get-ChildItem $input_file | Select-String -Pattern $regex1
$scan_dates_matches = Get-ChildItem $input_file | Select-String -Pattern $regex2

$scans = $scan_matches.matches.Value
$scan_dates = $scan_dates_matches.matches.Value

#$scans
#$scan_dates

0..($scans.Count - 1) | ForEach-Object {$completed_scans += @("$($scans[$_]) ; $($scan_dates[$_])")}

$completed_scans > $output_file

#Add-Content -Path $output_file -Value "$completed_scans `r`n"









share|improve this question















I've got a log file that may have zero or more occurrences of string1, followed by string 2.



I have regex1 that pulls a value from string 1, and a regex2, that pulls a value from string 2 on the same line. there is other text in the same line as well, but I only want the substrings from the two regexes.



What I have so far:



$input_file = "D:Logs*.log"
$output_file = "D:LogsExecuted_Scans.txt"

$regex1 = ‘(?<=Command ScanPath = )(.*)(?=)(Total Files)’
$regex2 = ‘(?<=Start Time = )(.*?)(?=))’

$path = @(Get-Content $input_file |
Select-String -Pattern $regex1 -AllMatches |
ForEach-Object {$_.Matches} |
ForEach-Object {$_.Value})
$starttime = @(Get-Content $input_file |
Select-String Select-String -Pattern $regex2)

Write-Output ($path, $starttime) | Out-File $output_file -Append;


The first regex pulls out a UNC path, the second pulls out out a date/time. The script does pull out the UNC paths, but doesn't pull out the dates. I'd like the pair to be on the same line, with a separator (;).



A few lines of the log file are here. I put "Some Text" in places where there could be random, non-pertinent text. You can see the line I'm trying to work with at the top, and there could be many lines similar to this in the file. I should note, that each line that contains both strings will be start with the text I replaced with "Line of interest".



Message........: 2018-11-30 12:06:12: Line of interest: {"Some Text.","Some Text","0"} {Summary : (Command ScanPath = \hostnameshareProgram Files (x86)FolderFolder)(Total Files = 3)(Total Bytes = 223324428)(Start Time = 11/30/2018 12:04:28 PM)(End Time = 11/30/2018 12:06:12 PM)(Errors = 0)}.
Message........: 2018-11-30 12:06:12: Some Text 2018-11-30 13:00:00.
Message........: 2018-11-30 12:06:12: Some Text.
Message........: 2018-11-30 12:06:16: Some Text. {"Some Text.","Some Text","0"}.
Message........: 2018-11-30 12:06:16: Some Text is 2018-11-30 13:00:44.


Update:



Through some more googling, and banging my head against a wall, I ame up with this. It seems a bit clumsy and inefficient, and I'm sure there is a better way to do it, but it yields the results I am looking for.



$input_file = "D:Logs*.log"
$output_file = "D:LogsExecuted_Scans.txt"

$regex1 = "(?<=Command ScanPath = )(.*)(?=)(Total Files)"
$regex2 = "(?<=Start Time = )(.*?)(?=))"
$completed_scans = @()

$scan_matches = Get-ChildItem $input_file | Select-String -Pattern $regex1
$scan_dates_matches = Get-ChildItem $input_file | Select-String -Pattern $regex2

$scans = $scan_matches.matches.Value
$scan_dates = $scan_dates_matches.matches.Value

#$scans
#$scan_dates

0..($scans.Count - 1) | ForEach-Object {$completed_scans += @("$($scans[$_]) ; $($scan_dates[$_])")}

$completed_scans > $output_file

#Add-Content -Path $output_file -Value "$completed_scans `r`n"






regex powershell parsing






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 28 '18 at 16:03

























asked Dec 27 '18 at 20:18









Higgies

92




92




closed as off-topic by LotPings, tink, Machavity, eyllanesc, Makyen Dec 28 '18 at 6:12


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – LotPings, tink, Machavity, eyllanesc

If this question can be reworded to fit the rules in the help center, please edit the question.




closed as off-topic by LotPings, tink, Machavity, eyllanesc, Makyen Dec 28 '18 at 6:12


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – LotPings, tink, Machavity, eyllanesc

If this question can be reworded to fit the rules in the help center, please edit the question.








  • 1




    Don't use typographic quotes ‘/’ in scripts. Also show a sample input file (if neccessary neutralized).
    – LotPings
    Dec 27 '18 at 20:40






  • 1




    please post a sample of the input you are working with. two or three lines should be enuf to work with.
    – Lee_Dailey
    Dec 27 '18 at 20:58










  • Thanks guys! Edited the original post with a log file snippet.
    – Higgies
    Dec 28 '18 at 13:21










  • OK, by some more googling and banging my head against the wall, I came up with this. It seems a little clumsy, and I'm sure there is a more elegant way to do it, but it yields the results I am looking for.
    – Higgies
    Dec 28 '18 at 16:01














  • 1




    Don't use typographic quotes ‘/’ in scripts. Also show a sample input file (if neccessary neutralized).
    – LotPings
    Dec 27 '18 at 20:40






  • 1




    please post a sample of the input you are working with. two or three lines should be enuf to work with.
    – Lee_Dailey
    Dec 27 '18 at 20:58










  • Thanks guys! Edited the original post with a log file snippet.
    – Higgies
    Dec 28 '18 at 13:21










  • OK, by some more googling and banging my head against the wall, I came up with this. It seems a little clumsy, and I'm sure there is a more elegant way to do it, but it yields the results I am looking for.
    – Higgies
    Dec 28 '18 at 16:01








1




1




Don't use typographic quotes ‘/’ in scripts. Also show a sample input file (if neccessary neutralized).
– LotPings
Dec 27 '18 at 20:40




Don't use typographic quotes ‘/’ in scripts. Also show a sample input file (if neccessary neutralized).
– LotPings
Dec 27 '18 at 20:40




1




1




please post a sample of the input you are working with. two or three lines should be enuf to work with.
– Lee_Dailey
Dec 27 '18 at 20:58




please post a sample of the input you are working with. two or three lines should be enuf to work with.
– Lee_Dailey
Dec 27 '18 at 20:58












Thanks guys! Edited the original post with a log file snippet.
– Higgies
Dec 28 '18 at 13:21




Thanks guys! Edited the original post with a log file snippet.
– Higgies
Dec 28 '18 at 13:21












OK, by some more googling and banging my head against the wall, I came up with this. It seems a little clumsy, and I'm sure there is a more elegant way to do it, but it yields the results I am looking for.
– Higgies
Dec 28 '18 at 16:01




OK, by some more googling and banging my head against the wall, I came up with this. It seems a little clumsy, and I'm sure there is a more elegant way to do it, but it yields the results I am looking for.
– Higgies
Dec 28 '18 at 16:01












0






active

oldest

votes

















0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes

Popular posts from this blog

Mossoró

Can't read property showImagePicker of undefined in react native iOS

Error while reading .h5 file using the rhdf5 package in R