How to show a string that appears after specific string in a file with a batch? - string

I need to create a batch file that would show me a string printed after a specific string in some log file.
For example: I have a log file with a line that ends with the string "Calculated number: XX". I want to create a batch file that would go to that log, find this string and print only XX part to the screen (XX is some number that changes every now and then). Any ideas what is the best way to do that?
Help will be much appreciated, thanks in advance!

The format of the string is what comes before and after the part you are interested in - as it can matter how the line is parsed.
This code is robust and will print the number at the end of the string, if it is indeed at the end.
It uses a helper batch file called repl.bat - download from: https://www.dropbox.com/s/qidqwztmetbvklt/repl.bat
Place repl.bat in the same folder as the batch file or in a folder that is on the path.
#echo off
type "file.log" | repl ".*Calculated number: (.*)" "$1" a

#ECHO OFF
SETLOCAL
FOR /f "delims=" %%a IN (q23040271.txt) DO (
ECHO "%%a"|FIND "Calculated number: " >NUL 2>nul
IF NOT ERRORLEVEL 1 SET "line=%%a"&GOTO found
)
ECHO target NOT found&GOTO :eof
:found
ECHO %line:~-2%
GOTO :EOF
I used a file named q23040271.txt containing junk text and sample data for my testing.
This relies heavily on the assumption that the very first occurrence of Calculated number:
will be the required ite, and that it will be at the end of the line - no checking is done that it is actually at the end of the line.
replacing
ECHO "%%a"|FIND "Calculated number: " >NUL 2>nul
with
ECHO "%%a"|FINDSTR /e /R /c:"Calculated number: ..." >NUL 2>nul
would perform that end-of-line check (in theory - I haven't checked it) - note the three consecutive dots.
This will be agonisngly slow if there are millions of lines - unless the target string is very early in the file.
The more information you provide, the better a solution that can be devised.

Related

Batch - Find string in text file and delete full line

I use the script from this Thread accepted answer from Mofi. The script copy folders and store them in text file to exclude once copied folders on next run.
Sometimes I have folders called [incomplete]-different.names and I do not want to copy this folders. I want that all folders with the string [incomplete]- and the name behind are skipped or are not even written in the text file %CurrentList% for further processing.
These are my previous attempts but so far I could not get up and running with the script from the top.
Help would be nice, and thanks in advance.
Try 1:
for /f "delims=" [incomplete]-%%D in ("%CurrentList%") do (
set str=%%D
set str=!str: =!
set str=!str: %%D =!
echo !str!
Try 2:
findstr /v /b /c:"[incomplete]-"%%D" "%CurrentList%" del "%%D"
You were really close with your second attempt.
If all you want is to delete lines in %CurrentList% that contain the string [incomplete]-, you can just direct the output of a findstr /v to a temp file and then overwrite CurrentList with that file.
findstr /v /c:"[incomplete]-" "%CurrentList%" >tmpList.txt
move /y tmpList.txt "%CurrentList%" >nul

Find a string and replace specific letters in batch file

I have a to create an autosys job to trigger a batch file which would find a specific string and then replace the next 4 characters.
For example if file (with multiple lines) has below content and i am searching for played
the mad fox jumped of the old vine and
played soccer.
I should replace "socc" with "INFA"
I am new to batch files and my lead has been insisting that i do this using a batch file only. Any help would be greatly apprciated.
Thanks,
Joy
#echo off &setlocal
set "search=search string"
set "replace=kordo anstataui"
set "textfile=file.txt"
set "newfile=new.txt"
(for /f "delims=" %%i in ('findstr /n "^" "%textfile%"') do (
set "line=%%i"
setlocal enabledelayedexpansion
set "line=!line:%search%=%replace%!"
echo(!line!
endlocal
))>"%newfile%"
type "%newfile%"
Apparently you are searching for "played " with a space at the end, although your question is a bit vague.
See my hybrid JScript/batch utility called REPL.BAT that does regular expression search and replace. It works on any modern version of Windows from XP onward, and it does not require installation of any 3rd party executeables.
Using REPL.BAT:
type "yourFile.txt"|repl "played ...." "played INFA" >"yourFile.txt.new"
move /y "yourFile.txt.new" "yourFile.txt" >nul
I use this at times: sar.bat
::Search and replace
#echo off
if "%~3"=="" (
echo.Search and replace
echo Syntax:
echo "%~nx0" "filein.txt" "fileout.txt" "regex" "replace_text" [first]
echo.
echo.EG: change the first time apple appears on each line, to orange
echo."%~nx0" "my text old.txt" "my text changed.txt" "apple" "orange" first
echo.
echo.People that are familiar with regular expressions can use some:
echo.
echo.Change every line starting from old (and everything after it^) to new
echo."%~nx0" "my text old.txt" "my text changed.txt" "old.*" "new"
echo.
echo.If [first] is present only the first occurrence per line is changed
echo.
echo.To make the search case sensitive change
echo.IgnoreCase= from True to False
echo.
pause
goto :EOF
)
if "%~5"=="" (set global=true) else (set global=false)
set s=regex.replace(wscript.stdin.readall,"%~4")
>_.vbs echo set regex=new regexp
>>_.vbs echo regex.global=%global%
>>_.vbs echo regEx.IgnoreCase=True
>>_.vbs echo regex.pattern="%~3"
>>_.vbs echo wscript.stdOut.write %s%
cscript /nologo _.vbs <"%~1" >"%~2"
del _.vbs
Get yourself a copy of sed for your OS and call it from a batch file.
sed -e 's/socc/INFA/g' inputFileName > outputFileName
#echo off
setlocal EnableDelayedExpansion
set "search=played "
set replacement=INFA
set numChars=4
set line=the mad fox jumped of the old vine and played soccer.
rem The characters after the equal-signs are Ascii-254
for /F "tokens=1* delims=■" %%a in ("!line:%search%=■!") do (
set "rightPart=%%b"
set "line=%%a%search%%replacement%!rightPart:~%numChars%!"
)
echo !line!
Output:
the mad fox jumped of the old vine and played INFAer.
You must insert previous code into a loop that process the entire file. I left that part as an exercise for you...

How to run batch script without using *.bat extension

Is there any method in Windows through which we can execute a batch script without *.bat extension?
This is an interesting topic to me! I want to do some observations about it.
The important point first: A Batch file is a file with .BAT or .CMD extension. Period. Batch files can achieve, besides the execution of usual DOS commands, certain specific Batch-file facilities, in particular:
Access to Batch file parameters via %1 %2 ... and execution of SHIFT command.
Execution of GOTO command.
Execution of CALL :NAME command (internal subroutine).
Execution of SETLOCAL/ENDLOCAL commands.
Now the funny part: Any file can be redirected as input for CMD.exe so the DOS commands contained in it are executed in a similar way of a Batch file, with some differences. The most important one is that previous Batch-file facilities will NOT work. Another differences are illustrated in the NOT-Batch file below (I called it BATCH.TXT):
#echo off
rem Echo off just suppress echoing of the prompt and each loop of FOR command
rem but it does NOT suppress the listing of these commands!
rem Pause command does NOT pause, because it takes the character that follows it
pause
X
rem This behavior allows to put data for a SET /P command after it
set /P var=Enter data:
This is the data for previous command!
echo Data read: "%var%"
rem Complex FOR/IF commands may be assembled and they execute in the usual way:
for /L %i in (1,1,5) do (
set /P line=
if "!line:~0,6!" equ "SHOW: " echo Line read: !line:~6!
)
NOSHOW: First line read
SHOW: Second line
NOSHOW: This is third line
SHOW: The line number 4
NOSHOW: Final line, number five
rem You may suppress the tracing of the execution redirecting CMD output to NUL
rem In this case, redirect output to STDERR to display messages in the screen
echo This is a message redirected to STDERR >&2
rem GOTO command doesn't work:
goto label
goto :EOF
rem but both EXIT and EXIT /B commands works:
exit /B
:label
echo Never reach this point...
To execute previous file, type: CMD /V:ON < BATCH.TXT
The /V switch is needed to enable delayed expansion.
More specialized differences are related to the fact that commands in the NOT-Batch file are executed in the command-line context, NOT the Batch-file context. Perhaps Dave or jeb could elaborate on this point.
EDIT: Additional observations (batch2.txt):
#echo off
rem You may force SET /P command to read the line from keyboard instead of
rem from following lines by redirecting its input to CON device.
rem You may also use CON device to force commands output to console (screen),
rem this is easier to write and read than >&2
echo Standard input/output operations> CON
echo/> CON
< CON set /P var=Enter value: > CON
echo/> CON
echo The value read is: "%var%"> CON
Execute previous file this way: CMD < BATCH2.TXT > NUL
EDIT: More additional observations (batch3.txt)
#echo off
rem Dynamic access to variables that usually requires DelayedExpansion via "call" trick
rem Read the next four lines; "next" means placed after the FOR command
rem (this may be used to simulate a Unix "here doc")
for /L %i in (1,1,4) do (
set /P line[%i]=
)
Line one of immediate data
This is second line
The third one
And the fourth and last one...
(
echo Show the elements of the array read:
echo/
for /L %i in (1,1,4) do call echo Line %i- %line[%i]%
) > CON
Execute this file in the usual way: CMD < BATCH3.TXT > NUL
Interesting! Isn't it?
EDIT: Now, GOTO and CALL commands may be simulated in the NotBatch.txt file!!! See this post.
Antonio
Just use:
type mybat.txt | cmd
Breaking it down...
type mybat.txt reads mybat.txt as a text file and prints the contents. The | says capture anything getting printed by the command on its left and pass it as an input to the command on its right. Then cmd (as you can probably guess) interprets any input it receives as commands and executes them.
In case you were wondering... you can replace cmd with bash to run on Linux.
in my case, to make windows run files without extension (only for *.cmd, *.exe) observed, i have missed pathext variable (in system varailbles) to include .cmd. Once added i have no more to run file.cmd than simply file.
environment variables --> add/edit system variable to include .cmd;.exe (ofcourse your file should be in path)
It could be possible yes, but probably nor in an easy way =) cause first of all.. security.
I try to do the same thing some year ago, and some month ago, but i found no solution about it.. you could try to do
execu.cmd
type toLaunch.txt >> bin.cmd
call bin.cmd
pause > nul
exit
then in toLaunch.txt put
#echo off
echo Hello!
pause > nul
exit
just as example, it will "compile" the code, then it will execute the "output" file, that is just "parse"
instead of parsed you could also just rename use and maybe put an auto rename inside the script using inside toLaunch.txt
ren %0 %0.txt
hope it helped!
It is possible at some degree. You'll need an admin permissions to run assoc and ftype commands. Also a 'caller' script that will use your code:
Lets say the extension you want is called .scr.
Then execute this script as admin:
#echo off
:: requires Admin permissions
:: allows a files with .scr (in this case ) extension to act like .bat/.cmd files.
:: Will create a 'caller.bat' associated with the extension
:: which will create a temp .bat file on each call (you can consider this as cheating)
:: and will call it.
:: Have on mind that the %0 argument will be lost.
rem :: "installing" a caller.
if not exist "c:\scrCaller.bat" (
echo #echo off
echo copy "%%~nx1" "%%temp%%\%%~nx1.bat" /Y ^>nul
echo "%%temp%%\%%~nx1.bat" %%*
) > c:\scrCaller.bat
rem :: associating file extension
assoc .scr=scrfile
ftype scrfile=c:\scrCaller "%%1" %%*
You even will be able to use GOTO and CALL and the other tricks you know. The only limitation is that the the %0 argument will be lost ,tough it can be hardcoded while creating the temp file.
As a lot of languages compile an .exe file for example I think this a legit approach.
If you want variables to be exported to the calling batch file, you could use
for /F "tokens=*" %%g in (file.txt) do (%%g)
This metod has several limitations (don't use :: for comments), but its perfect for configuration files.
Example:
rem Filename: "foo.conf"
rem
set option1=true
set option2=false
set option3=true
#echo off
for /F "tokens=*" %%g in (foo.conf) do (%%g)
echo %option1%
echo %option2%
echo %option3%
pause

batch script - to remove duplicate tokens in file

I have duplicate tokens in text file I would like to create new text file without the duplicate tokens (keeping the delimiters)
The delimiter is:~#^*^#~
example file:
aaa~#^*^#~bbb~#^*^#~aaa~#^*^#~bbb~#^*^#~aaa~#^*^#~bbb~#^*^#~aaa~#^*^#~bbb~#^*^#~aaa~#^*^#~bbb~#^*^#~xxx~#^*^#~bbb~#^*^#~aaa~#^*^#~bbb~#^*^#~aaa~#^*^#~bbb~#^*^#~aaa~#^*^#~bbb~#^*^#~aaa~#^*^#~bbb~#^*^#~aaa~#^*^#~bbb~#^*^#~aaa~#^*^#~bbb~#^*^#~aaa~#^*^#~bbb~#^*^#~aaa~#^*^#~bbb~#^*^#~aaa~#^*^#~bbb
Result should be:
aaa~#^*^#~bbb~#^*^#~xxx
I found script that remove duplicate lines:
==================================
#echo off > outfile
if %1'==' echo which file? && goto :eof
if not exist %1 echo %1 not found && goto :eof
for /f "tokens=* delims= " %%a in (%1) do (
find "%%a" < outfile > nul
if errorlevel 1 echo %%a >> outfile
)
The script work nice for duplicate lines,
So i modified the delims from:
"tokens=* delims="
to
"tokens=* delims=~#^*^#~"
But it wont work, What am i doing wrong? is one of the delimiter characters reserved word?
Thank you for any suggestion.
The FOR DELIMITERS option treats each character as a delimiter. You cannot use a sequence of characters as a delimiter, so it will not help in your case.
Windows batch is a marginal text processor for simple tasks. You have a particularly nasty problem for a Windows batch file. It might be doable, but the code would be complicated and slow at best.
I strongly advise you use some other tool better suited for text processing. I believe any of the following could be used:
VBscript
JavaScript
Powershell
3rd party tools like Gnu sed for Windows, perl, ... many more
Windows batch is probably about the worst choice you could make, especially for your problem. (this is coming from someone who really enjoys using batch)

String processing using Batch Script

I'm currently creating a batch script that has to loop through the lines in a file, checking for some string, and if theres a match prefix that string with a '#' (comment it out).
I'm perfectly new to batch script, all I got this far is:
for /f %%j in (CMakeLists.txt) do (
if "%%j"=="Extensions_AntTweakBar" (
echo lol1
)
if "%%j"=="Extensions_Inspection" (
echo lol2
)
if "%%j"=="Extensions_InspectionBar" (
echo lol3
)
)
So my current issue is, I don't know how to operate on string within batch scripts. If someone could help me out that would be appreciated :)
You can just use the text you want to append followed by your variable generally.
C:\>set MY_VAR=Hello world!
C:\>echo #%MY_VAR%
#Hello world!
C:\>set MY_VAR=#%MY_VAR%
C:\>echo %MY_VAR%
#Hello world!
If you're just doing echo, that's fine. echo #%%j will do what you need.
But if you want to set the line to a variable, you have to enable delayed expansion. Add setlocal ENABLEDELAYEDEXPANSION to the top of your file and then surround your variables with ! instead of %. For example (and notice that I've added delims= to put the entire line in %%j instead of the first word on the line):
#echo off
setlocal ENABLEDELAYEDEXPANSION
set LINE=
for /f "delims=" %%j in (CMakeLists.txt) do (
set LINE=%%j
if "%%j"=="Extensions AntTweakBar" (
set LINE=#%%j
)
if "%%j"=="Extensions Inspection" (
set LINE=#%%j
)
if "%%j"=="Extensions InspectionBar" (
set LINE=#%%j
)
echo !LINE!
)
Given this input file:
Extensions AntTweakBar
some text
Extensions Inspection
Extensions What?
some more text
Extensions InspectionBar
Extensions InspectionBar this line doesn't match because delims= takes all text
even more text
The above script produces this output:
C:\>comment.bat
#Extensions AntTweakBar
some text
#Extensions Inspection
Extensions What?
some more text
#Extensions InspectionBar
Extensions InspectionBar this line doesn't match because delims= takes all text
even more text
And of course removing #echo off will help you debug problems.
But all that being said, you're about at the limit of what you can accomplish with batch string processing. If you still want to use batch commands, you may need to start writing lines to temporary files and using findstr with a regex.
Without a better understanding of what you want inside your loop or what your CMakeLists.txt file looks like, try this on for starters:
FINDSTR "SOMETHING" %%J && ECHO #%%J || ECHO %%J
The && makes the second command (the ECHO) conditional on the first command exiting without an error state, and the || is like a logical OR and it runs when the first one doesn't.
Really, for modifying the internals of a text file you are probably going to be much better off using either sed or awk - win32 binaries can be found in the UnxUtils project.

Resources