Solved

Look for a word in a CSV and remove everything under it using powershell

Posted on 2015-01-19
5
106 Views
Last Modified: 2015-01-19
I have a line in a csv called examples.
I want to remove that line and every line under it using powershell.  

What is the best way to accomplish this?

Thanks
0
Comment
Question by:SLPowers
  • 3
  • 2
5 Comments
 
LVL 84

Expert Comment

by:oBdA
ID: 40557528
Your question is not quite clear. Do you have a line called examples in a csv file, or do you have some line in a csv file called examples?
I'm leaning towards the former, so try this:
$InputFile = "C:\Temp\test.csv"
$OutputFile = "C:\Temp\test-NoExamples.csv"
$StopLine = "Examples"
Get-Content $InputFile | % {If ($_ -ne $StopLine) {$_} Else {Break}} | Set-Content $OutputFile

Open in new window

If the latter: is "the line" a regular csv line, and how do you want to identify the line in question; as the full text line, as part of a text line, as properties of the csv columns, ...?
0
 

Author Comment

by:SLPowers
ID: 40557539
Thanks so much for the fast reply.  I am sorry for my poorly worded question.


I have a large CSV file.   Somewhere down the file will be a Row and in column A row (something)  the word "Examples" will be there.

I want to delete the word examples and every row below that.

Thanks again
0
 

Author Comment

by:SLPowers
ID: 40557545
Snap24.png
0
 
LVL 84

Accepted Solution

by:
oBdA earned 500 total points
ID: 40557567
If the following doesn't work for you, please provide a csv file (in a 'Code' box or as attachment) you'll be working with (you can of course anonymize the contents).
$InputFile = "C:\Temp\test.csv"
$OutputFile = "C:\Temp\test-NoExamples.csv"
$StopLine = "Examples:"
Get-Content $InputFile | % {If ($_.Trim('"').ToUpper().StartsWith($StopLine.ToUpper())) {Break} Else {$_}} | Set-Content $OutputFile

Open in new window

0
 

Author Closing Comment

by:SLPowers
ID: 40557594
Worked perfectly!


Thanks
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Synchronize a new Active Directory domain with an existing Office 365 tenant
A brief introduction to what I consider to be the best editor for PowerShell.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

749 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