Solved

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

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

757 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now