How to search and get the values from the file
Moderator: DosItHelp
How to search and get the values from the file
Hi,
I have a file which has the information like below
BEGIN DSJOB
Identifier "CopyOfExmplCdc_Data"
DateModified "2012-09-23"
TimeModified "13.18.56"
BEGIN DSJOB
Identifier "Data_Data"
DateModified "2012-09-23"
TimeModified "13.18.56"
END DSJOB
I need to get the DSJOB names that are in identifier category under BEGIN DSJOB command and I need to have output as CopyOfExmplCdc_Data and Data_Data with out any quotes. I know that we can achive this is unix using grep and tr commands but how to achive this in batch script.
I have a file which has the information like below
BEGIN DSJOB
Identifier "CopyOfExmplCdc_Data"
DateModified "2012-09-23"
TimeModified "13.18.56"
BEGIN DSJOB
Identifier "Data_Data"
DateModified "2012-09-23"
TimeModified "13.18.56"
END DSJOB
I need to get the DSJOB names that are in identifier category under BEGIN DSJOB command and I need to have output as CopyOfExmplCdc_Data and Data_Data with out any quotes. I know that we can achive this is unix using grep and tr commands but how to achive this in batch script.
Re: How to search and get the values from the file
This may work. Input file is file.txt and output file is file.log
@echo off
for /f "tokens=1,*" %%a in ('find "Identifier " ^<"file.txt"') do >>file.log echo %%~b
@echo off
for /f "tokens=1,*" %%a in ('find "Identifier " ^<"file.txt"') do >>file.log echo %%~b
Re: How to search and get the values from the file
Hi tried below command. I want to search for .dsx file extensions and also Identifier can be in multiple places I want only the Identifier below BEGIN DSJOB.
for /f "tokens=1,*" %%a in ('find "Identifier " ^<"H:\Data\Dir\*.dsx"') do >> H:\Data\Dir\file.log echo %%~b
but I'm getting below error
H:\Data\Dir>REM ========= as command line input parameters to the script ======
===========================
The filename, directory name, or volume label syntax is incorrect.
Thanks,
Raju
for /f "tokens=1,*" %%a in ('find "Identifier " ^<"H:\Data\Dir\*.dsx"') do >> H:\Data\Dir\file.log echo %%~b
but I'm getting below error
H:\Data\Dir>REM ========= as command line input parameters to the script ======
===========================
The filename, directory name, or volume label syntax is incorrect.
Thanks,
Raju
Re: How to search and get the values from the file
The Batch file below display the value enclosed in quotes that appear in the line below "BEGIN DSJOB" in file.txt:
Antonio
Code: Select all
@echo off
setlocal EnableDelayedExpansion
set lastLine=0
< file.txt (for /F "delims=:" %%a in ('findstr /N /C:"BEGIN DSJOB" file.txt') do (
set /A skip=%%a-lastLine+1, lastLine=%%a+1
for /L %%i in (1,1,!skip!) do set /P line=
for /F tokens^=2^ delims^=^" %%b in ("!line!") do echo %%b
))
Antonio
Re: How to search and get the values from the file
Didn't Dave have some FINDSTR code to search across line breaks and still get the 2nd line. Or does it only get the first line per his documentation?
Re: How to search and get the values from the file
I'm getting an error saying, The system cannot find the file specified.
H:\Data\Dir>setlocal EnableDelayedExpansion
H:\Data\Dir>set lastLine=0
H:\Data\Dir>(for /F "delims=:" %a in ('findstr /N /C:"BEGIN DSJOB" Test_Jobs.dsx
') do (
set /A skip=%a-lastLine+1, lastLine=%a+1
for /L %i in (1 1 !skip!) do set /P line=
for /F tokens=2 delims=" %b in ("!line!") do echo %b
) ) 0<file.txt
The system cannot find the file specified.
Thanks,
Raju
H:\Data\Dir>setlocal EnableDelayedExpansion
H:\Data\Dir>set lastLine=0
H:\Data\Dir>(for /F "delims=:" %a in ('findstr /N /C:"BEGIN DSJOB" Test_Jobs.dsx
') do (
set /A skip=%a-lastLine+1, lastLine=%a+1
for /L %i in (1 1 !skip!) do set /P line=
for /F tokens=2 delims=" %b in ("!line!") do echo %b
) ) 0<file.txt
The system cannot find the file specified.
Thanks,
Raju
Re: How to search and get the values from the file
@somu_june - regarding Aacini's code, you need to also change <file.txt to Test_Jobs.dsx. His code works.
@Squashman - the FINDSTR multi-line search only returns the first line in the search
------------------------
Here is a FOR /F solution - EDIT - fixed code to only take Identifier lines immediately after BEGIN DSJOB line
------------------
I prefer to use my REPL.BAT hybrid JScript/batch regex search and replace utility. It should be much faster than any pure batch solution, especially if the file is large.
Assuming you have REPL.BAT in your path or current directory, then the following (long) one liner works both from the command line and a batch file:
The above first uses REPL.BAT to remove the new line after BEGIN DSJOB so as to put the Identifier line on the same line. Then FINDSTR is used to preserve only lines that start with BEGIN DSJOBIdentifier. Finally, one more REPL.BAT is used to extract the desired value from the remaining lines.
Dave Benham
@Squashman - the FINDSTR multi-line search only returns the first line in the search
------------------------
Here is a FOR /F solution - EDIT - fixed code to only take Identifier lines immediately after BEGIN DSJOB line
Code: Select all
@echo off
set "begin="
for /f tokens^=1-2^ delims^=^" %%A in (Test_Jobs.dsx) do (
if "%%A" equ "BEGIN DSJOB" (
set "begin=1"
) else (
if defined begin if "%%A" equ "Identifier " echo %%B
set "begin="
)
)
------------------
I prefer to use my REPL.BAT hybrid JScript/batch regex search and replace utility. It should be much faster than any pure batch solution, especially if the file is large.
Assuming you have REPL.BAT in your path or current directory, then the following (long) one liner works both from the command line and a batch file:
Code: Select all
type Test_Jobs.dsx | repl "^(BEGIN DSJOB)\r?\n" "$1" m | findstr /bc:"BEGIN DSJOBIdentifier " | repl ".*\x22(.*)\x22.*" $1
The above first uses REPL.BAT to remove the new line after BEGIN DSJOB so as to put the Identifier line on the same line. Then FINDSTR is used to preserve only lines that start with BEGIN DSJOBIdentifier. Finally, one more REPL.BAT is used to extract the desired value from the remaining lines.
Dave Benham
Last edited by dbenham on 21 Jun 2013 09:08, edited 1 time in total.
Re: How to search and get the values from the file
Hi,
below solution is searching for all the Identifier's in the file, but I need to search for the identifier immedately after BEGIN DSJOB and not any identifier that are under other categories like BEGIN DSSCRIPT or BEGIN DSBPBINARY etc. When I run below batch file this this not providing any output and I'm getting an empty output
@echo off
set "begin="
for /f tokens^=1-2^ delims^=^" %%A in (Test_Jobs.dsx) do (
if "%%A" equ "BEGIN DSJOB" (
set "begin=1"
) else (
if defined begin if "%%A" equ "Identifier " (
echo %%B
set "begin="
)
)
)
For script two I'm getting below error due to not available of repl bat comand. If need to download it, from where I can download this
H:\Data\Dir>type Test_Jobs.dsx | repl "^(BEGIN DSJOB)\r?\n" "$1" m | findstr
/bc:"BEGIN DSJOBIdentifier " | repl ".*\x22(.*)\x22.*" $1
'repl' is not recognized as an internal or external command,
operable program or batch file.
below solution is searching for all the Identifier's in the file, but I need to search for the identifier immedately after BEGIN DSJOB and not any identifier that are under other categories like BEGIN DSSCRIPT or BEGIN DSBPBINARY etc. When I run below batch file this this not providing any output and I'm getting an empty output
@echo off
set "begin="
for /f tokens^=1-2^ delims^=^" %%A in (Test_Jobs.dsx) do (
if "%%A" equ "BEGIN DSJOB" (
set "begin=1"
) else (
if defined begin if "%%A" equ "Identifier " (
echo %%B
set "begin="
)
)
)
For script two I'm getting below error due to not available of repl bat comand. If need to download it, from where I can download this
H:\Data\Dir>type Test_Jobs.dsx | repl "^(BEGIN DSJOB)\r?\n" "$1" m | findstr
/bc:"BEGIN DSJOBIdentifier " | repl ".*\x22(.*)\x22.*" $1
'repl' is not recognized as an internal or external command,
operable program or batch file.
Re: How to search and get the values from the file
somu_june wrote:For script two I'm getting below error due to not available of repl bat comand. If need to download it, from where I can download this
Dave put a link to hit in his last post.
Re: How to search and get the values from the file
@somu_june - Regarding my first script - I had a silly logic error. I edited my prior post to fix the code.
Re: How to search and get the values from the file
Thanks I can see the repl.bat script and I tried the command
type Test_Jobs.dsx | repl "^(BEGIN DSJOB)\r?\n" "$1" m | findstr /bc:"BEGIN DSJOBIdentifier " | repl ".*\x22(.*)\x22.*" $1
but its not providing any output
H:\Data\Dir>type Mahesh_Jobs.dsx | repl "^(BEGIN DSJOB)\r?\n" "$1" m | finds
tr /bc:"BEGIN DSJOBIdentifier " | repl ".*\x22(.*)\x22.*" $1
H:\Data\Dir>
Thanks,
Somaraju
type Test_Jobs.dsx | repl "^(BEGIN DSJOB)\r?\n" "$1" m | findstr /bc:"BEGIN DSJOBIdentifier " | repl ".*\x22(.*)\x22.*" $1
but its not providing any output
H:\Data\Dir>type Mahesh_Jobs.dsx | repl "^(BEGIN DSJOB)\r?\n" "$1" m | finds
tr /bc:"BEGIN DSJOBIdentifier " | repl ".*\x22(.*)\x22.*" $1
H:\Data\Dir>
Thanks,
Somaraju
Re: How to search and get the values from the file
Extra spaces or other slight changes in your input file could cause problems with code as written, but it shouldn't take much to fix. I created a sample file based on what I saw in your question and tested - and it worked perfectly.
Try reading my explanation, as well as the documentation embedded within the REPL.BAT script. Then see if you can't figure out how to make it work.
It helps to tackle one transformation at a time. For example, first start out with a single pipe into the first REPL command and see what output that produces. Make changes until it gets the correct output, then move on to the next pipe. etc.
Dave Benham
Try reading my explanation, as well as the documentation embedded within the REPL.BAT script. Then see if you can't figure out how to make it work.
It helps to tackle one transformation at a time. For example, first start out with a single pipe into the first REPL command and see what output that produces. Make changes until it gets the correct output, then move on to the next pipe. etc.
Dave Benham
Re: How to search and get the values from the file
I tried with Fixed code but no luck
@echo off
set "begin="
for /f tokens^=1-2^ delims^=^" %%A in (Test_Jobs.dsx) do (
if "%%A" equ "BEGIN DSJOB" (
set "begin=1"
) else (
if defined begin if "%%A" equ "Identifier " echo %%B
set "begin="
)
)
Thanks,
Somaraju
@echo off
set "begin="
for /f tokens^=1-2^ delims^=^" %%A in (Test_Jobs.dsx) do (
if "%%A" equ "BEGIN DSJOB" (
set "begin=1"
) else (
if defined begin if "%%A" equ "Identifier " echo %%B
set "begin="
)
)
Thanks,
Somaraju
Re: How to search and get the values from the file
Can you supply an actual log file for someone to download and see where the problem is?
As you didn't use code tags with your sample data it could be different that what was displayed.
Or it could be unicode etc.
As you didn't use code tags with your sample data it could be different that what was displayed.
Or it could be unicode etc.
Re: How to search and get the values from the file
somu_june wrote:I tried with Fixed code but no luck
Then you need to provide some real life data for people to test with.
With the example input you gave us:
Code: Select all
BEGIN DSJOB
Identifier "CopyOfExmplCdc_Data"
DateModified "2012-09-23"
TimeModified "13.18.56"
BEGIN DSJOB
Identifier "Data_Data"
DateModified "2012-09-23"
TimeModified "13.18.56"
END DSJOB
Dave's code outputs just fine:
Code: Select all
C:\batch files\DosTips>Parse.bat
CopyOfExmplCdc_Data
Data_Data