Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Saturday, June 3, 2017 9:27 AM
I have a text file with following sample data:
aa bb cc dd
ee ff gg hh
ii jj kk ll
I need to add 2 more columns to this file at the start and fill those with a pre-defined value. How can we do this?
Please note that columns are not having any headers.
All replies (4)
Saturday, June 3, 2017 10:35 AM ✅Answered
I think you have different options. Even if there are no headers it looks like a csv file. So you could import the data as csv, add the data you need and export it again as csv.
Or you just read it as plain text and add what you need with string operations.
Depending on your approach you will need the cmdlets Import-CSV, Export-CSV, Get-Content, Set-Content, Out-File, Foreach-Object and maybe some common string operators like -join or -split.
Good luck and have a lot of fun!
Grüße - Best regards
PS:> (79,108,97,102|%{[char]$_})-join''
Saturday, June 3, 2017 11:24 AM
Per BOfH:
Import-CSV file -header c1,c2,c3 -Delimiter ' '| select *, @{n='newc4';e={'newvalue'}}
You can then save this using a custom output statement.
\(ツ)_/
Saturday, June 3, 2017 4:23 PM
yeah - I did it using a simple string operation:
(Get-Content -Path "My File Path") |ForEach-Object{$_ = "Hello Hello " +$_;echo $_}
Thank you.
Saturday, June 3, 2017 5:41 PM
Obviously sometimes we think just too sophisticated. ;-)
Grüße - Best regards
PS:> (79,108,97,102|%{[char]$_})-join''