Solved

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

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

The article will show you how you can maintain a simple logfile of all Startup and Shutdown events on Windows servers and desktops with PowerShell. The script can be easily adapted into doing more like gracefully silencing/updating your monitoring s…
A brief introduction to what I consider to be the best editor for PowerShell.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

910 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

22 Experts available now in Live!

Get 1:1 Help Now