View unanswered posts | View active topics It is currently 01 Sep 2014 01:30



Post new topic Reply to topic  [ 44 posts ]  Go to page Previous  1, 2, 3
Count string occurrences in text file 
Author Message

Joined: 10 Feb 2012 02:20
Posts: 4114
Post Re: Count string occurrences in text file
View the source files and see if there is an extra $ in there. Maybe there are two in a row at the end??


31 Jul 2012 11:19
Profile

Joined: 30 Jul 2012 07:09
Posts: 13
Post Re: Count string occurrences in text file
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.


31 Jul 2012 11:25
Profile

Joined: 30 Jul 2012 07:09
Posts: 13
Post Re: Count string occurrences in text file
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" . . . . .


31 Jul 2012 11:29
Profile

Joined: 23 Dec 2011 13:59
Posts: 1802
Post Re: Count string occurrences in text file
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?


31 Jul 2012 11:30
Profile

Joined: 10 Feb 2012 02:20
Posts: 4114
Post Re: Count string occurrences in text file
If it is consistant with your files then just subtract 1 from the total

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

Code:
@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


31 Jul 2012 12:15
Profile

Joined: 10 Feb 2012 02:20
Posts: 4114
Post Re: Count string occurrences in text file
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 &.


31 Jul 2012 12:18
Profile

Joined: 30 Jul 2012 07:09
Posts: 13
Post Re: Count string occurrences in text file
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.


31 Jul 2012 12:20
Profile

Joined: 30 Jul 2012 07:09
Posts: 13
Post Re: Count string occurrences in text file
Sorry. Not & but $. My actual search string is not a $ but using it as an example


31 Jul 2012 12:21
Profile

Joined: 10 Feb 2012 02:20
Posts: 4114
Post Re: Count string occurrences in text file
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.


31 Jul 2012 12:24
Profile

Joined: 01 Aug 2012 10:17
Posts: 1
Post Re: Count string occurrences in text file
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


01 Aug 2012 10:25
Profile

Joined: 10 Feb 2012 02:20
Posts: 4114
Post Re: Count string occurrences in text file
You could also use the technique using GnuSED.

Your issue can be illustrated here:

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


and this:

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


01 Aug 2012 13:48
Profile

Joined: 02 Jul 2013 21:28
Posts: 1
Post Re: Count string occurrences in text file
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.


02 Jul 2013 21:46
Profile

Joined: 10 Feb 2012 02:20
Posts: 4114
Post Re: Count string occurrences in text file
There is a new tool developed recently that can search in handy ways - see here for findrepl

viewtopic.php?f=3&t=4697


02 Jul 2013 23:45
Profile
Expert

Joined: 06 Dec 2011 22:15
Posts: 718
Location: México City, México
Post Re: Count string occurrences in text file
For example, using FindRepl.bat:
Code:
> < 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


03 Jul 2013 08:58
Profile
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 44 posts ]  Go to page Previous  1, 2, 3


Who is online

Users browsing this forum: Baidu [Spider], Bing [Bot], Google [Bot], Yahoo [Bot] and 21 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

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Forum style by Vjacheslav Trushkin for Free Forums/DivisionCore.