Check every string in textfile for substring - string

I've searched a long time and didn't find something useful for my problem. It may sound simple, I would be very happy if somebody could help me:
I want to write a batch script, which proves every string in a textfile whether it contains a specific substring. If this is the case, the whole string, which contains this substring, should be printed out.
The strings, I'm looking for, are surrounded by double quotes.
My code just works for all lines of my textfile, but I need it for all strings.
Thx in advance!
#echo off
setlocal enableextensions enabledelayedexpansion
for /f "delims=" %%A in ('findstr "somesubstring" "textfile.txt"') do (
echo %%A
)

Perhaps is this what you want?
#echo off
setlocal enabledelayedexpansion
set "substring=somesubstring"
for /f "delims=" %%A in ('findstr "%substring%" "textfile.txt"') do (
for %%B in (%%A) do (
set "string=%%~B"
if "!string:%substring%=!" neq "!string!" echo %%B
)
)
This Bath file may fail if the characters outside the "strings" (not enclosed in quotes) are special Batch characters.

I would first use a tool to isolate each string on a single line. Then you can use FINDSTR to return only quoted lines that contain the substring
The trickiest part is isolating the quoted strings. My REPL.BAT regex search and replace utility is a good option. It is a hybrid JScript/batch script that will run natively on any modern Windows machine from XP onward.
type "textfile.txt" | repl (\q.*?\q) \r\n$1\r\n x | findstr /x ^"\".*substring.*\"^"
If you want to see your strings without enclosing quotes, then:
for /f delims^=^ eol^= %%A in (
'type "textfile.txt" ^| repl (\q.*?\q) \r\n$1\r\n x ^| findstr /x ^"\".*substring.*\"^"'
) do echo %%~A

Related

Batch file extract string

I know this has been asked numerous times, but I've been researching for like 2 hours and still can't do that.
I need a batch script to extract a string from a file.
The content of the file is this:
C:\Windows\system32\tasks{7D7A0547-0D79-0805-0A11-0B780D08110D}
I want to extract this part:
{7D7A0547-0D79-0805-0A11-0B780D08110D}
I tried it with for /f command and all kinds of options and searches, but I just can't do it.
TIA
for /f "tokens=2 delims={}" %%# in ("C:\Windows\system32\tasks{7D7A0547-0D79-0805-0A11-0B780D08110D}") do echo {%%#}
?
#echo off
for /f "tokens=2 delims={}" %%A in (
'findstr "{[0-9A-F-]*}" "X:\path\yourfile.ext" '
) Do Echo %%A

Findstr /g with token or delim

Suppose we have 2 files
First.txt
123
456
And Second.txt
789;123
123;def
482;xaq
What i need is to find the lines in the second file only containing entries of the first file in first column (token 1, delim ; ).
This is what i need:
Output.txt
123;def
Of course,
findstr /g:first.txt second.txt
will output both lines:
789;123
123;def
Any idea how i can mix findstr and for /f to get the needed output?
Thank you!
If all of the elements in the first column are of the same length, then the simple answer would be
findstr /b /g:first.txt second.txt
Note however that if first.txt contains a line 12 then this would match 123;abc and 129;pqr in the second file.
You can take advantage of the super-limited regex capabilities of findstr and compare each line of first.txt to only the very beginning of each line of second.txt.
#echo off
for /F %%A in (first.txt) do findstr /R /C:"^%%A;" second.txt
The /R flag means that the search string should be treated as a regular expression. The ^ in the search string means that %%A comes at the very beginning of the line. The ; is a literal semicolon that will prevent the 123 line from picking up 1234;abcd in second.txt.
Without executing a separate findstr for each value and to avoid the problem with partial matches at the start of the line, you can try with
#echo off
setlocal enableextensions disabledelayedexpansion
( cmd /q /c"(for /f "delims=" %%a in (first.txt) do echo(%%a;)"
) | findstr /g:/ /l /b second.txt
What it does is read first.txt and echo each line with the delimiter. This output is retrieved by the findstr using /g:/ to use the standard input as the source for the elements to match, that will be considered as literals (/l) at the start of the line (/b) in the second.txt file
Is the general form for CSV. Note in batch %A becomes %%A.
for /f "delims=," %A in (csv.txt) do findstr /c:"%A" file2.txt
Here's the output
C:\Users\User>for /f "delims=," %A in (csv.txt) do findstr /c:"%A" csv1.txt
C:\Users\User>findstr /c:"55" csv1.txt
55,61,hi there, Good
C:\Users\User>findstr /c:"60" csv1.txt
54,60,hi there, Bad
C:\Users\User>findstr /c:"Bad" csv1.txt
54,63,hi there, Bad
54,60,hi there, Bad
C:\Users\User>findstr /c:"55" csv1.txt
55,61,hi there, Good
Contents of two files.
55,60
60,60
Bad,60
55,60
and
55,61,hi there, Good
54,62,hi there, Good
54,63,hi there, Bad
54,60,hi there, Bad

Batch search multiple strings simultaneously

I have this large database of equipment:
Equipment500
Equipment501
..........
Equipment998
Equipment999
As well as an even larger database with details about equipment:
Equipment1:details....
Equipment2:details....
..................
Equipment9998:details....
Equipment9999:details....
What i need, is to select only the details for equipment i need:
for /f "tokens=* delims= " %%a in (%cd%\equipment.db) do (
findstr /i /c:"%%a" details.db > Output\%%a
)
The output will be, of course, a folder with files:
In Equipment500 it will be Equipment500:details....
In Equipment501 it will be Equipment501:details....
..................
In Equipment998 it will be Equipment998:details....
In Equipment999 it will be Equipment999:details....
The problem is that it takes a lot of time.
I need this multithreaded so that it runs more instances of findstr (preferably all 500) at the sametime to do processing instantly.
Any idea is appreciated. Thank you!
#echo off
echo building input files (this needs some time):
del *.db
for /l %%i in (500,1,999) do #echo Equipment%%i>>equipment.db
for /l %%i in (1,1,9999) do #echo Equipment%%i:Detailswhatever>>details.db
echo %time% start adapting
REM adapt equipment.db:
(for /f "delims=" %%i in (equipment.db) do echo %%i:)>equip.db
REM find all strings:
echo %time% start searching
findstr /g:equip.db details.db >output.txt
echo %time% done
NOTE: "Equipment.db" has to be adapted, because searching for "Equipment2" would also find "Equipment20", Equipment21"... "Equipment200" ...
Since you only provide vague information about your file structure, I'd suggest
#echo off
for /f "tokens=1*delims=:" %%a in (details.db) do >>%%a.dat echo %%b
which assumes each entry in details.db is of the form
equipment1234:details

How do I set a variable to equal every line in a txt file with for/f?

I have a ChatRoom batch file and it creates a text file were all the user inputs go into like this:
Echo %User Input% >> C:\ChatterBox\Chat.txt
I have this so far:
For /F "Delims=" %%A In (C:\ChatterBox\Chat.txt) Do (
Set Chat=%%A
)
Echo %Chat%
That didn't work and it only gave me the first line of the text file, do you know how I can echo every line in this way?
Echo %Chat%
And have the output look like this?
"Line one"
"Line two"
"Line three"
"Line four"
You're echoing the line after the loop ends, so you only get the last line printed (not the first). In order to use the lines within the loop, you must enable delayed variable expansion -- otherwise the variables get expanded when the loop is started rather than every iteration. Once you've enabled it, you use !var! instead of %var% to get the delayed expansion.
Here's how to do it:
setlocal enabledelayedexpansion
For /F "Delims=" %%A In (Chat.txt) Do (
Set Chat=%%A
Echo !Chat!
)
Use this command:
for /f "delims=" %%i in (chat.txt) do echo %%i
this will show you all lines in the chat.txt file
hope it helps!
You could also put all lines in a single variable that can be used outside of the loop:
set Chat=
set NL=^
::The above two blank lines are needed
setlocal enabledelayedexpansion
for /f "delims=" %%a in (chat.txt) do (
set Chat=!Chat!!NL!%%a
)
Echo %Chat%
And that should serve you well. It is important to note that instead of going through all the trouble to print every line just use: type chat.txt.
Mona.
This will display the lines:
type "C:\ChatterBox\Chat.txt"

Merge non-empty lines of the two unicode text files, using windows batch

We have 2 Unicode files. One of them contains lines that are missing in another file. Like so:
1.
2. bbbbbbbbbbbbbbbbb
3.
4. ddddddddddddddddddddd
5. eeeeeeeeeeeeeeeeeeeeeeee
1. aaaaaaaaaaaaaa
2.
3. ccccccccccccccccc
4.
5.
We want to merge them into third file that will contain all lines:
1. aaaaaaaaaaaaaa
2. bbbbbbbbbbbbbbbbb
3. ccccccccccccccccc
4. ddddddddddddddddddddd
5. eeeeeeeeeeeeeeeeeeeeeeee
Notes:
a,b,c,d,e - can be any text.
line numbers are just for illustration purposes, they are not present in actual files.
I created this question with "batch-file" tag, but I am open to any suggestions about how to achieve this. Of course better not to involve something like C++
Assuming that no line starts with a colon,
first we read each line from two files into two arrays, including blank lines - there is a special hack for this, since normal for skips blank lines.
And then just concatenate elements with from both arrays with identical index, and output into results.txt :
setlocal EnableDelayedExpansion
set i=0
for /f "tokens=1* delims=:" %%A in ('type "file1.txt" ^| findstr /n "^"') do (
set /A i+=1
set arr1[!i!]=%%B
)
set i=0
for /f "tokens=1* delims=:" %%A in ('type "file2.txt" ^| findstr /n "^"') do (
set /A i+=1
set arr2[!i!]=%%B
)
for /L %%i in (1,1,%i%) do echo.!arr1[%%i]!!arr2[%%i]!>> result.txt

Resources