China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-08-06 04:02
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » How to check at once whether any of multiple strings exists in a file View 776 Replies 9
Original Poster Posted 2007-10-16 22:15 ·  中国 浙江 温州 电信
中级用户
★★
Credits 458
Posts 196
Joined 2006-10-05 12:04
19-year member
UID 64614
Status Offline
There are N strings used as signatures

A file may contain these N strings. How can I determine whether the file contains "any one" of these strings?

Running "find string file" one by one is too slow. If there are too many signatures, the speed is very slow

sed -e '/AAA/b' -e '/BBB/b' -e '/CCC/b' -e d test.txt&& echo %time%
This command can search multiple strings at once, but from the returned result I can't tell whether it contains the strings
Even if the file does not contain these strings it still shows %time% the same way, so how should this be written?

Are there any other tools that can do this? :)
Floor 2 Posted 2007-10-16 23:26 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
20-year member
UID 59080
Status Offline
Put the strings to search below
sed.ini:

AAA
BBB
CCC

findstr /g:sed.ini YourFileToSearch
Floor 3 Posted 2007-10-16 23:55 ·  中国 湖南 娄底 电信
银牌会员
★★★
Credits 2,268
Posts 879
Joined 2006-12-19 16:23
19-year member
UID 73968
Gender Male
Status Offline

@echo off&setlocal EnableDelayedExpansion
set xs=0
set "var=sed -e "{s/aaa//;s/bbb//;s/ccc//g}" a.txt"
for /f "tokens=1* delims=:" %%a in ('findstr /n .* a.txt') do set num%%a=%%b
for /f "delims=" %%a in ('%var%') do (
set /a str+=1
call set uew=%%num!str!%%
if "!uew!"=="%%a" set /a xs=1
)
echo There are !xs! lines containing the specified strings.
pause
致精致简!
Floor 4 Posted 2007-10-17 00:27 ·  中国 浙江 温州 电信
中级用户
★★
Credits 458
Posts 196
Joined 2006-10-05 12:04
19-year member
UID 64614
Status Offline
Originally posted by lxmxn at 2007-10-16 23:26:
Put the strings to search below
sed.ini:

findstr /g:sed.ini YourFileToSearch


Only one line in sed.ini works

Multiple lines don't work??
Floor 5 Posted 2007-10-17 00:33 ·  中国 浙江 温州 电信
中级用户
★★
Credits 458
Posts 196
Joined 2006-10-05 12:04
19-year member
UID 64614
Status Offline
Originally posted by 26933062 at 2007-10-16 23:55:

@echo off&setlocal EnableDelayedExpansion
set xs=0
set "var=sed -e "{s/aaa//;s/bbb//;s/ccc//g}" a.txt"
for /f "tokens=1* delims=:" %%a in ('findstr /n . ...



It can only count the first signature; if the file contains bbb/ccc it can't count them:o
Floor 6 Posted 2007-10-17 01:16 ·  中国 湖南 娄底 电信
银牌会员
★★★
Credits 2,268
Posts 879
Joined 2006-12-19 16:23
19-year member
UID 73968
Gender Male
Status Offline
Looks like I can only run sed 3 times. Not sure about the efficiency
Each time replacing one string.
致精致简!
Floor 7 Posted 2007-10-17 01:35 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
20-year member
UID 59080
Status Offline
Originally posted by zerocq at 2007-10-17 00:27:


Only one line in sed.ini works

Multiple lines don't work??

I tested it, and multiple lines do work.
G:\>type sed.ini
perl
sed
first

G:\>type test.txt
this is a perl test
something to use sed
www.cn-dos.net/forum
my first important filter is the Great GNU sed
javascript

G:\>findstr /g:sed.ini test.txt
this is a perl test
something to use sed
my first important filter is the Great GNU sed

G:\>


[ Last edited by lxmxn on 2007-10-17 at 01:48 AM ]
Floor 8 Posted 2007-10-19 00:08 ·  中国 浙江 温州 电信
中级用户
★★
Credits 458
Posts 196
Joined 2006-10-05 12:04
19-year member
UID 64614
Status Offline
What operating system is the moderator using??
Floor 9 Posted 2007-10-19 01:25 ·  中国 浙江 温州 电信
中级用户
★★
Credits 458
Posts 196
Joined 2006-10-05 12:04
19-year member
UID 64614
Status Offline
The problem has been solved, this thread can be closed

egrep.exe can support searching multiple strings at the same time, using | to separate the strings

And it returns %errorlevel% correctly
Floor 10 Posted 2007-10-19 17:14 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
20-year member
UID 59080
Status Offline
My OS is Windows XP.
Forum Jump: