Jump to content
EduGeek EdSec 2026 is Go! 27th Oct in Derby! Join us for a day of EdTech security focused talks, networking, and an evening social ×

Powershell/VB/Batch File - Delete Rows From .csv if %thing% in Column Plus Bits


Recommended Posts

Posted

Hello everyone who's good with scripts and happy 2021.

 

Trying to do a thing. Not getting very far.

 

I have a .csv with three columns in it. Name, Code and Class Name.

 

What I'd like to do is have something that:

 

1 - If the third/class column is blank delete the whole row.

2 - If the class column starts with a NUMBER, delete the whole row.

3 - In one column only (third one) replace "/" and ":" and " "(space) characters with underscores.

 

Is such fiddling possible? You have no idea how happy it'll make me if it is.

 

Thanks any and everyone.

Posted

This is not exactly elegant but I'm a few glasses in... and I dislike compact code when to help others.. I create a new file in line 2..

 

$data=import-csv  "book1.csv"
$output = "book1new.csv"
foreach ($line in $data)
{

   if ($line.'Class Name' -eq "")
   {
       write-host -foregroundcolor Magenta "deleting row as blank class on row name $($line.name)"
   }
   elseif ($line.'Class Name' -match "^[0-9]" )
   {
       write-host -foregroundcolor Magenta "deleting row as Class starts with number: $($line.'class name')"
   }
   else
   {
       #If we are here the class column aint empty nor starts with a number"
       $newclassname = $line.'Class Name' -replace ('/','_')
       $newclassname = $newclassname -replace (':','_')
       $newclassname = $newclassname -replace (' ','_')
       $line.'Class Name' = $newclassname
       write-host -ForegroundColor Green "Writing out $line"
       $line | export-csv $output -Append -NoTypeInformation
   }
   
}

  • Thanks 1
Posted

Hahaha! I absolutely love the idea that somewhere out there in the world someone put down a pint to solve a problem that's been killing me for an afternoon!

 

Thanks so much over there. I'll give it a whirl in a mo. You're a gent also. :)

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...