How to export multi line string to single line textHow to handle command-line arguments in PowerShellPaste a multi-line Java String in EclipseHow to enter a multi-line commandPythonic way to create a long multi-line stringHow can I check if a string is null or empty in PowerShell?How do I concatenate strings and variables in PowerShell?How can I replace every occurrence of a String in a file with PowerShell?Is there an operator that can trim indentation in multi-line string?Variables in ADUsers2CSV scriptPython: How to insert a line in a multi-line string?
Ask one verbal question to figure out who is blind and who is mute among three persons
Rapid change in character
What's the origin of the concept of alternate dimensions/realities?
Is it possible for a person to be tricked into becoming a lich?
How to differentiate between two people with the same name in a story?
How were US credit cards verified in-store in the 1980's?
I was reported to HR as being a satan worshiper
Eshet Chayil in the Tunisian service
I was given someone else's visa, stamped in my passport
How do I portray irrational anger in first person?
Who declared the Last Alliance to be the "last" and why?
How did medieval manors handle population growth? Was there room for more fields to be ploughed?
Was it illegal to blaspheme God in Antioch in 360.-410.?
Don't look at what I did there
Resources to learn about firearms?
Sum and average calculator
Does the telecom provider need physical access to the SIM card to clone it?
Why haven't the British protested Brexit as ardently like Hong Kongers protest?
How to investigate an unknown 1.5GB file named "sudo" in my Linux home directory?
Printing a list as "a, b, c." using Python
Create a list of snaking numbers under 50,000
Idiomatic way to create an immutable and efficient class in C++?
How to animate a function plot
Small RAM 4 KB on the early Apple II?
How to export multi line string to single line text
How to handle command-line arguments in PowerShellPaste a multi-line Java String in EclipseHow to enter a multi-line commandPythonic way to create a long multi-line stringHow can I check if a string is null or empty in PowerShell?How do I concatenate strings and variables in PowerShell?How can I replace every occurrence of a String in a file with PowerShell?Is there an operator that can trim indentation in multi-line string?Variables in ADUsers2CSV scriptPython: How to insert a line in a multi-line string?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I have a text file "c:zz.txt" of which an extract is as follows:
#N ABSA ALL ROUNDER FoF
#D UT-ABSAAG
#P 20190215 393.83 393.83 0.00
#N ABSA BALANCED FUND
#D UT-ABSABA
#P 20190215 432.28 432.28 0.00
#N COMMUNITY GILT
#D UT-COM-G
#P 20190215 151.21 151.21 8.59
I would like to export this to a .TXT file with following format:
UT_Name,UT_Code,Date,Value1,Value2,Vol
ABSA ALL ROUNDER FoF,UT-ABSAAG,20190215,393.83,393.83,0
ABSA BALANCED FUND,UT-ABSABA,20190215,432.28,432.28,0
COMMUNITY GILT,UT-COM-G,20190215,151.21,151.21,8.59
My code below
clear-Host
get-content -raw "c:zz.txt" | % $_ -replace '(#N)',"`r`n" |% #P)',','|Set-Content ZZ1.txt
Output:
ABSA ALL ROUNDER FoF, UT-ABSAAG, 20190215 393.83 393.83 0.00
ABSA BALANCED FUND, UT-ABSABA, 20190215 432.28 432.28 0.00
COMMUNITY GILT, UT-COM-G, 20190215 151.21 151.21 8.59
Issue:
The problem turns out to be that dataset "#P" in original dataset is fixed length which means I can't simply replace all whitespaces, with commas as this will also affect field names "#N" and "#D" above which I dont want to affect.
How do I selectively replace white spaces?
powershell export-to-csv multilinestring
add a comment |
I have a text file "c:zz.txt" of which an extract is as follows:
#N ABSA ALL ROUNDER FoF
#D UT-ABSAAG
#P 20190215 393.83 393.83 0.00
#N ABSA BALANCED FUND
#D UT-ABSABA
#P 20190215 432.28 432.28 0.00
#N COMMUNITY GILT
#D UT-COM-G
#P 20190215 151.21 151.21 8.59
I would like to export this to a .TXT file with following format:
UT_Name,UT_Code,Date,Value1,Value2,Vol
ABSA ALL ROUNDER FoF,UT-ABSAAG,20190215,393.83,393.83,0
ABSA BALANCED FUND,UT-ABSABA,20190215,432.28,432.28,0
COMMUNITY GILT,UT-COM-G,20190215,151.21,151.21,8.59
My code below
clear-Host
get-content -raw "c:zz.txt" | % $_ -replace '(#N)',"`r`n" |% #P)',','|Set-Content ZZ1.txt
Output:
ABSA ALL ROUNDER FoF, UT-ABSAAG, 20190215 393.83 393.83 0.00
ABSA BALANCED FUND, UT-ABSABA, 20190215 432.28 432.28 0.00
COMMUNITY GILT, UT-COM-G, 20190215 151.21 151.21 8.59
Issue:
The problem turns out to be that dataset "#P" in original dataset is fixed length which means I can't simply replace all whitespaces, with commas as this will also affect field names "#N" and "#D" above which I dont want to affect.
How do I selectively replace white spaces?
powershell export-to-csv multilinestring
add a comment |
I have a text file "c:zz.txt" of which an extract is as follows:
#N ABSA ALL ROUNDER FoF
#D UT-ABSAAG
#P 20190215 393.83 393.83 0.00
#N ABSA BALANCED FUND
#D UT-ABSABA
#P 20190215 432.28 432.28 0.00
#N COMMUNITY GILT
#D UT-COM-G
#P 20190215 151.21 151.21 8.59
I would like to export this to a .TXT file with following format:
UT_Name,UT_Code,Date,Value1,Value2,Vol
ABSA ALL ROUNDER FoF,UT-ABSAAG,20190215,393.83,393.83,0
ABSA BALANCED FUND,UT-ABSABA,20190215,432.28,432.28,0
COMMUNITY GILT,UT-COM-G,20190215,151.21,151.21,8.59
My code below
clear-Host
get-content -raw "c:zz.txt" | % $_ -replace '(#N)',"`r`n" |% #P)',','|Set-Content ZZ1.txt
Output:
ABSA ALL ROUNDER FoF, UT-ABSAAG, 20190215 393.83 393.83 0.00
ABSA BALANCED FUND, UT-ABSABA, 20190215 432.28 432.28 0.00
COMMUNITY GILT, UT-COM-G, 20190215 151.21 151.21 8.59
Issue:
The problem turns out to be that dataset "#P" in original dataset is fixed length which means I can't simply replace all whitespaces, with commas as this will also affect field names "#N" and "#D" above which I dont want to affect.
How do I selectively replace white spaces?
powershell export-to-csv multilinestring
I have a text file "c:zz.txt" of which an extract is as follows:
#N ABSA ALL ROUNDER FoF
#D UT-ABSAAG
#P 20190215 393.83 393.83 0.00
#N ABSA BALANCED FUND
#D UT-ABSABA
#P 20190215 432.28 432.28 0.00
#N COMMUNITY GILT
#D UT-COM-G
#P 20190215 151.21 151.21 8.59
I would like to export this to a .TXT file with following format:
UT_Name,UT_Code,Date,Value1,Value2,Vol
ABSA ALL ROUNDER FoF,UT-ABSAAG,20190215,393.83,393.83,0
ABSA BALANCED FUND,UT-ABSABA,20190215,432.28,432.28,0
COMMUNITY GILT,UT-COM-G,20190215,151.21,151.21,8.59
My code below
clear-Host
get-content -raw "c:zz.txt" | % $_ -replace '(#N)',"`r`n" |% #P)',','|Set-Content ZZ1.txt
Output:
ABSA ALL ROUNDER FoF, UT-ABSAAG, 20190215 393.83 393.83 0.00
ABSA BALANCED FUND, UT-ABSABA, 20190215 432.28 432.28 0.00
COMMUNITY GILT, UT-COM-G, 20190215 151.21 151.21 8.59
Issue:
The problem turns out to be that dataset "#P" in original dataset is fixed length which means I can't simply replace all whitespaces, with commas as this will also affect field names "#N" and "#D" above which I dont want to affect.
How do I selectively replace white spaces?
powershell export-to-csv multilinestring
powershell export-to-csv multilinestring
edited Mar 28 at 4:10
LotPings
23.8k6 gold badges16 silver badges33 bronze badges
23.8k6 gold badges16 silver badges33 bronze badges
asked Mar 27 at 23:02
milkyway pizzamilkyway pizza
132 bronze badges
132 bronze badges
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
this uses named capture groups to get the items and then exports it to a CSV file.
# fake reading in a raw text file
# in real life, use Get-Content -Raw
$InStuff = @'
#N ABSA ALL ROUNDER FoF
#D UT-ABSAAG
#P 20190215 393.83 393.83 0.00
#N ABSA BALANCED FUND
#D UT-ABSABA
#P 20190215 432.28 432.28 0.00
#N COMMUNITY GILT
#D UT-COM-G
#P 20190215 151.21 151.21 8.59
'@
# split into blocks, trim unwanted whitespace, filter out the blank block
$SplitInStuff = ($InStuff -split '#N').Trim().Where($_)
$Results = foreach ($SIS_Item in $SplitInStuff)
$Null = $SIS_Item -match '(?sm)(?<UT_Name>^.+$).*#D (?<UT_Code>.+).*#P (?<Date>d+)s+(?<Value1>[0-9.]+)s+(?<Value2>[0-9.]+)s+(?<Vol>[0-9.]+)'
[PSCustomObject]@
# the ".Trim()" was needed to remove leftover EOL/NewLine/space chars
UT_Name = $Matches.UT_Name.Trim()
UT_Code = $Matches.UT_Code.Trim()
Date = $Matches.Date.Trim()
Value1 = $Matches.Value1.Trim()
Value2 = $Matches.Value2.Trim()
Vol = $Matches.Vol.Trim()
$Results |
Export-Csv -LiteralPath "$env:TEMPmilkywaypizza_ProductInfo.csv" -NoTypeInformation
content of the CSV file ...
"UT_Name","UT_Code","Date","Value1","Value2","Vol"
"ABSA ALL ROUNDER FoF","UT-ABSAAG","20190215","393.83","393.83","0.00"
"ABSA BALANCED FUND","UT-ABSABA","20190215","432.28","432.28","0.00"
"COMMUNITY GILT","UT-COM-G","20190215","151.21","151.21","8.59"
add a comment |
Other method :
$Data = @'
#N ABSA ALL ROUNDER FoF
#D UT-ABSAAG
#P 20190215 393.83 393.83 0.00
#N ABSA BALANCED FUND
#D UT-ABSABA
#P 20190215 432.28 432.28 0.00
#N COMMUNITY GILT
#D UT-COM-G
#P 2019021 151.21 151.21 8.59
'@
#template for learn schema
$template=@'
#N Vol_ABSA_ALL_ROUNDER_FoF*:ABSA ALL ROUNDER FoF
#D UT-UT_Name:ABSAAG
#P Date:20190215 Value1:393.83 Value2:393.83 Vol:0.00
#N Vol_ABSA_ALL_ROUNDER_FoF*:ABSA ALL ROUNDER FoF 2
#D UT-UT_Name:ABSAAG2
#P Date:20190216 Value1:393.83 Value2:393.83 Vol:0.00
'@
$Data | ConvertFrom-String -TemplateContent $template | export-csv "c:tempresult.csv" -NoType
I think it's important to add the following disclaimer wheneverConvertFrom-String
is used: It provides separator-based parsing as well as heuristics-based parsing based on templates containing example values. The separator-based parsing applies automatic type conversions you cannot control, and the template language is poorly documented, with the exact behavior hard to predict - it's best to avoid this cmdlet altogether. Also note that it's not available in PowerShell Core.
– mklement0
Mar 28 at 12:24
add a comment |
How do I selectively replace runs of multiple whitespace characters?
E.g., by only selecting multiple spaces with a quantifier -replace " 2,",','
The following script based on your one liner:
## Q:Test2019328SO_55387785.ps1
$FileIn = '.zz.txt'
$FileOut= '.zz1.txt'
Set-Content $FileOut -Value "UT_Name,UT_Code,Date,Value1,Value2,Vol"
(Get-Content $FileIn -raw) -replace "(`r?`n)?#N " -replace "`r?`n(#D|#P) | 2,",',' |
Add-Content $FileOut
yields this output:
> Get-Content .ZZ1.txt
UT_Name,UT_Code,Date,Value1,Value2,Vol
ABSA ALL ROUNDER FoF,UT-ABSAAG,20190215,393.83,393.83,0.00
ABSA BALANCED FUND,UT-ABSABA,20190215,432.28,432.28,0.00
COMMUNITY GILT,UT-COM-G,20190215,151.21,151.21,8.59
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55387785%2fhow-to-export-multi-line-string-to-single-line-text%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
this uses named capture groups to get the items and then exports it to a CSV file.
# fake reading in a raw text file
# in real life, use Get-Content -Raw
$InStuff = @'
#N ABSA ALL ROUNDER FoF
#D UT-ABSAAG
#P 20190215 393.83 393.83 0.00
#N ABSA BALANCED FUND
#D UT-ABSABA
#P 20190215 432.28 432.28 0.00
#N COMMUNITY GILT
#D UT-COM-G
#P 20190215 151.21 151.21 8.59
'@
# split into blocks, trim unwanted whitespace, filter out the blank block
$SplitInStuff = ($InStuff -split '#N').Trim().Where($_)
$Results = foreach ($SIS_Item in $SplitInStuff)
$Null = $SIS_Item -match '(?sm)(?<UT_Name>^.+$).*#D (?<UT_Code>.+).*#P (?<Date>d+)s+(?<Value1>[0-9.]+)s+(?<Value2>[0-9.]+)s+(?<Vol>[0-9.]+)'
[PSCustomObject]@
# the ".Trim()" was needed to remove leftover EOL/NewLine/space chars
UT_Name = $Matches.UT_Name.Trim()
UT_Code = $Matches.UT_Code.Trim()
Date = $Matches.Date.Trim()
Value1 = $Matches.Value1.Trim()
Value2 = $Matches.Value2.Trim()
Vol = $Matches.Vol.Trim()
$Results |
Export-Csv -LiteralPath "$env:TEMPmilkywaypizza_ProductInfo.csv" -NoTypeInformation
content of the CSV file ...
"UT_Name","UT_Code","Date","Value1","Value2","Vol"
"ABSA ALL ROUNDER FoF","UT-ABSAAG","20190215","393.83","393.83","0.00"
"ABSA BALANCED FUND","UT-ABSABA","20190215","432.28","432.28","0.00"
"COMMUNITY GILT","UT-COM-G","20190215","151.21","151.21","8.59"
add a comment |
this uses named capture groups to get the items and then exports it to a CSV file.
# fake reading in a raw text file
# in real life, use Get-Content -Raw
$InStuff = @'
#N ABSA ALL ROUNDER FoF
#D UT-ABSAAG
#P 20190215 393.83 393.83 0.00
#N ABSA BALANCED FUND
#D UT-ABSABA
#P 20190215 432.28 432.28 0.00
#N COMMUNITY GILT
#D UT-COM-G
#P 20190215 151.21 151.21 8.59
'@
# split into blocks, trim unwanted whitespace, filter out the blank block
$SplitInStuff = ($InStuff -split '#N').Trim().Where($_)
$Results = foreach ($SIS_Item in $SplitInStuff)
$Null = $SIS_Item -match '(?sm)(?<UT_Name>^.+$).*#D (?<UT_Code>.+).*#P (?<Date>d+)s+(?<Value1>[0-9.]+)s+(?<Value2>[0-9.]+)s+(?<Vol>[0-9.]+)'
[PSCustomObject]@
# the ".Trim()" was needed to remove leftover EOL/NewLine/space chars
UT_Name = $Matches.UT_Name.Trim()
UT_Code = $Matches.UT_Code.Trim()
Date = $Matches.Date.Trim()
Value1 = $Matches.Value1.Trim()
Value2 = $Matches.Value2.Trim()
Vol = $Matches.Vol.Trim()
$Results |
Export-Csv -LiteralPath "$env:TEMPmilkywaypizza_ProductInfo.csv" -NoTypeInformation
content of the CSV file ...
"UT_Name","UT_Code","Date","Value1","Value2","Vol"
"ABSA ALL ROUNDER FoF","UT-ABSAAG","20190215","393.83","393.83","0.00"
"ABSA BALANCED FUND","UT-ABSABA","20190215","432.28","432.28","0.00"
"COMMUNITY GILT","UT-COM-G","20190215","151.21","151.21","8.59"
add a comment |
this uses named capture groups to get the items and then exports it to a CSV file.
# fake reading in a raw text file
# in real life, use Get-Content -Raw
$InStuff = @'
#N ABSA ALL ROUNDER FoF
#D UT-ABSAAG
#P 20190215 393.83 393.83 0.00
#N ABSA BALANCED FUND
#D UT-ABSABA
#P 20190215 432.28 432.28 0.00
#N COMMUNITY GILT
#D UT-COM-G
#P 20190215 151.21 151.21 8.59
'@
# split into blocks, trim unwanted whitespace, filter out the blank block
$SplitInStuff = ($InStuff -split '#N').Trim().Where($_)
$Results = foreach ($SIS_Item in $SplitInStuff)
$Null = $SIS_Item -match '(?sm)(?<UT_Name>^.+$).*#D (?<UT_Code>.+).*#P (?<Date>d+)s+(?<Value1>[0-9.]+)s+(?<Value2>[0-9.]+)s+(?<Vol>[0-9.]+)'
[PSCustomObject]@
# the ".Trim()" was needed to remove leftover EOL/NewLine/space chars
UT_Name = $Matches.UT_Name.Trim()
UT_Code = $Matches.UT_Code.Trim()
Date = $Matches.Date.Trim()
Value1 = $Matches.Value1.Trim()
Value2 = $Matches.Value2.Trim()
Vol = $Matches.Vol.Trim()
$Results |
Export-Csv -LiteralPath "$env:TEMPmilkywaypizza_ProductInfo.csv" -NoTypeInformation
content of the CSV file ...
"UT_Name","UT_Code","Date","Value1","Value2","Vol"
"ABSA ALL ROUNDER FoF","UT-ABSAAG","20190215","393.83","393.83","0.00"
"ABSA BALANCED FUND","UT-ABSABA","20190215","432.28","432.28","0.00"
"COMMUNITY GILT","UT-COM-G","20190215","151.21","151.21","8.59"
this uses named capture groups to get the items and then exports it to a CSV file.
# fake reading in a raw text file
# in real life, use Get-Content -Raw
$InStuff = @'
#N ABSA ALL ROUNDER FoF
#D UT-ABSAAG
#P 20190215 393.83 393.83 0.00
#N ABSA BALANCED FUND
#D UT-ABSABA
#P 20190215 432.28 432.28 0.00
#N COMMUNITY GILT
#D UT-COM-G
#P 20190215 151.21 151.21 8.59
'@
# split into blocks, trim unwanted whitespace, filter out the blank block
$SplitInStuff = ($InStuff -split '#N').Trim().Where($_)
$Results = foreach ($SIS_Item in $SplitInStuff)
$Null = $SIS_Item -match '(?sm)(?<UT_Name>^.+$).*#D (?<UT_Code>.+).*#P (?<Date>d+)s+(?<Value1>[0-9.]+)s+(?<Value2>[0-9.]+)s+(?<Vol>[0-9.]+)'
[PSCustomObject]@
# the ".Trim()" was needed to remove leftover EOL/NewLine/space chars
UT_Name = $Matches.UT_Name.Trim()
UT_Code = $Matches.UT_Code.Trim()
Date = $Matches.Date.Trim()
Value1 = $Matches.Value1.Trim()
Value2 = $Matches.Value2.Trim()
Vol = $Matches.Vol.Trim()
$Results |
Export-Csv -LiteralPath "$env:TEMPmilkywaypizza_ProductInfo.csv" -NoTypeInformation
content of the CSV file ...
"UT_Name","UT_Code","Date","Value1","Value2","Vol"
"ABSA ALL ROUNDER FoF","UT-ABSAAG","20190215","393.83","393.83","0.00"
"ABSA BALANCED FUND","UT-ABSABA","20190215","432.28","432.28","0.00"
"COMMUNITY GILT","UT-COM-G","20190215","151.21","151.21","8.59"
edited Mar 28 at 0:13
answered Mar 27 at 23:38
Lee_DaileyLee_Dailey
4,2142 gold badges8 silver badges13 bronze badges
4,2142 gold badges8 silver badges13 bronze badges
add a comment |
add a comment |
Other method :
$Data = @'
#N ABSA ALL ROUNDER FoF
#D UT-ABSAAG
#P 20190215 393.83 393.83 0.00
#N ABSA BALANCED FUND
#D UT-ABSABA
#P 20190215 432.28 432.28 0.00
#N COMMUNITY GILT
#D UT-COM-G
#P 2019021 151.21 151.21 8.59
'@
#template for learn schema
$template=@'
#N Vol_ABSA_ALL_ROUNDER_FoF*:ABSA ALL ROUNDER FoF
#D UT-UT_Name:ABSAAG
#P Date:20190215 Value1:393.83 Value2:393.83 Vol:0.00
#N Vol_ABSA_ALL_ROUNDER_FoF*:ABSA ALL ROUNDER FoF 2
#D UT-UT_Name:ABSAAG2
#P Date:20190216 Value1:393.83 Value2:393.83 Vol:0.00
'@
$Data | ConvertFrom-String -TemplateContent $template | export-csv "c:tempresult.csv" -NoType
I think it's important to add the following disclaimer wheneverConvertFrom-String
is used: It provides separator-based parsing as well as heuristics-based parsing based on templates containing example values. The separator-based parsing applies automatic type conversions you cannot control, and the template language is poorly documented, with the exact behavior hard to predict - it's best to avoid this cmdlet altogether. Also note that it's not available in PowerShell Core.
– mklement0
Mar 28 at 12:24
add a comment |
Other method :
$Data = @'
#N ABSA ALL ROUNDER FoF
#D UT-ABSAAG
#P 20190215 393.83 393.83 0.00
#N ABSA BALANCED FUND
#D UT-ABSABA
#P 20190215 432.28 432.28 0.00
#N COMMUNITY GILT
#D UT-COM-G
#P 2019021 151.21 151.21 8.59
'@
#template for learn schema
$template=@'
#N Vol_ABSA_ALL_ROUNDER_FoF*:ABSA ALL ROUNDER FoF
#D UT-UT_Name:ABSAAG
#P Date:20190215 Value1:393.83 Value2:393.83 Vol:0.00
#N Vol_ABSA_ALL_ROUNDER_FoF*:ABSA ALL ROUNDER FoF 2
#D UT-UT_Name:ABSAAG2
#P Date:20190216 Value1:393.83 Value2:393.83 Vol:0.00
'@
$Data | ConvertFrom-String -TemplateContent $template | export-csv "c:tempresult.csv" -NoType
I think it's important to add the following disclaimer wheneverConvertFrom-String
is used: It provides separator-based parsing as well as heuristics-based parsing based on templates containing example values. The separator-based parsing applies automatic type conversions you cannot control, and the template language is poorly documented, with the exact behavior hard to predict - it's best to avoid this cmdlet altogether. Also note that it's not available in PowerShell Core.
– mklement0
Mar 28 at 12:24
add a comment |
Other method :
$Data = @'
#N ABSA ALL ROUNDER FoF
#D UT-ABSAAG
#P 20190215 393.83 393.83 0.00
#N ABSA BALANCED FUND
#D UT-ABSABA
#P 20190215 432.28 432.28 0.00
#N COMMUNITY GILT
#D UT-COM-G
#P 2019021 151.21 151.21 8.59
'@
#template for learn schema
$template=@'
#N Vol_ABSA_ALL_ROUNDER_FoF*:ABSA ALL ROUNDER FoF
#D UT-UT_Name:ABSAAG
#P Date:20190215 Value1:393.83 Value2:393.83 Vol:0.00
#N Vol_ABSA_ALL_ROUNDER_FoF*:ABSA ALL ROUNDER FoF 2
#D UT-UT_Name:ABSAAG2
#P Date:20190216 Value1:393.83 Value2:393.83 Vol:0.00
'@
$Data | ConvertFrom-String -TemplateContent $template | export-csv "c:tempresult.csv" -NoType
Other method :
$Data = @'
#N ABSA ALL ROUNDER FoF
#D UT-ABSAAG
#P 20190215 393.83 393.83 0.00
#N ABSA BALANCED FUND
#D UT-ABSABA
#P 20190215 432.28 432.28 0.00
#N COMMUNITY GILT
#D UT-COM-G
#P 2019021 151.21 151.21 8.59
'@
#template for learn schema
$template=@'
#N Vol_ABSA_ALL_ROUNDER_FoF*:ABSA ALL ROUNDER FoF
#D UT-UT_Name:ABSAAG
#P Date:20190215 Value1:393.83 Value2:393.83 Vol:0.00
#N Vol_ABSA_ALL_ROUNDER_FoF*:ABSA ALL ROUNDER FoF 2
#D UT-UT_Name:ABSAAG2
#P Date:20190216 Value1:393.83 Value2:393.83 Vol:0.00
'@
$Data | ConvertFrom-String -TemplateContent $template | export-csv "c:tempresult.csv" -NoType
answered Mar 28 at 3:45
Esperento57Esperento57
9,5972 gold badges19 silver badges28 bronze badges
9,5972 gold badges19 silver badges28 bronze badges
I think it's important to add the following disclaimer wheneverConvertFrom-String
is used: It provides separator-based parsing as well as heuristics-based parsing based on templates containing example values. The separator-based parsing applies automatic type conversions you cannot control, and the template language is poorly documented, with the exact behavior hard to predict - it's best to avoid this cmdlet altogether. Also note that it's not available in PowerShell Core.
– mklement0
Mar 28 at 12:24
add a comment |
I think it's important to add the following disclaimer wheneverConvertFrom-String
is used: It provides separator-based parsing as well as heuristics-based parsing based on templates containing example values. The separator-based parsing applies automatic type conversions you cannot control, and the template language is poorly documented, with the exact behavior hard to predict - it's best to avoid this cmdlet altogether. Also note that it's not available in PowerShell Core.
– mklement0
Mar 28 at 12:24
I think it's important to add the following disclaimer whenever
ConvertFrom-String
is used: It provides separator-based parsing as well as heuristics-based parsing based on templates containing example values. The separator-based parsing applies automatic type conversions you cannot control, and the template language is poorly documented, with the exact behavior hard to predict - it's best to avoid this cmdlet altogether. Also note that it's not available in PowerShell Core.– mklement0
Mar 28 at 12:24
I think it's important to add the following disclaimer whenever
ConvertFrom-String
is used: It provides separator-based parsing as well as heuristics-based parsing based on templates containing example values. The separator-based parsing applies automatic type conversions you cannot control, and the template language is poorly documented, with the exact behavior hard to predict - it's best to avoid this cmdlet altogether. Also note that it's not available in PowerShell Core.– mklement0
Mar 28 at 12:24
add a comment |
How do I selectively replace runs of multiple whitespace characters?
E.g., by only selecting multiple spaces with a quantifier -replace " 2,",','
The following script based on your one liner:
## Q:Test2019328SO_55387785.ps1
$FileIn = '.zz.txt'
$FileOut= '.zz1.txt'
Set-Content $FileOut -Value "UT_Name,UT_Code,Date,Value1,Value2,Vol"
(Get-Content $FileIn -raw) -replace "(`r?`n)?#N " -replace "`r?`n(#D|#P) | 2,",',' |
Add-Content $FileOut
yields this output:
> Get-Content .ZZ1.txt
UT_Name,UT_Code,Date,Value1,Value2,Vol
ABSA ALL ROUNDER FoF,UT-ABSAAG,20190215,393.83,393.83,0.00
ABSA BALANCED FUND,UT-ABSABA,20190215,432.28,432.28,0.00
COMMUNITY GILT,UT-COM-G,20190215,151.21,151.21,8.59
add a comment |
How do I selectively replace runs of multiple whitespace characters?
E.g., by only selecting multiple spaces with a quantifier -replace " 2,",','
The following script based on your one liner:
## Q:Test2019328SO_55387785.ps1
$FileIn = '.zz.txt'
$FileOut= '.zz1.txt'
Set-Content $FileOut -Value "UT_Name,UT_Code,Date,Value1,Value2,Vol"
(Get-Content $FileIn -raw) -replace "(`r?`n)?#N " -replace "`r?`n(#D|#P) | 2,",',' |
Add-Content $FileOut
yields this output:
> Get-Content .ZZ1.txt
UT_Name,UT_Code,Date,Value1,Value2,Vol
ABSA ALL ROUNDER FoF,UT-ABSAAG,20190215,393.83,393.83,0.00
ABSA BALANCED FUND,UT-ABSABA,20190215,432.28,432.28,0.00
COMMUNITY GILT,UT-COM-G,20190215,151.21,151.21,8.59
add a comment |
How do I selectively replace runs of multiple whitespace characters?
E.g., by only selecting multiple spaces with a quantifier -replace " 2,",','
The following script based on your one liner:
## Q:Test2019328SO_55387785.ps1
$FileIn = '.zz.txt'
$FileOut= '.zz1.txt'
Set-Content $FileOut -Value "UT_Name,UT_Code,Date,Value1,Value2,Vol"
(Get-Content $FileIn -raw) -replace "(`r?`n)?#N " -replace "`r?`n(#D|#P) | 2,",',' |
Add-Content $FileOut
yields this output:
> Get-Content .ZZ1.txt
UT_Name,UT_Code,Date,Value1,Value2,Vol
ABSA ALL ROUNDER FoF,UT-ABSAAG,20190215,393.83,393.83,0.00
ABSA BALANCED FUND,UT-ABSABA,20190215,432.28,432.28,0.00
COMMUNITY GILT,UT-COM-G,20190215,151.21,151.21,8.59
How do I selectively replace runs of multiple whitespace characters?
E.g., by only selecting multiple spaces with a quantifier -replace " 2,",','
The following script based on your one liner:
## Q:Test2019328SO_55387785.ps1
$FileIn = '.zz.txt'
$FileOut= '.zz1.txt'
Set-Content $FileOut -Value "UT_Name,UT_Code,Date,Value1,Value2,Vol"
(Get-Content $FileIn -raw) -replace "(`r?`n)?#N " -replace "`r?`n(#D|#P) | 2,",',' |
Add-Content $FileOut
yields this output:
> Get-Content .ZZ1.txt
UT_Name,UT_Code,Date,Value1,Value2,Vol
ABSA ALL ROUNDER FoF,UT-ABSAAG,20190215,393.83,393.83,0.00
ABSA BALANCED FUND,UT-ABSABA,20190215,432.28,432.28,0.00
COMMUNITY GILT,UT-COM-G,20190215,151.21,151.21,8.59
edited Mar 28 at 13:20
answered Mar 28 at 4:51
LotPingsLotPings
23.8k6 gold badges16 silver badges33 bronze badges
23.8k6 gold badges16 silver badges33 bronze badges
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55387785%2fhow-to-export-multi-line-string-to-single-line-text%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown