Solved

Delete line from text file using batch command

Posted on 2009-07-02
6
1,278 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
How our DevOps Teams Maximize Uptime

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

 
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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

Suggested Solutions

Using dates in 'DOS' batch files has always been tricky as it has no built in ways of extracting date information.  There are many tricks using string manipulation to pull out parts of the %date% variable or output of the date /t command but these r…
Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

840 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