PowerShell write integer to file after x number of tabs - powershell

I'm sure this is ridiculously easy, but I'm a noob and trying to learn PowerShell.
I want to write an integer to each line of a tab delimited file, i.e. each line has 20 tabs; put a 1 after the nth tab.
No need to overwrite what's already there because in the current scenario there isn't anything.
Thanks!

If there is a header line then just import the file as a CSV, run it through a ForEach-Object loop and set that column to the integer that you want, then export the CSV again.
Import-CSV $File -Delimiter "`t" | ForEach{$_.ColumnName = $Integer} | Export-CSV $File -Delimiter "`t" -NoTypeInfo
If there is no header you could do the same thing and define your own headers. Except you would use ConvertTo-CSV instead of Export-CSV and then use Select to skip the header row, and use Set-Content to write the file. For my example I set the 7th column to $Integer.
$Headers = 1..20|ForEach{"Col$_"}
Import-CSV $File -Delimiter "`t" -Header $Headers | ForEach{$_.Col7 = $Integer} | ConvertTo-CSV -Delimiter "`t" -NoTypeInfo | Select -Skip 1 | Set-Content $File

Related

Powershell - add leading zeros in CSV File

I built a powershell script in order to format the content of a csv file. It is working fine for the majority of columns except for one column named "Kunde_PLZ"
I want to add leading zeros to the content of that column "Kunde_PLZ" if the number inside the column is shorter than 4 digits. Trying to use padLeft(5,"0") for that.
Import-Csv "c:\temp1.csv" -Delimiter ";" |
% {$_.BESTELLEINGANG = ([datetime]($_.BESTELLEINGANG)).ToString('dd.MM.yyyy');$_.LIEFERDATUM = ([datetime]($_.LIEFERDATUM)).ToString('dd.MM.yyyy');
$_.KUNDENNUMMER =($_.KUNDENNUMMER) -replace '\.', '';$_.KUNDENNUMMER =($_.KUNDENNUMMER) -replace ',.*', '';$_.KUNDE_PLZ = $_.KUNDE_PLZ.padLeft(5,"0") ;$_} |
sort Kundennummer -Descending | Select * -ExcludeProperty "KUNDE_ZUSATZ" |
Export-Csv "c:\resultfile.csv" -Delimiter ";" -Encoding UTF8 -notypeinfo -Force
But no leading zeros are added in the resultfile for the column "KUNDE_PLZ".
I also tried using foreach , but still no leading zeros added
$file = Import-Csv "C:\temp1.csv" -Delimiter ";" |
% {$_.BESTELLEINGANG = ([datetime]($_.BESTELLEINGANG)).ToString('dd.MM.yyyy');$_.LIEFERDATUM = ([datetime]($_.LIEFERDATUM)).ToString('dd.MM.yyyy');
$_.KUNDENNUMMER =($_.KUNDENNUMMER) -replace '\.', '';$_.KUNDENNUMMER =($_.KUNDENNUMMER) -replace ',.*', '';$_} |
sort Kundennummer -Descending | Select * -ExcludeProperty "KUNDE_ZUSATZ"
$file | foreach{$_.KUNDE_PLZ = $_.KUNDE_PLZ.padLeft(5,"0") }
$file | Export-Csv "C:\resultfile.csv" -Delimiter ";" -Encoding UTF8 -notypeinfo -Force
Any ideas how to get the leading zeros added to the column "Kunde_PLZ" inside the csv file ?

Select-Object not selecting object from csv-file

I'm trying to import some csv files to further work with them and export them in the end. They all have two header lines from which i'll only need the second one. I also need to delete most columns except a few. Unfortunately it seems you'll need to decide if you want to skip rows with get-content or exclude columns with import-csv. Neither of those can't do both, so i got a workaround:
$out="bla\bla\out.csv"
$in="bla\bla\in.csv"
$header= (get-content $in -TotalCount 2 )[-1]
$out = Import-csv $in -Header $header -Delimiter ";"|select column1 | Export-Csv -Path $out -NoTypeInformation
this returns an empty csv with the header name column1. What am i doing wrong?
Edit:
The input csv looks like:
filename;filename;...
column1;column2;...
1;a;...
2;b;...
...
I guess that -Header can't read arrays without single quotation marks, so i'm trying to find a solution to that atm.
If you know the name of the header you want to filter on, the following should do the trick and only requires reading the file once:
$out = "out.csv"
$in = "in.csv"
Get-Content $in | Select-Object -Skip 1 |
ConvertFrom-Csv -Delimiter ';' | Select-Object column1 |
Export-Csv $out -NoTypeInformation
If however, you don't know the name of the header you need to filter on (column1 on example above) but you know it's the first column, it would require an extra step:
$csv = Get-Content $in | Select-Object -Skip 1 | ConvertFrom-Csv -Delimiter ';'
$csv | Select-Object $csv[0].PSObject.Properties.Name[0] | Export-Csv $out -NoTypeInformation
We can get the first object of object array ($csv[0]) and get it's properties by accessing it's PSObject.Properties then select the 1st property (.Name[0] - column1 in this case).

Using Powershell to add values to existing CSV

I have been battling this for way too long and I hope you can be of assistance.
I have a CSV for which I need to add some values, preferrably with Powershell. I need to add header row and one column with a fixed text value.
My CSV, before anything done to it, looks like this:
contact-email;contact-sms;contact-firstname
test#example.com;+3580000000;Mike
And I need it to look like this:
contact-email;contact-sms;contact-firstname;order-site
test#example.com;+3580000000;Mike;Finland
So the last column "order-site" needs to be added and every line in that column should have a value of "Finland".
So far I have written this Powershell script I got off a tutorial:
$file = Import-Csv E:\Raportit\SALES\SALES_TD_S01.csv -Delimiter "`t" -Encoding Default -Header "contact-email;contact-sms;contact-firstname"
foreach($c in $file) {
Add-Member -Input $c -MemberType NoteProperty -Name "order-site" -Value "Finland"
}
$file | Export-Csv -Path "C:\Temp\test.csv" -Encoding Default -NoTypeInformation
But unfortunately, this makes the CSV look like this:
"contact-email;contact-sms;contact-firstname","order-site"
"test#example.com;+3580000000;Mike","Finland"
For the use case I have for this file, it need to look like the first should-look-like example, without double quotes and columns separated by semicolon (;). The double quotes are OK as long as the output looks like this:
"contact-email;contact-sms;contact-firstname;order-site"
"test#example.com;+3580000000;Mike;Finland"
I thank you guys so much in advance, I know this is probably a super simple task but I just cannot wrap my head around it to save my life.
if the file HAS headers:
Import-Csv -Path 'E:\Raportit\SALES\SALES_TD_S01.csv' -Delimiter ';' |
Select-Object *, #{Name = 'order-site'; Expression = {'Finland'}} |
Export-Csv -Path "C:\Temp\test.csv" -Delimiter ';' -NoTypeInformation
if the file DOES NOT HAVE headers:
$headers = 'contact-email','contact-sms','contact-firstname'
Import-Csv -Path 'E:\Raportit\SALES\SALES_TD_S01.csv' -Delimiter ';' -Header $headers |
Select-Object *, #{Name = 'order-site'; Expression = {'Finland'}} |
Export-Csv -Path "C:\Temp\test.csv" -Delimiter ';' -NoTypeInformation
CSV Output:
"contact-email";"contact-sms";"contact-firstname";"order-site"
"test#example.com";"+3580000000";"Mike";"Finland"

Import-Csv - Member already present issue

I have to combine multiple CSV files into a single one. Each of the CSV has a header. One of the columns header is identical. Ideally, the end file (all_out.csv) has to have a single header.
I run the PowerShell code:
Import-Csv out_1_result.csv,out_2_result.csv,out_3_result.csv,out_4_result.csv,out_5_result.csv,out_6_result.csv,out_7_result.csv,out_8_result.csv,out_9_result.csv,out_10_result.csv,out_11_result.csv,out_12_result.csv,out_13_result.csv,out_14_result.csv,out_15_result.csv,out_16_result.csv,out_17_result.csv,out_18_result.csv,out_19_result.csv,out_20_result.csv,out_21_result.csv,out_22_result.csv,out_23_result.csv,out_24_result.csv,out_25_result.csv,out_26_result.csv,out_27_result.csv,out_28_result.csv |
Export-Csv all_out.csv -NoType
and I end up with an error
Import-Csv : The member "URL" is already present.
Is there a way to ignore/fix this?
One of the columns header is identical
That means each CSV has two columns header 'URL'? Import-Csv creates objects where each header becomes a property name, e.g. #{Id=10; Url='example.com'} and using the same name again will clash.
There is no way this will work cleanly without you changing the csv files, as there is no way to say "use different column names" and also "skip the header row" just with the Import-Csv cmdlet.
The easiest change I can think of is to drop the header line from each one, e.g.:
$CsvFiles = 'out_1_result.csv','out_2_result.csv','out_3_result.csv','out_4_result.csv','out_5_result.csv','out_6_result.csv','out_7_result.csv','out_8_result.csv','out_9_result.csv','out_10_result.csv','out_11_result.csv','out_12_result.csv','out_13_result.csv','out_14_result.csv','out_15_result.csv','out_16_result.csv','out_17_result.csv','out_18_result.csv','out_19_result.csv','out_20_result.csv','out_21_result.csv','out_22_result.csv','out_23_result.csv','out_24_result.csv','out_25_result.csv','out_26_result.csv','out_27_result.csv','out_28_result.csv'
$NewFileNames = $CsvFiles | ForEach-Object {
$NewFileName = $_ + "_noheader.csv"
Get-Content $_ | Select-Object -Skip 1 | Set-Content $NewFileName -Encoding UTF8
$NewFileName # write new name to output stream
}
And then, when they have no header line, import them all and specify the header line as a parameter
Import-Csv -Path $NewFileNames -Header 'Col1', 'Col2', 'Url1', 'Url2' | Export-Csv ...
Place -Encoding parameter at the end of the command
Import-Csv -Path $output -Encoding UTF8
Export-csv -NoTypeInformation -Path $output -Encoding UTF8

How to select required columns in csv file using powershell

I have a csv file with data looking like this.
Div,Date,HomeTeam,AwayTeam,FTHG,FTAG,FTR,HTHG,HTAG,HTR
I1,20/08/16,Juventus,Fiorentina,2,1,H,1,0,H
I1,20/08/16,Roma,Udinese,4,0,H,0,0,D
I1,21/08/16,Atalanta,Lazio,3,4,A,0,3,A
I1,21/08/16,Bologna,Crotone,1,0,H,0,0,D
I am trying to export first six columns like below to a new csv file.
Div,Date,HomeTeam,AwayTeam,FTHG,FTAG
I1,20/08/16,Juventus,Fiorentina,2,1
I1,20/08/16,Roma,Udinese,4,0
I1,21/08/16,Atalanta,Lazio,3,4
I1,21/08/16,Bologna,Crotone,1,0
I am using the following powershell command.
Import-Csv $infile -DeLimiter ","|
Select'Div','Date','HomeTeam','AwayTeam','FTHG','FTAG'|
Export-Csv $trimfile -DeLimiter "," -NoTypeInformation
But my output csv file looks like this
"Div","Date","HomeTeam","AwayTeam","FTHG","FTAG"
"I1","20/08/16","Juventus","Fiorentina","2","1"
"I1","20/08/16","Roma","Udinese","4","0"
"I1","21/08/16","Atalanta","Lazio","3","4"
"I1","21/08/16","Bologna","Crotone","1","0"
The no.of columns are much higher in the actual file. Simplified it here. What am I doing wrong?
Try with this code :
Import-Csv $infile -DeLimiter "," |
Select-Object 'Div','Date','HomeTeam','AwayTeam','FTHG','FTAG' |
ConvertTo-Csv -NoTypeInformation |
ForEach-Object { $_ -replace '"', ""} |
Out-File $trimfile