Solved

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

Posted on 2015-01-19
5
97 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 83

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 83

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Utilizing an array to gracefully append to a list of EmailAddresses
"Migrate" an SMTP relay receive connector to a new server using info from an old server.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
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…

803 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