I'm trying to replace £ characters in a CSV with GBP.
I created a test CSV (C:\test\test1) as follows:
col1,
£100 Test
I have managed to write the following code:
Import-Csv C:test\test1.csv |
ForEach { if($_.ou -match "£") {$_.OU = $_.OU -replace "£","GBP"}; $_} |
Export-Csv C:\test\test1replaced.csv -NoTypeInformation
My issue is that in the file created the £ symbol seems to be replaced with ? and not GBP. Can't see what I'm doing wrong.
Quoting the answer of Jeroen Mostert:
Replace :
Import-Csv C:test\test1.csv |
ForEach { if($_.ou -match "£") {$_.OU = $_.OU -replace "£","GBP"}; $_} |
Export-Csv C:\test\test1replaced.csv -NoTypeInformation
With this:
Import-Csv C:test\test1.csv -Encoding UTF8 |
ForEach { if($_.ou -match "£") {$_.OU = $_.OU -replace "£","GBP"}; $_} |
Export-Csv C:\test\test1replaced.csv -NoTypeInformation
You should use the -Encoding Switch and set the type as UTF8 to get it done.
Well thanks for the input. It doesn't look like it but it got me to what I came up with:
(get-content C:test\test1.csv).replace('£', 'GBP') | set-content C:\test\test1replaced.csv
Related
I am trying to Remove unnecessary commas in a column in the CSV file. For now, I know a few issues and hard-coded it, But I wanted the code to be dynamic. Any suggestions are greatly appreciated.
$FilePath = "C:\Test\"
Get-ChildItem $FilePath -Filter .csv | ForEach-Object {
(Get-Content $_.FullName -Raw) | Foreach-Object {
$_ -replace ',"Frederick, Fred",' , ',"Frederick Fred",' `
-replace ',"Brian, Josiah",' , ',"Brian Josiah",' `
-replace ',"Lisinopril ,Tablet / 20MG",' , ',"Lisinopril Tablet / 20MG",'
} | Set-Content $_.FullName
}
Try this, also note that I worked with the csv sample that you gave here.It might not work with other csv files.
also make sure that you change the path of %YOURCSVFILE% to the real path of your file
#import the csv
$csv = Import-Csv -Path %YOURCSVFILE% -Delimiter ','
#going each row and replacing commas
foreach ($desc in $csv){
$desc.Desc = $desc.Desc -replace ',',''
}
#exporting the csv
$csv | Export-csv -NoTypeInformation "noCommas.csv"
Here's a few more alteratives for you:
Method 1. Loop through the rows with foreach(..) and capture the output:
$result = foreach ($row in (Import-Csv -Path 'D:\Test\FileWithCommasInDescription.csv')) {
$row.Desc = $row.Desc -replace ','
$row # output the updated item
}
$result | Export-Csv -Path 'D:\Test\FileWithoutCommasInDescription.csv' -NoTypeInformation
Method 2. Use ForEach-Object and the automatic variable $_. Pipe the results through:
Import-Csv -Path 'D:\Test\FileWithCommasInDescription.csv' | ForEach-Object {
$_.Desc = $_.Desc -replace ','
$_ # output the updated item
} | Export-Csv -Path 'D:\Test\FileWithoutCommasInDescription.csv' -NoTypeInformation
Method 3. Use a calculated property:
Import-Csv -Path 'D:\Test\FileWithCommasInDescription.csv' |
Select-Object ID, #{Name = 'Desc'; Expression = {$_.Desc -replace ','}}, Nbr -ExcludeProperty Desc |
Export-Csv -Path 'D:\Test\FileWithoutCommasInDescription.csv' -NoTypeInformation
All will result in a new CSV file
"ID","Desc","Nbr"
"12","Frederick Fred","11"
"21","Brian Josiah","31"
"13","Lisinopril Tablet / 20MG","17"
Below is one of the file data I have in text file
B97SW | CHANGED | rc=0 >>
Server Name";"SystemFolderPath";"IdenityReference";"FileSystemRights";"Vulnerable
B97SW;C:\Windows\system32;CREATOR OWNER;268435456;No
B97SW;C:\Windows\system32;NT AUTHORITY\SYSTEM;268435456;No
B97SW;C:\Windows\system32;NT AUTHORITY\SYSTEM;Modify, Synchronize;No
........
I am trying to replace ";" with "," and write to csv.
Below is the code I wrote but it is not writing the data in csv.
$FileList = Get-ChildItem -Path "C:\Files"
$props=[ordered]#{
ServerName=''
SystemFolderPath=''
IdenityReference=''
FileSystemRights=''
Vulnerable=''
}
New-Object PsObject -Property $props |
Export-Csv C:\2021.csv -NoTypeInformation
$FinalData = #()
foreach($n_file in $FileList)
{
$FileName = $n_file.FullName
$FileContent = Get-Content -Path $FileName | Select-Object -Skip 2
foreach($line in $FileContent)
{
$line = $line -replace(";",",")
$line | Export-Csv -Path C:\2021.csv -Append -NoTypeInformation -Force
}
}
output I am getting
"ServerName","SystemFolderPath","IdenityReference","FileSystemRights","Vulnerable"
"","","","",""
,,,,
,,,,
Please let me know what is wrong I am doing here.
$line | Export-Csv -Path C:\2021.csv -Append -NoTypeInformation -Force
This doesn't work because Export-Csv expects object(s) with properties, but $line is just a string. You need to parse it into an object first, using ConvertFrom-Csv.
Try this:
$FileList = Get-ChildItem -Path "C:\Files"
foreach($n_file in $FileList)
{
$FileName = $n_file.FullName
Get-Content -Path $FileName |
Select-Object -Skip 2 |
ConvertFrom-Csv -Delimiter ';' -Header ServerName, SystemFolderPath, IdenityReference, FileSystemRights, Vulnerable |
Export-Csv -Path C:\2021.csv -Append -NoTypeInformation -Force
}
As we have skipped the original headers, we have to supply these through the -Header parameter of ConvertFrom-Csv.
Your CSV file is goofed up in two ways. First, there is a line of garbage before the header line. Second, in the header line the semi-colons are surrounded by double quotes. The correct form would be to surround the header names with quotes instead.
Once these format errors are fixed, you can read the csv file with this:
Import-Csv myfile.csv -delimiter ";"
Or if you want to produce a comma delimited csv file, try this:
Import-Csv myfile.csv -delimiter ";" | Export-Csv newfile.csv
The result will be correct but it will have a lot of unnecessary double quotes.
What i tried is
import-csv C:\file.csv | export-csv C:\file2.csv -NoTypeInformation -Encoding UTF8
The above code works but is there any way to replace it in the same file?
Example :
Content in csv - text,"text with a,comma",sample
output should be - "text","text with a,comma","sample"
Just wrap the Import-Csv part in brackets so that cmdlet will be finished with the file and you can overwrite the same:
(Import-Csv -Path 'C:\file.csv') | Export-Csv -Path 'C:\file.csv' -NoTypeInformation -Encoding UTF8
P.S. from the question it isn't clear if the CSV file has headers. If not, use the -Header parameter on Import-Csv
Try this:
import-csv C:\file.csv | export-csv C:\file2.csv -NoTypeInformation -Encoding UTF8
Get-Content C:\file2.csv | set-content C:\file.csv
Remove-Item C:\file2.csv
I have a text file where it will say the computer name and current date they logged in.
04/10/2017, "PC1"
04/10/2017, "PC4"
05/10/2017, "PC3"
09/10/2017, "PC2"
I'm having issues trying to run a script that will look for any line that includes "PC2" and delete that line :
get-content "c:\file.csv" | %{if($_ -match "PC2"){$_ -replace $_, ""}} | set-content c:\file.csv
(Get-Content 'C:\File.csv') -notmatch 'PC2' | Set-Content 'C:\File1.csv'
You can also use regex
File extension is csv
Import-Csv 'C:\File.csv' -Header Logged,Computer |
where {$_.Computer -ne 'PC2'} |
Export-Csv 'C:\File.csv' -NoClobber -NoTypeInformation
(Get-Content -Path 'C:\File.csv') |
Where-Object { $_ -notlike '*PC2*' } |
Set-Content -Path 'C:\File.csv'
Here you go. This utilizes an easier-to-understand wildcard comparison operator and just filters out the lines that have the matched string.
So I am looking at breaking up a CSV using Powershell. The CSV is delmited by | which isn't a problem, and I am looking to break it up into multiple smaller csvs while retaining the original. The breaks would occur based off of the value in a single column containing one of a list of values.
What I have done so far is to import the csv (delimited by |) and then
foreach($line in $csv) {
if($columnValue -like $target1) {
export-csv filename1.csv -Delimiter `| $line -append)}
elseif($columnValue -like $target2) {
export-csv filename2.csv -Delimiter `| $line -append)}
etc.
However I do not think it is exporting correctly, and I do not want there to be the quotes (and yes I know this is standard but I do not want them) Also I want the header from the original csv to be applied to the child csvs and its not being applied.
sorry if theres a better way to format the code still new here
Here is where I suggest the awesomeness of the Switch cmdlet. It compares something against multiple potential matches, and executes those matches where appropriate.
Switch($csv){
{$_.column -match $target1} {$_ | Export-CSV filename1.csv -append -delimiter '|'}
{$_.column -match $target2} {$_ | Export-CSV filename2.csv -append -delimiter '|'}
{$_.column -match $target3} {$_ | Export-CSV filename3.csv -append -delimiter '|'}
}
$data = import-csv $csvfile
$data | ?{$_.val -eq $criteria1} | export-csv -path "File1.csv"
$data | ?{$_.val -eq $criteria2} | export-csv -path "File2.csv"