JRA Posted January 1, 2021 Posted January 1, 2021 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.
chaplic Posted January 2, 2021 Posted January 2, 2021 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 } } 1
JRA Posted January 2, 2021 Author Posted January 2, 2021 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.
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now