\"Event Date and Time\",\"User\",\"OS Host Names\",\"Source IP\",\"Destination IP\",\"Server Group\",\"Service\",\"Operation\",\"Object\",\"Query\",\"SQL Exception Occurred\",\"Source Application\"\n<\/code><\/pre>\nThe $_ variable in the ForEach_Object loop is picking up the correct value for the Query field<\/p>","upvoteCount":0,"datePublished":"2016-10-03T09:26:13.000Z","url":"https://community.spiceworks.com/t/truncating-a-field-in-a-csv-file-if-it-is-too-long/529854/5","author":{"@type":"Person","name":"iainsmart","url":"https://community.spiceworks.com/u/iainsmart"}},{"@type":"Answer","text":"
Give me one or 2 fake lines… and 'll see what i can come up with on my machine later<\/p>\n
Does the total length of one line has to be 30000chars or less or just the query entry?<\/p>","upvoteCount":0,"datePublished":"2016-10-03T09:44:00.000Z","url":"https://community.spiceworks.com/t/truncating-a-field-in-a-csv-file-if-it-is-too-long/529854/6","author":{"@type":"Person","name":"edwineekelaers2","url":"https://community.spiceworks.com/u/edwineekelaers2"}},{"@type":"Answer","text":"
It’s just the Query entry (Excel has a limit of 32,767 characters per cell, but even using smaller numbers has the same issue!)<\/p>\n
Example csv:<\/p>\n
\"Event Date and Time\",\"User\",\"OS Host Names\",\"Source IP\",\"Destination IP\",\"Server Group\",\"Service\",\"Operation\",\"Object\",\"Query\",\"SQL Exception Occurred\",\"Source Application\"\n\"09/29/2016 2:30:13 AM\",\"User1\",\"\",\"aaa.bbb.ccc.ddd\",\"aaa.bbb.ccc.ddd\",\"Server Group 1\",\"Service 1\",\"Update\",\"sysjobactivity\",\"DECLARE @nextScheduledRunDate DATETIME SET @nextScheduledRunDate = msdb.dbo.agent_datetime(@P1, @P2) UPDATE msdb.dbo.sysjobactivity SET next_scheduled_run_date = @nextScheduledRunDate WHERE session_id = @P3 AND job_id = @P4\",\"No Exception Occurred\",\"\"\n\"09/29/2016 8:53:22 AM\",\"User2\",\"\",\"aaa.bbb.ccc.ddd\",\"aaa.bbb.ccc.ddd\",\"Server Group 2\",\"Service 2\",\"set deadlock_priority\",\"\",\"SET ROWCOUNT 0 SET TEXTSIZE 2147483647 SET NOCOUNT OFF SET CONCAT_NULL_YIELDS_NULL ON SET ARITHABORT ON SET LOCK_TIMEOUT -1 SET QUERY_GOVERNOR_COST_LIMIT 0 SET DEADLOCK_PRIORITY NORMAL SET TRANSACTION ISOLATION LEVEL READ COMMITTED SET ANSI_NULLS ON SET ANSI_NULL_DFLT_ON ON SET ANSI_PADDING ON SET ANSI_WARNINGS ON SET CURSOR_CLOSE_ON_COMMIT OFF SET IMPLICIT_TRANSACTIONS OFF SET QUOTED_IDENTIFIER ON\",\"No Exception Occurred\",\"\"\n\"09/29/2016 1:37:09 PM\",\"User3\",\"\",\"aaa.bbb.ccc.ddd\",\"aaa.bbb.ccc.ddd\",\"Server Group 1\",\"Service 1\",\"\",\"\",\"SELECT SYSTEM_USER\",\"No Exception Occurred\",\"\"\n<\/code><\/pre>\nThis CSV has3 rows with Query field lengths of 223, 401 and 8. Trying to truncate row 2 to 300 characters has the same issue as 30000 chars in my CSV.<\/p>","upvoteCount":0,"datePublished":"2016-10-03T10:02:23.000Z","url":"https://community.spiceworks.com/t/truncating-a-field-in-a-csv-file-if-it-is-too-long/529854/7","author":{"@type":"Person","name":"iainsmart","url":"https://community.spiceworks.com/u/iainsmart"}},{"@type":"Answer","text":"
$csv = @\"\none,two,three\n1,2,3\n100,200,300\n1000,2000,3000\n1,2000,3\n\"@ -split \"`r`n\"\n\n$csv = ConvertFrom-Csv $csv\n\n$properties = $csv | \nGet-Member -MemberType NoteProperty | \nSelect-Object -ExpandProperty Name\n\n$new_csv_data = ForEach($line in $csv) {\n $props = @{}\n\n ForEach($property in $properties) {\n If($line.\"$property\".Length -gt 3) {\n $props[$property] = $line.\"$property\".Substring(0,3)\n } Else {\n $props[$property] = $line.\"$property\"\n }\n }\n\n New-Object -TypeName PSObject -Property $props\n}\n\n$new_csv_data |\nSelect-Object -Property $properties\n<\/code><\/pre>\nThere is an example that trims every cell if it is longer than 3.<\/p>","upvoteCount":1,"datePublished":"2016-10-03T10:23:23.000Z","url":"https://community.spiceworks.com/t/truncating-a-field-in-a-csv-file-if-it-is-too-long/529854/9","author":{"@type":"Person","name":"craigduff","url":"https://community.spiceworks.com/u/craigduff"}},{"@type":"Answer","text":"\n\n
<\/div>\n
cduff:<\/div>\n
\n$csv = @\"\none,two,three\n1,2,3\n100,200,300\n1000,2000,3000\n1,2000,3\n\"@ -split \"`r`n\"\n\n$csv = ConvertFrom-Csv $csv\n\n$properties = $csv | \nGet-Member -MemberType NoteProperty | \nSelect-Object -ExpandProperty Name\n\n$new_csv_data = ForEach($line in $csv) {\n $props = @{}\n\n ForEach($property in $properties) {\n If($line.\"$property\".Length -gt 3) {\n $props[$property] = $line.\"$property\".Substring(0,3)\n } Else {\n $props[$property] = $line.\"$property\"\n }\n }\n\n New-Object -TypeName PSObject -Property $props\n}\n\n$new_csv_data |\nSelect-Object -Property $properties\n<\/code><\/pre>\nThere is an example that trims every cell if it is longer than 3.<\/p>\n<\/blockquote>\n<\/aside>\n
Nice … Am going to steal that code.<\/p>","upvoteCount":0,"datePublished":"2016-10-03T10:25:00.000Z","url":"https://community.spiceworks.com/t/truncating-a-field-in-a-csv-file-if-it-is-too-long/529854/10","author":{"@type":"Person","name":"edwineekelaers2","url":"https://community.spiceworks.com/u/edwineekelaers2"}},{"@type":"Answer","text":"
For whatever reason, putting the field name in lower case works!<\/p>","upvoteCount":0,"datePublished":"2016-10-03T11:45:50.000Z","url":"https://community.spiceworks.com/t/truncating-a-field-in-a-csv-file-if-it-is-too-long/529854/11","author":{"@type":"Person","name":"iainsmart","url":"https://community.spiceworks.com/u/iainsmart"}}]}}
iainsmart
(Iain Smart)
October 3, 2016, 7:45am
1
As my first PS project, I am trying to loop through a large CSV file (>25k lines, circa 30Mb) and truncate the ‘Query’ field if it’s character count is larger than a certain value (i.e. larger than Excel can handle).
I have this following code:
Function Get-FileName($initialDirectory)
{
[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null
$OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
$OpenFileDialog.initialDirectory = $initialDirectory
$OpenFileDialog.filter = "CSV (*.csv)| *.csv"
$OpenFileDialog.ShowDialog() | Out-Null
$OpenFileDialog.filename
}
$inputfile = Get-FileName "C:\Logs"
$csv = Import-CSV $inputfile
$csv.Query | ForEach-Object -Process {
if((Measure-Object -Character) -gt 30000)
{
$csv.Query = $csv.Query.String(30000)
}
}
Export-Csv -Path $inputfile -NoTypeInformation
However I am getting the following error:
“Cannot compare “Microsoft.PowerShell.Commands.TextMeasureInfo” because it is not IComparable.”
on the Measure-Object cmdlet
How can I check to see if the length of the Query is less than 30000?
Bonus question: Is this the most efficient way of reading the file?
Thanks!
3 Spice ups
$string="123456"
$string[0..3] -join ""
Try this… It’ll return the first 4 characters only from the $string
1 Spice up
iainsmart
(Iain Smart)
October 3, 2016, 9:14am
3
OK, I have tweaked the code slightly to:
$csv.Query | ForEach-Object -Process {
$count = $_.length
if ($count -gt 30000)
{
$truncQuery = $_[0..30000] -join ""
$csv.Query = $truncQuery
}
}
However, I am now getting an error that the Query property cannot be found on the object when I try to set the value to $truncQuery
post the first 3-4 lines of your csv file ( sanitize as needed ). Need to know if the csv file contains a header line or not…
@iainsmart
iainsmart
(Iain Smart)
October 3, 2016, 9:26am
5
It’s a bit tricky to sanitize the CSV, but it does have a header line:
"Event Date and Time","User","OS Host Names","Source IP","Destination IP","Server Group","Service","Operation","Object","Query","SQL Exception Occurred","Source Application"
The $_ variable in the ForEach_Object loop is picking up the correct value for the Query field
Give me one or 2 fake lines… and 'll see what i can come up with on my machine later
Does the total length of one line has to be 30000chars or less or just the query entry?
iainsmart
(Iain Smart)
October 3, 2016, 10:02am
7
It’s just the Query entry (Excel has a limit of 32,767 characters per cell, but even using smaller numbers has the same issue!)
Example csv:
"Event Date and Time","User","OS Host Names","Source IP","Destination IP","Server Group","Service","Operation","Object","Query","SQL Exception Occurred","Source Application"
"09/29/2016 2:30:13 AM","User1","","aaa.bbb.ccc.ddd","aaa.bbb.ccc.ddd","Server Group 1","Service 1","Update","sysjobactivity","DECLARE @nextScheduledRunDate DATETIME SET @nextScheduledRunDate = msdb.dbo.agent_datetime(@P1, @P2) UPDATE msdb.dbo.sysjobactivity SET next_scheduled_run_date = @nextScheduledRunDate WHERE session_id = @P3 AND job_id = @P4","No Exception Occurred",""
"09/29/2016 8:53:22 AM","User2","","aaa.bbb.ccc.ddd","aaa.bbb.ccc.ddd","Server Group 2","Service 2","set deadlock_priority","","SET ROWCOUNT 0 SET TEXTSIZE 2147483647 SET NOCOUNT OFF SET CONCAT_NULL_YIELDS_NULL ON SET ARITHABORT ON SET LOCK_TIMEOUT -1 SET QUERY_GOVERNOR_COST_LIMIT 0 SET DEADLOCK_PRIORITY NORMAL SET TRANSACTION ISOLATION LEVEL READ COMMITTED SET ANSI_NULLS ON SET ANSI_NULL_DFLT_ON ON SET ANSI_PADDING ON SET ANSI_WARNINGS ON SET CURSOR_CLOSE_ON_COMMIT OFF SET IMPLICIT_TRANSACTIONS OFF SET QUOTED_IDENTIFIER ON","No Exception Occurred",""
"09/29/2016 1:37:09 PM","User3","","aaa.bbb.ccc.ddd","aaa.bbb.ccc.ddd","Server Group 1","Service 1","","","SELECT SYSTEM_USER","No Exception Occurred",""
This CSV has3 rows with Query field lengths of 223, 401 and 8. Trying to truncate row 2 to 300 characters has the same issue as 30000 chars in my CSV.
Is it allways the same field that goes too long?? i.e. The query field??
If so then try this quick throw together …
Don’t chuckle too much about the sample csv i used… Wasn’t in a creative mood
Sample csv
"Event Date and Time","User","OS Host Names","Source IP","Destination IP","Server Group","Service","Operation","Object","Query","SQL Exception Occurred","Source Application"
"03 October 2016 14:02:05","Joe6pack","smallville","127.0.0.1","127.0.0.2","snowwhite","caffeinate","a","b","1234567890123456789012345678901234567890","no clue","pokemon go"
Code
$inputfile="c:\data\test.csv"
$csv=import-csv $inputfile
foreach ($line in $csv)
{
$line.query=$line.query[0..9] -join ""
}
$csv | export-csv -Path $inputfile -NoTypeInformation
Result is here ( the query field’s truncated now )
"Event Date and Time","User","OS Host Names","Source IP","Destination IP","Server Group","Service","Operation","Object","Query","SQL Exception Occurred","Source Application"
"03 October 2016 14:02:05","Joe6pack","smallville","127.0.0.1","127.0.0.2","snowwhite","caffeinate","a","b","1234567890","no clue","pokemon go"
If that’s it then good luck. If not do tell me…
@iainsmart
1 Spice up
$csv = @"
one,two,three
1,2,3
100,200,300
1000,2000,3000
1,2000,3
"@ -split "`r`n"
$csv = ConvertFrom-Csv $csv
$properties = $csv |
Get-Member -MemberType NoteProperty |
Select-Object -ExpandProperty Name
$new_csv_data = ForEach($line in $csv) {
$props = @{}
ForEach($property in $properties) {
If($line."$property".Length -gt 3) {
$props[$property] = $line."$property".Substring(0,3)
} Else {
$props[$property] = $line."$property"
}
}
New-Object -TypeName PSObject -Property $props
}
$new_csv_data |
Select-Object -Property $properties
There is an example that trims every cell if it is longer than 3.
1 Spice up
cduff:
$csv = @"
one,two,three
1,2,3
100,200,300
1000,2000,3000
1,2000,3
"@ -split "`r`n"
$csv = ConvertFrom-Csv $csv
$properties = $csv |
Get-Member -MemberType NoteProperty |
Select-Object -ExpandProperty Name
$new_csv_data = ForEach($line in $csv) {
$props = @{}
ForEach($property in $properties) {
If($line."$property".Length -gt 3) {
$props[$property] = $line."$property".Substring(0,3)
} Else {
$props[$property] = $line."$property"
}
}
New-Object -TypeName PSObject -Property $props
}
$new_csv_data |
Select-Object -Property $properties
There is an example that trims every cell if it is longer than 3.
Nice … Am going to steal that code.
iainsmart
(Iain Smart)
October 3, 2016, 11:45am
11
For whatever reason, putting the field name in lower case works!