Solved

Delete line from text file using batch command

Posted on 2009-07-02
6
1,273 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
6 Comments
 
LVL 31

Accepted Solution

by:
Zoppo earned 125 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

If like me you are one who spends a lot of time working and scripting with cmd.exe, sometimes it is handy to be able to quickly view a calendar for a given month and year. This script will quickly do just that!  Save the code posted below to a .bat …
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
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).

920 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

15 Experts available now in Live!

Get 1:1 Help Now