DosTips.com

A Forum all about DOS Batch
It is currently 04 Dec 2016 02:08

All times are UTC-06:00




Post new topic  Reply to topic  [ 44 posts ]  Go to page Previous 1 2 3
Author Message
PostPosted: 31 Jul 2012 11:19 
Offline
Expert

Joined: 10 Feb 2012 02:20
Posts: 6033
View the source files and see if there is an extra $ in there. Maybe there are two in a row at the end??


Top
   
PostPosted: 31 Jul 2012 11:25 
Offline

Joined: 30 Jul 2012 07:09
Posts: 13
I've done that. :( I opened up the file in ultra edit and did a search -> find -> count for the string. there is one less than what my script is reporting. It may be that this script adds one too many line breaks.


Top
   
PostPosted: 31 Jul 2012 11:29 
Offline

Joined: 30 Jul 2012 07:09
Posts: 13
I'm not familar with SED so I'm looking at their site for some more insight on the switches. I'm curious as to the \ before the search string
eg. sed "s/\&/n\/g" . . . . .


Top
   
PostPosted: 31 Jul 2012 11:30 
Offline
Expert

Joined: 23 Dec 2011 13:59
Posts: 3367
kmbarre wrote:
I've done that. :( I opened up the file in ultra edit and did a search -> find -> count for the string. there is one less than what my script is reporting. It may be that this script adds one too many line breaks.

You sure there isn't an existing Line Break in the file already. LF or CR?


Top
   
PostPosted: 31 Jul 2012 12:15 
Offline
Expert

Joined: 10 Feb 2012 02:20
Posts: 6033
If it is consistant with your files then just subtract 1 from the total

Here's a single batch file to do *.txt

Code: Select all
@echo off
setlocal enabledelayedexpansion
for %%a in (*.txt) do (
sed "s/\$/\n/g" "%%a" >file.tmp
for /f %%b in ('find /c /v "" ^<file.tmp') do set /a num=%%b-1
echo %%a  - !num!
del file.tmp
)
pause


Top
   
PostPosted: 31 Jul 2012 12:18 
Offline
Expert

Joined: 10 Feb 2012 02:20
Posts: 6033
kmbarre wrote:
I'm not familar with SED so I'm looking at their site for some more insight on the switches. I'm curious as to the \ before the search string
eg. sed "s/\&/n\/g" . . . . .


It is escaping the $ but you changed it to &.


Top
   
PostPosted: 31 Jul 2012 12:20 
Offline

Joined: 30 Jul 2012 07:09
Posts: 13
nope. no line breaks. I've looked at he files with a hex editor and found nothing. For all files, regardless of size, they contain only one line. no CR or LF.


Top
   
PostPosted: 31 Jul 2012 12:21 
Offline

Joined: 30 Jul 2012 07:09
Posts: 13
Sorry. Not & but $. My actual search string is not a $ but using it as an example


Top
   
PostPosted: 31 Jul 2012 12:24 
Offline
Expert

Joined: 10 Feb 2012 02:20
Posts: 6033
kmbarre wrote:
Sorry. Not & but $. My actual search string is not a $ but using it as an example



if you don't give correct details then you can often get broken code. The escape is not necessary in many cases.


Top
   
PostPosted: 01 Aug 2012 10:25 
Offline

Joined: 01 Aug 2012 10:17
Posts: 1
Hi, I tried to create a code to find a string "MK" in series of files in a folder. The output file should list file name and count of string "MK". Somehow countstr does not get couted in the code below. Please help!

@echo off
set pathname=Z:\CAD\PDMS\Project\HIBFLR\BTHINFO\MAR
set outpath=%pathname%\1.prn
if exist %outpath% del %outpath%
set "str=MK"

rem cd %pathname%
rem ECHO %pathname%
Z:
set pathletter=%CD:~0,2%
rem -----------------------
ECHO Path Drive Changed to %pathletter%

FOR %%j IN (%pathname%\*.txt ) DO (

set cnt=0
for /f ^"eol^=^

delims^=^" %%a in ('"findstr /i "/c:%str%" %%j"') do set "ln=%%a"&call :countStr
echo Spool for %%j is %cnt%
echo Spool for %%j is %cnt% >>%outpath%
)
pause
exit /b
set cnt=0

:countStr
setlocal enableDelayedExpansion
:loop
if defined ln (
set "ln2=!ln:*%str%=!"
if "!ln2!" neq "!ln!" (
set "ln=!ln2!"
set /a "cnt+=1"
goto :loop
)
)
endlocal & set cnt=%cnt%
exit /b


Top
   
PostPosted: 01 Aug 2012 13:48 
Offline
Expert

Joined: 10 Feb 2012 02:20
Posts: 6033
You could also use the technique using GnuSED.

Your issue can be illustrated here:

Code: Select all
@echo off
(
for %%a in (one) do call :next %%a
echo ...[%var%]...
)
pause
goto :EOF
:next
set var=two


and this:

Code: Select all
@echo off
setlocal enabledelayedexpansion
(
for %%a in (one) do call :next %%a
echo ...[!var!]...
)
pause
goto :EOF
:next
set var=two


Top
   
PostPosted: 02 Jul 2013 21:46 
Offline

Joined: 02 Jul 2013 21:28
Posts: 1
The example script (dbenham) does not handle "<" being in the input file. I'm searching an xml document am am getting the error "< was unexpected at this time". What changes need to be made to your example script for this case? I executed a find/replace for "<" to test that this is the culprit.


Top
   
PostPosted: 02 Jul 2013 23:45 
Offline
Expert

Joined: 10 Feb 2012 02:20
Posts: 6033
There is a new tool developed recently that can search in handy ways - see here for findrepl

viewtopic.php?f=3&t=4697


Top
   
PostPosted: 03 Jul 2013 08:58 
Offline
Expert

Joined: 06 Dec 2011 22:15
Posts: 1237
Location: México City, México
For example, using FindRepl.bat:
Code: Select all
> < test.txt FindRepl.bat /N
1:hand randomtext<hand randomtext
2:hand <randomtext
3:hand random<text
4:hand randomtext

> < test.txt FindRepl.bat "hand" /$:0 /N
1: "hand" "hand"
2: "hand"
3: "hand"
4: "hand"

> echo %errorlevel%
5

> < test.txt FindRepl.bat "<" /$:0 /N
1: "<"
2: "<"
3: "<"

> echo %errorlevel%
3


Antonio


Top
   
Display posts from previous:  Sort by  
Post new topic  Reply to topic  [ 44 posts ]  Go to page Previous 1 2 3

All times are UTC-06:00


Who is online

Users browsing this forum: Bing [Bot], Google [Bot], Yahoo [Bot] and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Limited