two findstr questions

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
berserker
Posts: 95
Joined: 18 Dec 2013 00:51

Re: two findstr questions

#16 Post by berserker » 06 Jan 2014 01:44

grep has its origin in the unix world. you can go to any unix or linux forum site to ask. or you can go to stackoverflow.

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

Re: two findstr questions

#17 Post by Squashman » 06 Jan 2014 06:59

bars143 wrote:can you recommend me a forum site specializing in grep.exe usage like dostips.com in cmd.exe usage?


thanks,

bars

You can't compare GREP.exe to CMD.exe. That is like comparing Apples to Oranges. CMD.exe is a shell like BASH, CSH and the likes on the unix side. GREP would be more to the liking of FINDSTR but has much more options which is why the hard working people here created native utilities like REPL and FINDREPL.

If you want help with something it isn't that hard to GOOGLE search.

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

Re: two findstr questions

#18 Post by foxidrive » 06 Jan 2014 07:21

I think bars was tongue in cheek...

bars143
Posts: 87
Joined: 01 Sep 2013 20:47

Re: two findstr questions

#19 Post by bars143 » 06 Jan 2014 10:05

Squashman wrote:
bars143 wrote:can you recommend me a forum site specializing in grep.exe usage like dostips.com in cmd.exe usage?


thanks,

bars

You can't compare GREP.exe to CMD.exe. That is like comparing Apples to Oranges. CMD.exe is a shell like BASH, CSH and the likes on the unix side. GREP would be more to the liking of FINDSTR but has much more options which is why the hard working people here created native utilities like REPL and FINDREPL.

If you want help with something it isn't that hard to GOOGLE search.


thanks for information.
now i will choose and prioritize cmd.exe usage/.bat script first as my netbook is a window 7 32bit native. i found the like of awk , whose manual is a lot more to learn (and most be efficient with unix OS ?).

i decided and back to .bat scripting.

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

Re: two findstr questions

#20 Post by Squashman » 06 Jan 2014 13:55

Don't be discouraged by my post. Most of those famous unix utilities have been ported so that they can be used in Windows. Look at the nice thread Brian has started about AWK. We have plenty of people around here that also use SED and GREP. If you think you need to use one of those utilities in your batch file just ask. Someone will either tell you that it can be done with native commands or they will help you with the syntax of the command you are trying to incorporate into your script.

bars143
Posts: 87
Joined: 01 Sep 2013 20:47

Re: two findstr questions

#21 Post by bars143 » 06 Jan 2014 18:19

Squashman wrote:Don't be discouraged by my post. Most of those famous unix utilities have been ported so that they can be used in Windows. Look at the nice thread Brian has started about AWK. We have plenty of people around here that also use SED and GREP. If you think you need to use one of those utilities in your batch file just ask. Someone will either tell you that it can be done with native commands or they will help you with the syntax of the command you are trying to incorporate into your script.


im more interested in the likeness of wget which is portable, standalone and easy.

grep requires other dll.
awk is more like php.

its better for me to finish studying .bat usefulness to my only window 7-netbook.

if i learned enough .bat usage then i will use unix apps so that i can learn their difference.

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

Re: two findstr questions

#22 Post by Squashman » 06 Jan 2014 18:52

Not sure if all the versions of grep for windows require the extra dll files.

berserker
Posts: 95
Joined: 18 Dec 2013 00:51

Re: two findstr questions

#23 Post by berserker » 06 Jan 2014 19:17

Squashman wrote:Not sure if all the versions of grep for windows require the extra dll files.

gnu win32 grep (practically most of them ) has installers so just one click install does the job.

berserker
Posts: 95
Joined: 18 Dec 2013 00:51

Re: two findstr questions

#24 Post by berserker » 06 Jan 2014 19:27

bars143 wrote:im more interested in the likeness of wget which is portable, standalone and easy.

grep requires other dll.
awk is more like php.

its better for me to finish studying .bat usefulness to my only window 7-netbook.

if i learned enough .bat usage then i will use unix apps so that i can learn their difference.

awk is nothing like php except both allow for basic programming. PHP is more than that.
some of the tools require shared libraries , eg if you use grep's pcre (perl regex) then probably it may use pcre library. but dll are just files. if one can download tools, it doesn't hurt hard disk space to download another file.

another way to put it. take all the little batch modules dostips created and combine them into a dll. then you can use it too in your batch. that's the concept.

Sponge Belly
Posts: 200
Joined: 01 Oct 2012 13:32
Location: Ireland
Contact:

Re: two findstr questions

#25 Post by Sponge Belly » 20 Jan 2014 18:42

Hi Again! :-)

Thanks to everyone who answered. And apologies for not replying sooner. I was having a little break… ;-)

Anyways, I plundered some code from one of the links Squashman suggested and came up with this snippet that finds the longest line in a file:

Code: Select all

@echo off & setlocal enableextensions enabledelayedexpansion
set fs=%~z1

set "dummy=%tmp%\dummy.txt"
<nul set /p "=#" >"%dummy%"
start "" /b /w cmd /q /c for /l %%i in (^) do (^
type "%dummy%" ^>^>"%dummy%" ^& for %%f in ("%dummy%") do ^
if %%~zf geq %fs% exit 0^)

set /a lc=longlen=0,prevoffset=-1
for /f "delims=:" %%a in ('
fc /b "%~1" "%dummy%" ^| findstr /ec:": 0A 23"
') do (set /a lc+=1
set /a len=0x%%a - prevoffset - 2
if !len! gtr !longlen! set /a longlen=len,longline=lc
set /a prevoffset=0x%%a)
echo("%~1": line %longline% is %longlen% bytes long

:end
del "%dummy%"
endlocal & goto :EOF


Note that the filename on the command line must have CR-LF line endings for every line.

I didn’t develop the program any further because it slows to a crawl for large files. Maybe I could save the offset of every line to a file and then sort the file, but I’d have to pad the offsets first. I like to use pure Batch solutions as a rule, but I may have to resort to a hybrid script in this case.

- SB

Sponge Belly
Posts: 200
Joined: 01 Oct 2012 13:32
Location: Ireland
Contact:

find longest line in text file

#26 Post by Sponge Belly » 21 Nov 2015 08:55

Hello Again! :)

I returned to this problem after a long break with a fresh perspective. Sure enough, the only practical solution, imho, is a hybrid script:

Code: Select all

@if (@X==@Y) @then
@echo off & setlocal enableextensions disabledelayedexpansion

for /f "tokens=1,2" %%A in ('
cscript //nologo //e:jscript "%~f0" "%~dpnx1"
') do set /a num=%%A,len=%%B
echo(line %num% of file "%~dpnx1" is %len% chars long

endlocal & exit /b 0

@end

// http://rosettacode.org/wiki/Longest_string_challenge

var fso = new ActiveXObject("Scripting.FileSystemObject");
var file = fso.OpenTextFile(WScript.Arguments(0).replace(/\\/g,"\\\\"),1,false);
var current=lc=previous=0, longStr='';

while (!file.AtEndOfStream) {
    lc++;
    current = file.ReadLine().length;
    if (current > previous) {
        longStr = lc + ' ' + current;
        previous = current;
    }
}
WScript.Echo(longStr);

file.Close();
WScript.Quit(0);


The program is very fast, even on large files. It’s also line-ending agnostic. However, if two or more lines have the longest length, only the first line number is returned.

Thanks again to everyone who contributed to this topic.

TTFN!

- SB

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

Re: two findstr questions

#27 Post by Aacini » 21 Nov 2015 11:39

Although is probable that the pure Batch file below run slower than your hybrid one, it returns all the longest lines:

Code: Select all

@echo off
setlocal EnableDelayedExpansion

set /A ofs=0, max=0
for /F "tokens=1,2 delims=:" %%a in ('findstr /N /O "^" %1') do (
   set /A len=%%b-ofs, ofs=%%b
   if !len! geq !max! (
      set /A line=%%a-1
      if !len! gtr !max! (
         set "lines=Longest lines (length !len!):"
         set "max=!len!"
      )
      set "lines=!lines! !line!"
   )
)
echo %lines%

This method makes good use of /O findstr switch, that returns the offset in bytes of the beginning of each line. Of course, this method have the same limitations than findstr command.

Antonio

Sponge Belly
Posts: 200
Joined: 01 Oct 2012 13:32
Location: Ireland
Contact:

Re: two findstr questions

#28 Post by Sponge Belly » 22 Nov 2015 10:47

Hi Aacini! :)

I explored a findstr /no solution, but decided it was too messy. There’s no way to tell Unix and Windows line-endings apart, line numbers must be decremented, and what if the last line in the file happens to be the longest? :?

Cheers!

- SB

Post Reply