?
Solved

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

Posted on 2015-01-19
5
Medium Priority
?
111 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 85

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 85

Accepted Solution

by:
oBdA earned 2000 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

Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  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

The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
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…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

771 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