Solved

Remove lines from csv

Posted on 2015-01-11
6
77 Views
Last Modified: 2015-01-13
I have a 5 GB csv file.
I need to remove all lines with a predefined phrase.
How do I do that?;)
Thanks!
0
Comment
Question by:PleaseAnswer
  • 3
  • 2
6 Comments
 
LVL 69

Accepted Solution

by:
Qlemo earned 500 total points
ID: 40543235
You can't remove lines from a file (as you can append data). The file needs to get copied to apply changes.
If you do not need to know anything about the particular data, and just can try to match strings:
get-content C:\SourceFile.csv |
  ? { $_ -notlike '*Phrase to detect*' } |
  set-content C:\DestinationFile.csv

Open in new window

0
 

Author Comment

by:PleaseAnswer
ID: 40543276
I'll give it a go, thanks!
0
 
LVL 29

Expert Comment

by:becraig
ID: 40543301
Another approach would be
(gc orig-file.csv) -notmatch "string you expect to delete" | out-file clean-file.csv

Open in new window

0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
LVL 69

Expert Comment

by:Qlemo
ID: 40543356
-match is dangerous because a phrase might contain characters like . () and more which have special meaning.
0
 
LVL 29

Expert Comment

by:becraig
ID: 40543379
-match looks for a match inside a string and supports regexes
-like looks for a match inside a string and supports wildcards

I think the reverse might be true, where a like statement due to wildcards could yield multiple false positives while match looks for a pure pattern match.

I will however yield to your wisdom on this.
0
 
LVL 69

Expert Comment

by:Qlemo
ID: 40543391
I don't think it makes a difference in the end, but without me knowing that phrase, who can tell?
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This script checks a path to see if a folder exists. If the folder does exist you will get output "The folder has previously been created. No action taken" If not it will create the folder. Then adds one user modify permission to the folder. It …
Set OWA language and time zone in Exchange for individuals, all users or per database.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question