Appending a CSV file to end of another using PowerShell - powershell

I have two CSV files with the same column structure; A.csv and B.csv
I'm looking for a PowerShell way to append B.csv to the end of A.csv, without the header.

You can just try:
Import-Csv a.csv, b.csv | Export-Csv c.csv -notype

I recommend the answer with Import-Csv | Export-Csv for simplicity. Just be aware that it's probably not very efficient to parse both (potentially large) files as CSV when that's not really necessary.
This example doesn't make any assumptions about the structure of the files and just takes file 2 (without the first line) and appends it to file 1:
Get-Content .\file2.txt | where ReadCount -gt 1 >> .\file1.txt

A variation on #JPBlanc's answer:
$files=#("C:\temp\test\a.csv", "C:\temp20170219\test\b.csv")
import-csv $files | export-csv -NoType "c:\temp\c.csv"

Related

From a CSV file get the file header and a portion of the file based on starting and ending line number parameters using PowerShell

So I have a very huge CSV file, the first line has the column headers. I want to keep the first line as a header and add a portion of the file from the file's mid-section or perhaps the end. I'm also trying to select only a few of the columns from the file. And finally, it would be great if the solution also changed the file delimiter from a comma to a tab.
I'm aiming for a solution that's a one-liner or perhaps 2?
Non-working Code version 30 ...
Get-Content -Tail 100 filename.csv | Export-Csv -Delimiter "`t" -NoTypeInformation -Path .\filename_out.csv
I'm trying to get a better grip on PowerShell. So far, so good but I'm not quite there yet. But trying to solve such challenges are helping me (and hopefully others) build a good collection of coding idioms. (FYI - the boss is trying PowerShell due to our efforts so.)
OK thanks to iRon tip. Import-CSV defaults to comma separated, the Select-Object -Property get the columns I want, the select -Last gets the last 200 rows, and the Export-CSV changes the delimiter to a tab:
Import-Csv iarf.csv |
Select-Object -Property Id,Name,RecordTypeId,CreatedDate |
select -Last 200 |
Export-Csv -Delimiter "`t" -NoTypeInformation -Path .\iarf100props6.csv
iRon provided the crucial pointer: Using Import-Csv rather than Get-Content allows you to retrieve arbitrary ranges from the original file as objects, if selected via Select-Object, and exporting these objects again via Export-Csv automatically includes a header line whose column names are the input objects' property names, as initially derived from the input file's header line.
In order to select an arbitrary range of rows, combine Select-Object's -Skip and -First parameters:
To only get rows from the beginning, use just -First $count:
To only get rows from the end, use just -Last $count
To get rows in a given range, use just -Skip $startRowMinus1 -First $rangeRowCount
For instance, the following command extracts rows 10 through 30:
Import-Csv iarf.csv |
Select-Object -Property Id,Name,RecordTypeId,CreatedDate -Skip 9 -First 20 |
Export-Csv -Delimiter "`t" -NoTypeInformation -Path .\iarf100props6.csv

Adding Extra Headers in CSV

Input CSV:
CHeader1,CHeader2,CHeader3,CHeader4,CHeader5
a1,a2,a3,a4,a5
b1,b2,b3,b4,b5
Output CSV:
PHeader1,PHeader2
CHeader1,CHeader2,CHeader5
a1,a2,a5
b1,b2,b5
Already tried
Import-Csv -Path .\before.csv |
select CHeader1, CHeader2, CHeader5 |
Export-Csv -Path .\after.csv
This produces the file without parent level headers.
Any suggestions to add parent level headers in first line of CSV followed by client headers and then the data?
What you're trying to create there is not actually a CSV, at least not in a way that the *-Csv cmdlets could handle. You can manually create it like this, though:
'PHeader1,PHeader2' | Set-Content '.\after.csv'
Import-Csv '.\before.csv' |
Select-Object CHeader1, CHeader2, CHeader5 |
ConvertTo-Csv -NoType |
Add-Content '.\after.csv'

How to append datatable to existing records in CSV file

I have CSV file MyFile.csv. It contains following data:
RollNumber Name
1 Amol
2 Ravi
Now I am fetching few records from SQL Server as follows:
RollNumber Name
3 Viku
4 Vaibhav
I am not able to find a way to append these new records to existing CSV file. I don't want to repeat header again. I don't have MS Excel, just want to play with CSV. I tried using Add-content, but it didn't worked out.
Since PowerShell v3 the Export-Csv cmdlet has an -Append parameter that allows appending to an existing CSV.
$data | Export-Csv 'MyFile.csv' -Append -NoType
On earlier versions you can work around that by converting the data to CSV, skip the header line, then append to the output file using Add-Content or Out-File -Append:
$data | ConvertTo-Csv -NoType | select -Skip 1 | Add-Content 'MyFile.csv'

Removing Header and Footer from imported .csv

I have 3 .csv files that I am combining into one. This bit of code works:
Get-ChildItem 'C:\Scripts\testing\csvStuffer\temp\Individual.*.csv' |
ForEach-Object {Import-Csv $_} |
Export-Csv -NoTypeInformation 'C:\Scripts\testing\csvStuffer\temp\MergedCsvFiles.csv'
The problem is that each .csv file has a header and a footer.
I do not want to keep the header or footer from any of the files.
Any suggestions of what I need to add to the above code to remove the headers and footers???
Thanks!
This is not the most elegant solution but it worked for my test files.
Get-ChildItem 'C:\Scripts\testing\csvStuffer\temp\Individual.*.csv' |
ForEach-Object {
$filecontent = get-content $_ | select-object -skip 1;
$filecontent | select -First $($filecontent.length -1) | Set-Content -Path $_;
};
Skipping the first line is easy with select-object. Dropping the last line requires a bit more work, but since get-content returns an array of lines, you can just grab all but the last element in that array.
Looks like alroc already gave an answer, but since I already had it written up I figured I'd post this too. It doesn't load it all into a variable, it just reads each file, strips the first and last line of the current file, and then pipes to out-file with -append on it.
gci 'C:\Scripts\testing\csvStuffer\temp\Individual.*.csv' | %{
$(gc $_.fullname|skip 1)|select -First ($(gc $_.fullname|skip 1).count-1)
}|Out-File -Append 'C:\Scripts\testing\csvStuffer\temp\MergedCsvFiles.csv'

Merge One CSV file into another

I have path1\sample1.csv and path2\sample2.csv file with same column attributes. I need to merge both files with different path into path1\sample1.csv using PowerShell.
Something like this?
Import-CSV path\sample1.csv | Export-CSV path1\sample1.csv
Import-CSV path\sample2.csv | Export-CSV path1\sample1.csv -Append -NoTypeInformation