How to search and get the values from the file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
somu_june
Posts: 30
Joined: 19 Jun 2013 20:26

How to search and get the values from the file

#1 Post by somu_june » 19 Jun 2013 21:49

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.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: How to search and get the values from the file

#2 Post by foxidrive » 19 Jun 2013 22:02

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

somu_june
Posts: 30
Joined: 19 Jun 2013 20:26

Re: How to search and get the values from the file

#3 Post by somu_june » 20 Jun 2013 07:59

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

Aacini
Expert
Posts: 1930
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: How to search and get the values from the file

#4 Post by Aacini » 20 Jun 2013 18:42

The Batch file below display the value enclosed in quotes that appear in the line below "BEGIN DSJOB" in file.txt:

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

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: How to search and get the values from the file

#5 Post by Squashman » 20 Jun 2013 20:24

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?

somu_june
Posts: 30
Joined: 19 Jun 2013 20:26

Re: How to search and get the values from the file

#6 Post by somu_june » 20 Jun 2013 21:03

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

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: How to search and get the values from the file

#7 Post by dbenham » 20 Jun 2013 21:46

@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

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.

somu_june
Posts: 30
Joined: 19 Jun 2013 20:26

Re: How to search and get the values from the file

#8 Post by somu_june » 21 Jun 2013 08:21

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.

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: How to search and get the values from the file

#9 Post by Squashman » 21 Jun 2013 08:25

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.

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: How to search and get the values from the file

#10 Post by dbenham » 21 Jun 2013 09:10

@somu_june - Regarding my first script - I had a silly logic error. I edited my prior post to fix the code.

somu_june
Posts: 30
Joined: 19 Jun 2013 20:26

Re: How to search and get the values from the file

#11 Post by somu_june » 21 Jun 2013 10:56

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

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: How to search and get the values from the file

#12 Post by dbenham » 21 Jun 2013 11:13

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

somu_june
Posts: 30
Joined: 19 Jun 2013 20:26

Re: How to search and get the values from the file

#13 Post by somu_june » 21 Jun 2013 11:16

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

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: How to search and get the values from the file

#14 Post by foxidrive » 21 Jun 2013 11:29

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.

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: How to search and get the values from the file

#15 Post by Squashman » 21 Jun 2013 11:32

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

Post Reply