1

I need to write a batch script to copy those lines from a pipe-delimited file that have one of two given ID numbers.

For example, for this file:

Jack | 12 | Jacksonville
Jane | 34 | Minneapolis

The ID numbers that have to be hardcoded are 12 and 56. So a new text file has to be generated will contain this:

Jack | 12 | Jacksonville

Suggestions?

2 Answers 2

2

I'm not sure what you mean by "copy". I'm assuming you want to create a new file containing just the matching lines. There is a very simple solution using FINDSTR with regular expressions. Each regex looks for the ID in the 2nd column, and allows any number of spaces (including 0) before and after the ID.

findstr /rb /c:"[^|]*| *12 *|" /c:"[^|]*| *56 *|" "file.txt" >"newFile.txt"

Update: 2015-12-20

FINDSTR has very primitive and totally non-standard regex support. Lack of alternation forces each number alternative to require the entire regex. This can be a problem if you are searching for many more alternatives.

There is a much simpler solution using my JREPL.BAT regex text processing tool. It is pure script (hyrid JScript/batch) that runs natively on any Windows machine from XP onward.

call jrepl "^[^|]*\| *(12|56) *\|.*" $0 /jmatch /f "test.txt" /o "newFile.txt"

It is a simple matter to add additional pipe delimited numbers to search for within the parentheses.

Sign up to request clarification or add additional context in comments.

Comments

1

All ID's matching either 12 or 56 in the file IDFile.txt will be outputted into a new file MatchingIDs.txt.

@echo off
setlocal enabledelayedexpansion
set line=0
for /f "tokens=2 delims=|" %%a in (IDFile.txt) do (
set num=%%a
set num=!num: =!
if !num!==12 set bool=true
if !num!==56 set bool=true
if "!bool!"=="true" call :GETLINE
set /a line+=1
set bool=false
)

:GETLINE
if not %line%==0 set skip=skip=%line%
for /f "%skip% tokens=* delims=" %%b in (IDFile.txt) do (
echo %%b >>MatchingIDs.txt
goto :BREAK
)
:BREAK

4 Comments

Didn't work.. Are you sure set num=!num: =! and set skip=skip=%line% are syntactically correct? (Apologies, very new to batch scripting)
@Shalapolia It worked fine for me, try adding a pause above the :GETLINE and see if it gives you any errors. Yes :) the first removes the spaces from !num! as there are spaces separating the pipes, and the second sets the skip variable for the for loop if the value to skip is more than 0, if it's not then it won't be defined so it won't be run, because skip=0 is not syntactically correct.
Ah, its kinda working now. Thanks Bali! Just one more question: It works when I hardcode 2-digit ID numbers but not when I use 7-digit ones. Where is that declared exactly?
Ah, my bad. Just had to change the number of tokens. Many thanks Bali! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.