Powershell Import-csv with return character - powershell
I tried the following to turn a text file into a document by leveraging import-csv where each item in the original document was a new line
Sample file.txt
James Cameron
Kirk Cobain
Linda Johnson
Code:
$array = import-csv file.txt | ConvertFrom-Csv -Delim `r
foreach ($Data in $array)
{
if (sls $Data Master.txt -quiet)
{Add-Content file.txt $Data}
}
It never created the document
Import-Csv takes a CSV and outputs PSCustomObjects. It's intended for when the file has a header row, and it reads that as the properties of the objects. e.g.
FirstName,LastName
James,Cameron
Kirk,Cobain
# ->
#{FirstName='James';LastName='Cameron'}
#{FirstName='Kirk';LastName='Cobain'}
etc.
If your file has no header row, it will take the first row and then ruin everything else afterwards. You need to provide the -Header 'h1','h2',... parameter to fix that. So you could use -Header Name, but your data only has one property, so there's not much benefit.
ConvertFrom-Csv is intended to do the same thing, but from CSV data in a variable instead of a file. They don't chain together usefully. It will try, but what you end up with is...
A single object, with a property called '#{James=Kirk}' and a value of '#{James=Linda}', where 'James' was taken from line 1 as a column header, and the weird syntax is from forcing those objects through a second conversion.
It's not at all clear why you are reading in from file.txt and adding to file.txt. But since you don't have a CSV, there's no benefit from using the CSV cmdlets.
$lines = Get-Content file.txt
$master = Get-Content master.txt
foreach ($line in $lines)
{
if ($master -contains $line)
{
Add-Content file2.txt $line
}
}
or just
gc file.txt |? { sls $_ master.txt -quiet } | set-content file2.txt
Auto-generated PS help links from my codeblock (if available):
gc is an alias for Get-Content (in module Microsoft.PowerShell.Management)
? is an alias for Where-Object
sls is an alias for Select-String (in module Microsoft.PowerShell.Utility)
Set-Content (in module Microsoft.PowerShell.Management)
Related
Remove additional commas in CSV file using Powershell
I have a csv file that I'll like to import to sql but isn't properly formatted. I am not able to format the generated file (excel file) so I'm looking to do this with the CSV file using. I want to remove the extra commas and also replace the department name (,,,,,,) with the correct department as seen in the example below. Thank you in advance. Example: Current Format: Department,,,,,,First Name,,,,Last Name,,,,,,,School Year,Enrolment Status Psychology ,,,,,,,,,,,,,,,,,,,,,,, (Remove this line) ,,,,,,Jane,,,,Doe,,,,,,,2022,Enrolled ,,,,,,Jeff,,,,Dane,,,,,,,2019,Enrolled ,,,,,,Tate,,,,Anderson,,,,,,,2019,Not Enrolled ,,,,,,Daphne,,,,Miller,,,,,,,2021,Enrolled ,,,,,,Cora,,,,Dame,,,,,,,2022,Enrolled Computer Science ,,,,,,,,,,,,,,,,,,,,,,, (Remove this line) ,,,,,,Dora,,,,Explorer,,,,,,,2022,Not Enrolled ,,,,,,Peppa,,,,Diggs,,,,,,,2020,Enrolled ,,,,,,Conrad,,,,Strat,,,,,,,2020,Enrolled ,,,,,,Kat,Noir,,,,2019,,,,,,,Enrolled ,,,,,,Lance,,,,Bug,2018,,,,,,,Enrolled Ideal format: Department,First Name,Last Name,School Year,Enrolment Status Psychology ,,,,,,,,,,,,,,,,,,,,,,, (Remove this line) Psychology,Jane,Doe,2022,Enrolled Psychology,Jeff,Dane,2019,Enrolled Psychology,Tate,Anderson,2019,Not Enrolled Psychology,Daphne,Miller,2021,Enrolled Psychology,Cora,Dame,2022,Enrolled Computer Science ,,,,,,,,,,,,,,,,,,,,,,, (Remove this line) Computer Science,Dora,Explorer,2022,Not Enrolled Computer Science,Peppa,Diggs,2020,Enrolled Computer Science,Conrad,Strat,2020,Enrolled Computer Science,Kat,Noir,2019,Enrolled Computer Science,Lance,Bug,2018,Enrolled
here you go: $csvArray = new-object System.Collections.Generic.List[string] #Import the file $text = (gc "C:\tmp\testdata.txt") -replace ",{2,}","," $arrayEnd = $text.count -1 $text[1..$arrayEnd] | %{ If ($_ -notmatch "^(,)"){ $department = $_ -replace "," } Else { $csvArray.add($department + $_) } } $csvArray.Insert(0,$text[0]) $csvArray | set-content 'C:\tmp\my.csv'
Using the Csv cmdlets: $Csv = #' Department,,,,,,First Name,,,,Last Name,,,,,,,School Year,Enrolment Status Psychology ,,,,,,,,,,,,,,,,,,,,,,, (Remove this line) ,,,,,,Jane,,,,Doe,,,,,,,2022,Enrolled ,,,,,,Jeff,,,,Dane,,,,,,,2019,Enrolled ,,,,,,Tate,,,,Anderson,,,,,,,2019,Not Enrolled ,,,,,,Daphne,,,,Miller,,,,,,,2021,Enrolled ,,,,,,Cora,,,,Dame,,,,,,,2022,Enrolled Computer Science ,,,,,,,,,,,,,,,,,,,,,,, (Remove this line) ,,,,,,Dora,,,,Explorer,,,,,,,2022,Not Enrolled ,,,,,,Peppa,,,,Diggs,,,,,,,2020,Enrolled ,,,,,,Conrad,,,,Strat,,,,,,,2020,Enrolled ,,,,,,Kat,Noir,,,,2019,,,,,,,Enrolled ,,,,,,Lance,,,,Bug,2018,,,,,,,Enrolled '# $List = ConvertFrom-Csv $Csv -Header #(1..20) # |Import-Csv .\Your.Csv -Header #(1..20) $Columns = $List[0].PSObject.Properties.Where{ $_.Value -and $_.Value -ne 'Department' }.Name $List |Select-Object -Property $Columns |Where-Object { $_.$($Columns[0]) } | ConvertTo-Csv -UseQuote Never |Select-Object -Skip 1 # |Set-Content -Encoding utf8 out.csv First Name,Last Name,School Year,Enrolment Status Jane,Doe,2022,Enrolled Jeff,Dane,2019,Enrolled Tate,Anderson,2019,Not Enrolled Daphne,Miller,2021,Enrolled Cora,Dame,2022,Enrolled Dora,Explorer,2022,Not Enrolled Peppa,Diggs,2020,Enrolled Conrad,Strat,2020,Enrolled Kat,,,Enrolled Lance,Bug,,Enrolled
Use a switch statement: & { $first = $true switch -Wildcard -File in.csv { # Loop over all lines in file in.csv ',*' { # intra-department line # Prepend the department name, eliminate empty fields and output. $dept + (($_ -split ',' -ne '') -join ',') } default { if ($first) { # header line # Eliminate empty fields and output. ($_ -split ',' -ne '') -join ',' $first = $false } else { # department-only line $dept = ($_ -split ',')[0] # save department name } } } } | Set-Content -Encoding utf8 out.csv Note: $_ -split ',' splits each line into fields by ,, and -ne '' filters out empty fields from the resulting array; applying -join ',' rejoins the nonempty fields with ,, which in effect removes multiple adjacent , and thereby eliminates empty fields. If you don't mind the complexity of a regex, you can perform the above more simply with a single -replace operation, as shown in Toni's helpful answer. Using switch -File is an efficient way to read files line by line and perform conditional processing based on sophisticated matching (as an alternative to -Wildcard you can use -Regex for regex matching, and you can even use script blocks ({ ... } as conditionals). As a language statement, switch cannot be used directly in a pipeline. This limitation can be overcome by enclosing it in a script block ({ ... }) invoked with &, which enables the usual, memory-friendly streaming behavior in the pipeline; that is, the lines are processed one by one, as are the modified output lines relayed to Set-Content, so that the input file needn't be read into memory as a whole. In your case, plain-text processing of your CSV file enabled a simple solution, but in general it is better to parse CSV files into objects whose properties you can work with, using the Import-Csv cmdlet, and, for later re-exporting to a CSV file, Export-Csv,
Is there a way to merge similar lines using Powershell?
Suppose I have two csv files. One is id_number,location_code,category,animal,quantity 12212,3,4,cat,2 29889,7,6,dog,2 98900, 33221,1,8,squirrel,1 the second one is: 98900,2,1,gerbil,1 The second file may have a newline or something at the end (maybe or maybe not, I haven't checked), but only the one line of content. There may be three or four or more different varieties of the "second" file, but each one will have a first element (98900 in this example) that corresponds to an incomplete line in the first file similar to what is in this example. Is there a way using powershell to automatically merge the line in the second (plus any additional similar) csv file into the matching line(s) of the first file, so that the resulting file is: 12212,3,4,cat,2 29889,7,6,dog,2 98900,2,1,gerbil,1 33221,1,8,squirrel,1
main.csv id_number,location_code,category,animal,quantity 12212,3,4,cat,2 29889,7,6,dog,2 98900, 33221,1,8,squirrel,1 correction_001.csv 98900,2,1,gerbil,1 merge code used at the commandline, or in the .ps1 file of your choice $myHeader = #('id_number','location_code','category','animal','quantity') #Stage all the correction files: last correction in the most recent file wins $ToFix = #{} filter Plumbing_Import-Csv($Header){import-csv -LiteralPath $_ -Header $Header} ls correction*.csv | sort -Property LastWriteTime | Plumbing_Import-Csv $myHeader | %{$ToFix[$_.id_number]=$_} function myObjPipe($Header){ begin{ function TextTo-CsvField([String]$text){ #text fields which contain comma, double quotes, or new-line are a special case for CSV fields and need to be accounted for if($text -match '"|,|\n'){return '"'+($text -replace '"','""')+'"'} return $text } function myObjTo-CsvRecord($obj){ return ''+ $obj.id_number +','+ $obj.location_code +','+ $obj.category +','+ (TextTo-CsvField $obj.animal)+','+ $obj.quantity } $Header -join ',' } process{ if($ToFix.Contains($_.id_number)){ $out = $ToFix[$_.id_number] $ToFix.Remove($_.id_number) }else{$out = $_} myObjTo-CsvRecord $out } end{ #I assume you'd append any leftover fixes that weren't used foreach($out in $ToFix.Values){ myObjTo-CsvRecord $out } } } import-csv main.csv | myObjPipe $myHeader | sc combined.csv -encoding ascii You could also use ConvertTo-Csv, but my preference is to not have all the extra " cruft. Edit 1: reduced code redundancy, accounted for \n, fixed appends, and used #OwlsSleeping suggestion about the -Header commandlet parameter also works with these files: correction_002.csv 98900,2,1,I Win,1 correction_new.csv 98901,2,1,godzilla,1 correction_too.csv 98902,2,1,gamera,1 98903,2,1,mothra,1 Edit 2: convert gc | ConvertTo-Csv over to Import-Csv to fix the front-end \n issues. Now also works with: correction_003.csv 29889,7,6,"""bad"" monkey",2
This is a simple solution assuming there's always exactly one match, and you don't care about output order. Change the output path to csv1 to overwrite. I added headers manually in both input files, but you can specify them in Import-Csv instead if you'd rather avoid changing your files. [array]$MissingLine = Import-Csv -Path "C:\Users\me\Documents\csv2.csv" [string]$MissingId = $MissingLine[0].id_number [array]$BigCsv = Import-Csv -Path "C:\Users\me\Documents\csv1.csv" | Where-Object {$_.id_number -ne $MissingId} ($BigCsv + $MissingLine) | Export-Csv -Path "C:\Users\me\Documents\Combined.csv"
Powershell replace text once per line
I have a Powershell script that I am trying to work out part of it, so the text input to this is listing the user group they are part of. This PS script is supposed to replace the group with the groups that I am assigning them in active directory(I am limited to only changing groups in active directory). My issue is that when it reaches HR and replaces it, it will then proceed to contine and replace all the new but it all so replaces the HR in CHRL, so my groups look nuts right now. But I am looking it over and it doesn't do it with every line. But for gilchrist it will put something in there for the HR in the name. Is there anything can I do to keep it for changing or am I going to have to change my HR to Human Resources? Thanks for the help. $lookupTable = #{ 'Admin' = 'W_CHRL_ADMIN_GS,M_CHRL_ADMIN_UD,M_CHRL_SITE_GS' 'Security' = 'W_CHRL_SECURITY_GS,M_CHRL_SITE_GS' 'HR' = 'M_CHRL_HR_UD,W_CHRL_HR_GS,M_CHRL_SITE_GS' $original_file = 'c:\tmp\test.txt' $destination_file = 'c:\tmp\test2.txt' Get-Content -Path $original_file | ForEach-Object { $line = $_ $lookupTable.GetEnumerator() | ForEach-Object { if ($line -match $_.Key) { $line = $line -replace $_.Key, $_.Value } } $line } | Set-Content -Path $destination_file Get-Content $destination_file test.txt: user,group john.smith,Admin joanha.smith,HR john.gilchrist,security aaron.r.smith,admin abby.doe,secuity abigail.doe,admin
Your input appears to be in CSV format (though note that your sample rows have trailing spaces, which you'd have to deal with, if they're part of your actual data). Therefore, use Import-Csv and Export-Csv to read / rewrite your data, which allows a more concise and convenient solution: Import-Csv test.txt | Select-Object user, #{ Name='group'; Expression = { $lookupTable[$_.group] } } | Export-Csv -NoTypeInformation -Encoding Utf8 test2.txt Import-Csv reads the CSV file as a collection of custom objects whose properties correspond to the CSV column values; that is, each object has a .user and .name property in your case. $_.group therefore robustly reports the abstract group name only, which you can directly pass to your lookup hashtable; Select-Object is used to pass the original .user value through, and to replace the original .group value with the lookup result, using a calculated property. Export-Csv re-converts the custom objects to a CSV file: -NoTypeInformation suppresses the (usually useless) data-type-information line at the top of the output file -Encoding Utf8 was added to prevent potential data loss, because it is ASCII encoding that is used by default. Note that Export-Csv blindly double-quotes all field values, whether they need it or not; that said, CSV readers should be able to deal with that (and Import-Csv certainly does). As for what you tried: The -replace operator replaces all occurrences of a given regex (regular expression) in the input. Your regexes amounts to looking for (case-insensitive) substrings, which explains why HR matches both the HR group name and substring hr in username gilchrist. A simple workaround would be to add assertions to your regex so that the substrings only match where you want them; e.g.: ,HR$ would only match after a , at the end of a line ($). However, your approach of enumerating the hashtable keys for each input CSV row is inefficient, and you're better off splitting off the group name and doing a straight lookup based on it: # Split the row into fields. $fields = $line -split ',' # Update the group value (last field) $fields[-1] = $lookupTable[$fields[-1]] # Rebuild the line $line = $fields -join ',' Note that you'd have to make an exception for the header row (e.g., test if the lookup result is empty and refrain from updating, if so).
Why don't you load your text file as a CSV file, using Import-CSV and use "," as a delimiter? This will allow you to have a Powershell Object you can work on. and then export it as text o CSV. if I use your file & lookup table this code may help you : $file = Import-Csv -Delimiter "," -Path "c:\ps\test.txt" $lookupTable = #{ 'Admin' = 'W_CHRL_ADMIN_GS,M_CHRL_ADMIN_UD,M_CHRL_SITE_GS' 'Security' = 'W_CHRL_SECURITY_GS,M_CHRL_SITE_GS' 'HR' = 'M_CHRL_HR_UD,W_CHRL_HR_GS,M_CHRL_SITE_GS'} foreach ($i in $file) { #Compare and replace ... } Export-CSV $file -Delimiter "," You can then iterate over $file and compare and replace. you can also Export-CSV after you're done.
Removing a single line from CSV
I have a CSV with several rows and two columns: "Name" and "Information". I'm looping through the CSV and checking each line for a condition, if the condition is met I'd like to remove the line: for ($i=0; $i -le $CSV.length; $i++) { if ($CSV.name == "Fred") { #remove $CSV[$i] -- that one line; both "Name" and "Information" } } I've seen solutions that use Get-Content or Import-Csv and temporary file(s) but I haven't wrapped my head around it and figure there must be an easier way. Regardless, any help is appreciated!
I would read the CSV using the Import-CSV cmdlet and use the Where-Object cmdlet to filter the entries. Finally, write the CSV back using the Export-CSV cmdlet: $csv = import-csv 'YourPath.csv' $csv | Where Name -ne 'Fred' | Export-Csv 'YourPath.csv' -NoTypeInformation
Powershell. Writing out lines based on string within the file
I'm looking for a way to export all lines from within a text file where part of the line matches a certain string. The string is actually the first 4 bytes of the file and I'd like to keep the command to only checking those bytes; not the entire row. I want to write the entire row. How would I go about this? I am using Windows only and don't have the option to use many other tools that might do this. Thanks in advance for any help.
Do you want to perform a simple "grep"? Then try this select-string .\test.txt -pattern "\Athat" | foreach {$_.Line} or this (very similar regex), also writes to an outfile select-string .\test.txt -pattern "^that" | foreach {$_.Line} | out-file -filepath out.txt This assumes that you want to search for a 4-byte string "that" at the beginning of the string , or beginning of the line, respectively.
Something like the following Powershell function should work for you: function Get-Lines { [cmdletbinding()] param( [string]$filename, [string]$prefix ) if( Test-Path -Path $filename -PathType Leaf -ErrorAction SilentlyContinue ) { # filename exists, and is a file $lines = Get-Content $filename foreach ( $line in $lines ) { if ( $line -like "$prefix*" ) { $line } } } } To use it, assuming you save it as get-lines.ps1, you would load the function into memory with: . .\get-lines.ps1 and then to use it, you could search for all lines starting with "DATA" with something like: get-lines -filename C:\Files\Datafile\testfile.dat -prefix "DATA" If you need to save it to another file for viewing later, you could do something like: get-lines -filename C:\Files\Datafile\testfile.dat -prefix "DATA" | out-file -FilePath results.txt Or, if I were more awake, you could ignore the script above, use a simpler solution such as the following one-liner: get-content -path C:\Files\Datafile\testfile.dat | select-string -Pattern "^DATA" Which just uses the ^ regex character to make sure it's only looking for "DATA" at the beginning of each line.
To get all the lines from c:\somedir\somefile.txt that begin with 'abcd' : (get-content c:\somedir\somefile.txt) -like 'abcd*' provided c:\somedir\somefile.txt is not an unusually large (hundreds of MB) file. For that situation: get-content c:\somedir\somefile.txt -readcount 1000 | foreach {$_ -like 'abcd*'}