Regular expression in batch file is not working - regex

we used Regex in batch by using following command,
Dir "C:\Test\Res345_45664_1335" /s /b /a:-d | findstr /R "[(\d+)_(\d+)_(\d+)]" > filelist.txt
The "C:\Test\Res345_45664_1335" directory contains following files,
Res345_45664_1335.txt
Output.txt
list.txt
We need file that’s in the format
But the above dir command with regex displaying all files present in the "C:\Test\Res345_45664_1335" directory. Because "C:\Test\Res345_45664_1335" directory contains the same format "Res345_45664_1335". But We need files only(with full path).
Thanks.

\d, () and + are not a valid meta characters in findstr. See findstr /? fore more advanced help. You should substitute it with [0-9][0-9]* .
Dir "C:\Test\Res345_45664_1335" /s /b /a:-d | findstr /ER "[0-9][0-9]*_[0-9][0-9]*_[0-9][0-9]*.txt" > filelist.txt

Do the files have extensions?
Dir "c:\test\Res345_45664_1335" /s /b /a:-d | findstr /R "[0-9]*_[0-9]*_[0-9]*\."

try
Dir /s /b /a:-d *_*_*
Not really sure what you mean by "the format"
Ah - filename in format string_string_string...
FOR /f "delims=" %%i IN ('dir /s /b /a-d *_*_*') DO ECHO "%%~ni"|FINDSTR /r "..*_..*_..*" >nul&IF NOT ERRORLEVEL 1 ECHO %%i
(that's as a batch line - reduce %% to % to run from the prompt)

Batch scripting does not have such feasibility as like programming languages. Some simple regular expression can be worked with 'findstr' but it has some limitations like '+' character has replacement as '.*'. But don't worry, I have a solution for you.
you can simply use power-shell inside a batch script and here below your solution:
#powershell -command "if (($string -match $regex) -eq $true) {write-host matched!}"
It will work for you and if you want to throw an exception then simply throw and assert in batch script as:
#powershell -command "if (($string -match $regex) -eq $true) {throw [System.ArgumentException] invalid input}"
I hope this would suffice for you and others who may in need of this.

Related

CMD script incorrect return code for end-of-line regex findstr command

In a CMD script, I am trying to determine if a string has a "/" as the last character or not, using a regular expression with the findstr command. However, in both cases, I am getting a return code of 1.
#echo off
SETLOCAL enabledelayedexpansion
SET Project=/Folder1
ECHO.%Project% | findstr /R /C:"/$" 1>nul
ECHO %errorlevel%
SET Project=/Folder1/
ECHO.%Project% | findstr /R /C:"/$" 1>nul
ECHO %errorlevel%
Is there an issue here with the syntax?

Batch Regex: match exact window title

How can I make batch regex expression (in FINDSTR /r /c:"ng") match this window title only? It matches also MongoDB terminal window.
"ng\>" didn't work.
Full code for context:
for /f "tokens=2 delims=," %%a in ('
TASKLIST /fi "imagename eq cmd.exe" /v /fo:csv /nh ^| FINDSTR /r /c:"ng"
') do taskkill /PID %%a
EDIT:
Additional info from my comment under Squashman's response:
How to avoid returning multiple PIDs from last call? script examples screenshot
List of opened windows: windows screenshot
Using the option to find at the beginning of the word worked in my testing.
TASKLIST /fi "imagename eq cmd.exe" /v /fo:csv /nh | FINDSTR "\<ng"
This also worked in my testing.
TASKLIST /fi "imagename eq cmd.exe" /v /fo:csv /nh | FINDSTR "\"ng\""

Edit Windows .reg Files with sed / grep: search for a FilePath output RegPath Key Value

I am currently trying to edit a full dump .reg File on Linux Mint. The Goal is to find a given path in the values an then to print out the corresponding regpath the key and the full value itself.
I know that I can achieve this using regex patterns in grep or sed unfortunately I am pretty new to the named programs.
Heres one Example: I am searching for C:\\ProgramData
[HKEY_LOCAL_MACHINE\...]
"noPath0"="1.1.9103.0"
"path0Key"="C:\\ProgramData\\..."
"noPath1"="2.1.9103.0"
"path1Key"="...C:\\ProgramData\\..."
[HKEY_LOCAL_MACHINE\...]
"noPath0"=dword:00000000
The output should be the following:
[HKEY_LOCAL_MACHINE\...]
"path0Key"="C:\\ProgramData\\..."
"path1Key"="...C:\\ProgramData\\..."
I've figured out the following two regexPattern:
Regpath: ^\[.[^\]]*\n
Key+Value: .*C\:\\\\ProgramData.*
The problem is how do I combine both patterns and use them in grep or sed or what ever is more suitable for this task ?
A sed script would be a more elegant way, but for a quick-and-dirty solution, I'd write a script that runs csplit on your first regex, then grep with your second regex on each split file. i.e.
if exist xx* del /q xx*
csplit myfile.reg.txt /^\[/ {*}
for %%f in (xx*) do call :search %%f
goto :EOF
:search
grep ".*C\:\\\\ProgramData.*" %1 >nul
if not "%errorlevel%"=="0" goto :EOF
grep "^\[\|.*C\:\\\\ProgramData.*" %1
goto :EOF
Bacause of ty733420's help I was able to create a bash script that does job ... really really slowly ... but at least it works:
#!/bin/bash
#: <<'COMMENT'
if [ -e xx00 ]; then
rm xx*
fi
csplit -s ./regBackup.reg.txt "/^\[/" '{*}'
for name in $(ls xx*)
do
#echo $name
if grep '.*C\:\\\\Program Files.*' $name > /dev/null; then
grep '^\[.*' $name
grep '.*C\:\\\\Program Files.*' $name
echo ""
fi
done
exit 0
:
./sedS.sh > out.txt
Thanks for you help ty733420 :)
If someone knows how to speed this task up I would appreciate it.

sort list by keywords

I have a list of keywords in a keywords.txt file. I have another file list.txt with the keywords in the beginning of each line. How can I sort the lines in list.txt to the same order they appear in keywords.txt?
keywords.txt
house
car
tree
woods
mailbox
list.txt
car bbdfbdfbdfbdf
tree gdfgvsgsgs
mailbox gsgsdfsdf
woods gsgsdgsdgsdgsdgsddsd
house gsdgfsdgsdgsdgsdg
final result in list.txt
house gsdgfsdgsdgsdgsdg
car bbdfbdfbdfbdf
tree gdfgvsgsgs
woods gsgsdgsdgsdgsdgsddsd
mailbox gsgsdfsdf
Here is an improved and simplified version of kiswa's answer.
#echo off
(
for /f "usebackq" %%A in ("keywords.txt") do findstr /bl "%%A" list.txt
)>sorted.txt
REM move /y sorted.txt list.txt
The FINDSTR command only matches lines that begin with the keyword, and it forces the search to be a literal search. (FINDSTR could give the wrong result if the /L option is not specified and the keyword happens to contain a regex meta-character.)
The code to replace the original file with the sorted file is commented out. Simply remove the REM statement to activate the MOVE statement.
As with kiswa's answer, the above will only output lines from list.txt that match a keyword in keywords.txt.
You might have lines in list.txt that do not match a keyword. If you want to preserve those lines at the bottom of the sorted output, then use:
#echo off
(
for /f "usebackq" %%A in ("keywords.txt") do findstr /bli "%%A" "list.txt"
findstr /vblig:"keywords.txt" "list.txt"
)>sorted.txt
::move /y sorted.txt list.txt
Note that the /I (case insensitive) option must be used because of a FINDSTR bug dealing with multiple literal search strings of different lengths. The /I option avoids the bug, but it would cause problems if your keywords are case sensitive. See What are the undocumented features and limitations of the Windows FINDSTR command?.
You might have keywords that are missing from list.txt. If you want to include those keywords without any data following them, then use:
#echo off
(
for /f "usebackq" %%A in ("keywords.txt") do findstr /bl "%%A" "list.txt" || echo %%A
)>sorted.txt
::move /y sorted.txt list.txt
Obviously you can combine both techniques to make sure you preserve the union of both files:
#echo off
(
for /f "usebackq" %%A in ("keywords.txt") do findstr /bli "%%A" "list.txt" || echo %%A
findstr /vblig:"keywords.txt" "list.txt"
)>sorted.txt
::move /y sorted.txt list.txt
All of the above assume the keywords do not contain space or tab characters. If they do, then the FOR /F options and FINDSTR options must change:
#echo off
(
for /f "usebackq delims=" %%A in ("keywords.txt") do findstr /bic:"%%A" "list.txt" || echo %%A
findstr /vblig:"keywords.txt" "list.txt"
)>sorted.txt
::move /y sorted.txt list.txt
$ join -1 2 -2 1 <(cat -n keywords.txt | sort -k2) <(sort list.txt) | sort -k2n | cut -d ' ' -f 1,3-
house gsdgfsdgsdgsdgsdg
car bbdfbdfbdfbdf
tree gdfgvsgsgs
woods gsgsdgsdgsdgsdgsddsd
mailbox gsgsdfsdf
Here's a Windows batch file. It's probably not the most efficient, but I think it's nicely readable.
#echo off
for /F "tokens=*" %%A in (keywords.txt) do (
for /F "tokens=*" %%B in ('findstr /i /C:"%%A" list.txt') do (
echo %%B >> sorted.txt
)
)
del list.txt
rename sorted.txt list.txt
This creates a sorted file, then removes the list file and renames the sorted file.

Batch File: List all files not beginning with "SP"

I have a DOS batch file that performs an action for files beginning with text "SP", as follows:
FOR /F "tokens=*" %%A IN ( 'DIR SP*.sql /s /b' ) DO ECHO .compile_file = "%%A" >> output.txt
The key bit here is obviously:
DIR SP*.sql /s /b
I need to do a similar thing before the "FOR" line above, but for all other files not starting with SP*.sql
Something like:
FOR /F "tokens=*" %%A IN ( 'DIR [^SP]*.sql /s /b' ) DO ECHO .run_file = "%%A" >> output.txt
FOR /F "tokens=*" %%A IN ( 'DIR SP*.sql /s /b' ) DO ECHO .compile_file = "%%A" >> output.txt
Does anyone know how I can do this?
Can I use regex for this sort of thing?
The batch file will run under CMD in Windows XP.
Cheers
In batch, usually you can use findstr with /V to negate regex , eg
.... | findstr /V "^SPL"
try passing the filename to findstr and see how it goes
%~nI - expands %I to a file name only