How to remove specific lines from a text files using either vbs or bat.

1050 pts.
Batch files
Batch jobs
Text manipulation
Windows Server 2003 Standard Edition
We have a text file that is a result of an export from an application we have. We use the following bat file to rename and remove blank lines from it based on the way it exports the export options/format cannot be changed. I would like a way to add to this bat file a method to remove specific lines for the renamed, blank space removed text file. Example of the needs: text file is 30 lines long and we would want to remove line 3,7,18,19 and 28. Not the actual lines or length we would want to remove, just an idea of what we would like accomplish. These lines are informational and cannot be removed from the export option and are not needed for the SQL import. When this runs we would use a bat file in dump this cleaned up file into a SQL table. BAT file begins====================================
@echo off
pushd C:WE_Reportuser
for /D %%j in (*) do move "%%j" "%%~dpjapps"

move C:WE_Reportuserapps* C:WE_Reportapp_csv
:: removes blank lines from file for import into SQL For /F "tokens=* delims=" %%A in (C:WE_Reportapp_csvuser_app.txt) Do Echo %%A >> C:WE_Reportapp_csvtestuser_app.txt ============NEED specific line removal code here========== ==============EOF===========================

Software/Hardware used:
server 2003 standard, SQL 2000,xp pro

Answer Wiki

Thanks. We'll let you know when a new response is added.

There are several ways of managing this item: SED, FINDSTR and even a FOR loop.

My favorite method is using a SED utility. SED is very flexible and SED scripts. A SED script run from your batch file will provide the functionality you require. For simple items, it could be a one line SED “script.”

From your example, removing blank lines is a simple single line SED script.

A second method is using FINDSTR or FIND (windows utilities) and “print” lines that don’t match to a file. You can run multiple FINDSTR in a brute force approach.

A third method is using a FOR loop to process each line of the file and only adding the lines you want to a new file.

Seder’s Super SED


Take a look at this similar question and answer. It was about replacing lines, but the script provided could easily be modified to remove lines.

Feel free to post any questions/doubts/comments you might have.


Discuss This Question: 1  Reply

There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.
  • swinehart6803
    Thank Plb After figuring out the syntax and command line switches it was easy using ssed. For anyone who comes across a need like this again the code I used is below ======================bat file line========================= ssed -e 1,8d -e 10,21d -e $d report.csv > C:cleaned_report.csv ======================End of code=========================== The key was to be able to stack the command and delete multiple lines is to use the "-e" switch. And this version does not like to have spaces after the line number or range of numbers, unlike to web examples. Plus I had trouble running getting it edit files that did not exist in the same folder as the .exe, so I just dumped the files I wanted to edit into the same folder as ssed.exe and ran the it there.
    1,050 pointsBadges:

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

Thanks! We'll email you when relevant content is added and updated.


Share this item with your network: