Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Delete line from text file using batch command

Posted on 2009-07-02
6
Medium Priority
?
1,301 Views
Last Modified: 2012-08-14
Hi,

I have a batch file that creates a text doc from an AD query. Basically it produces a text file with a list of all hosts from particular OU's on the domain. The idea is that I can then use this list to run various scripts and commands against.

I wish to add a line in my batch file AFTER the creation of this doc that will allow me to remove given lines with me providing hostnames in the string. This is to exclude certain systems from whatever commands I run post this.

So, basically, does anyone know a DOS string that will search a text file for a text string and then delete any lines with that contain that string and save the modded text file?

Hope this makes sense!

Thanks,
0
Comment
Question by:bheroniphr
[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
6 Comments
 
LVL 31

Accepted Solution

by:
Zoppo earned 500 total points
ID: 24762282
Hi,

you can use 'type' and 'find', i.e.:

> type in.txt | find /V "<search sting>" > out.txt

This will write all lines from 'in.txt' to 'out.txt' which don't contain the '<search string>'. To remove multiple strings just copy 'out.txt' to 'in.txt' and redo the same with another '<search string>'

Hope that helps,

ZOPPO
0
 
LVL 12

Expert Comment

by:Serge Fournier
ID: 24762430
try using WSH (windows host script) instead of .BAT
it have all the function to read a files and rewrite it without the unwanted line

lots of examples on internet as how to open file, read it, write it
i put a very crude example in my code snippet

crude cause it use two different file name and n ddo nott rename the files after to have the original file name


objfil04 = read
objfil05 = write

      Set objfil04 = objFSO.OpenTextFile("c:\aef\" & fil02 & "_aef.csv", 1, 0) 
      Set objfil05 = objfso.OpenTextFile("c:\aef\" & fil02 & ".csv", 2, true)
      a = objFil04.readLine
if instr(a,"i dont want this line") = 0 then      
   objFil05.Write a
end if
 
      objfil05.close
      objfil04.close

Open in new window

0
 
LVL 12

Expert Comment

by:Serge Fournier
ID: 24762446
i also forgot a:

do while objfil04.AtEndOfStream <> True
loop

and a objfso definition object

Set objfso = CreateObject("Scripting.fileSystemObject")      
Set objfil04 = objFSO.OpenTextFile("c:\aef\" & fil02 & "_aef.csv", 1, 0) 
      Set objfil05 = objfso.OpenTextFile("c:\aef\" & fil02 & ".csv", 2, true)
do while objfil04.AtEndOfStream <> True      
   a = objFil04.readLine
   if instr(a,"i dont want this line") = 0 then      
      objFil05.Write a
   end if
loop
 
      objfil05.close
      objfil04.close

Open in new window

0
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 
LVL 43

Expert Comment

by:Steve Knight
ID: 24763354
If you use findstr instead of find you can do it with one line:

findstr /v "pc1 pc2 pc3" yourlist.txt > newlist.txt

Steve
0
 

Author Closing Comment

by:bheroniphr
ID: 31599135
Exactly what I was looking for, short and sweet.

Thanks to the others but I did need something that could be embedded in my existing Batch and this solution was also much shorter and cleaner than the others suggested.

Thanks to all.

B
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 24765312
You didn't like the easy option ofusing one command then?

Steve
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
This article was inspired by a question here at Experts Exchange (http://www.experts-exchange.com/Software/Photos_Graphics/Images_and_Photos/Q_28629170.html). The requirements stated in that question are (1) reduce the file size of a large number of…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

705 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