Computer Hope

Microsoft => Microsoft DOS => Topic started by: elwayisgod on May 27, 2008, 11:10:52 AM

Title: Windows Batch/DOS - How to remove all lines from text file that end with '_' und
Post by: elwayisgod on May 27, 2008, 11:10:52 AM
I have a file 'users.txt'.  It's just a listing of usernames:

samt
robw
phil_
bill_

How can I remove all the lines that end with '_' and resave it as users.txt again using Windows Batch/DOS?

sam
Title: Re: Windows Batch/DOS - How to remove all lines from text file that end with '_' und
Post by: DaveLembke on May 27, 2008, 11:56:56 AM
I'd just use notepad and use the Replace ALL "_" with " " if you are talking about just a single text file that needs to be conditioned, and you are running Windows on this system.

Then save it...
Title: Re: Windows Batch/DOS - How to remove all lines from text file that end with '_'
Post by: elwayisgod on May 27, 2008, 12:11:13 PM
Ended up with this and it seems to work ok:

findstr /v _ users.txt > users2.txt

move /y users2.txt users.txt
Title: Re: Windows Batch/DOS - How to remove all lines from text file that end with '_'
Post by: ghostdog74 on May 27, 2008, 08:25:52 PM
you might want to match an exact _ at the end of the line in case you do really have _ all over the line.
Title: Re: Windows Batch/DOS - How to remove all lines from text file that end with '_' und
Post by: .bat_man on May 28, 2008, 04:12:51 AM
try this code
assuming the file that contail the names is q.txt

@echo off
setlocal ENABLEDELAYEDEXPANSION
for /f "" %%i in (q.txt) do (
set r=%%i
if "!r:~-1!" NEQ "_" echo %%i >>a   )
move a q.txt
endlocal