Page 2 of 2

Re: two findstr questions

Posted: 06 Jan 2014 01:44
by berserker
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.

Re: two findstr questions

Posted: 06 Jan 2014 06:59
by Squashman
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.

Re: two findstr questions

Posted: 06 Jan 2014 07:21
by foxidrive
I think bars was tongue in cheek...

Re: two findstr questions

Posted: 06 Jan 2014 10:05
by bars143
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.

Re: two findstr questions

Posted: 06 Jan 2014 13:55
by Squashman
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.

Re: two findstr questions

Posted: 06 Jan 2014 18:19
by bars143
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.

Re: two findstr questions

Posted: 06 Jan 2014 18:52
by Squashman
Not sure if all the versions of grep for windows require the extra dll files.

Re: two findstr questions

Posted: 06 Jan 2014 19:17
by berserker
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.

Re: two findstr questions

Posted: 06 Jan 2014 19:27
by berserker
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.

Re: two findstr questions

Posted: 20 Jan 2014 18:42
by Sponge Belly
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

find longest line in text file

Posted: 21 Nov 2015 08:55
by Sponge Belly
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

Re: two findstr questions

Posted: 21 Nov 2015 11:39
by Aacini
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

Re: two findstr questions

Posted: 22 Nov 2015 10:47
by Sponge Belly
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