DosTips.com

A Forum all about DOS Batch
It is currently 24 Aug 2016 00:41

All times are UTC-06:00




Post new topic  Reply to topic  [ 19 posts ]  Go to page 1 2 Next
Author Message
 Post subject: Replace Type with AWK
PostPosted: 23 Sep 2010 00:45 
Offline

Joined: 02 Aug 2010 05:25
Posts: 184
Hello,

it's possible to change "type" with AWK in this code
Code:
FOR /F %%d IN ('type "%2"^|find "" /v /c') DO SET /a cnt_record=%%d


Regards
Dario


Top
   
PostPosted: 23 Sep 2010 01:26 
Offline
Expert

Joined: 04 Jun 2010 20:05
Posts: 177
Location: Russia
Code:
awk "END{exit NR}" "%~2"
set cnt_record=%ERRORLEVEL%


Top
   
PostPosted: 23 Sep 2010 03:53 
Offline

Joined: 02 Aug 2010 05:25
Posts: 184
Superb!

TY

Regards
Dario


Top
   
PostPosted: 23 Sep 2010 04:17 
Offline

Joined: 12 May 2006 01:13
Posts: 319
what is the maximum number that %errorlevel% can take? If it has a limit, its not advisable to use NR and errorlevel. Since NR can be very large, depending on how many lines you have in your file. A few better ways,
1) pipe the count to somewhere, then grab from there. eg a tmp file
2) use a for loop to get the return value
Code:
 for ..... ('awk .....' ) do (
  set records=.....
)


3) Or you can do whatever you want inside awk itself, since its a mini programming language
Code:
awk "END{ print \"total count is \"NR}"


Top
   
PostPosted: 23 Sep 2010 06:07 
Offline
Expert

Joined: 04 Jun 2010 20:05
Posts: 177
Location: Russia
ghostmachine4 wrote:
what is the maximum number that %errorlevel% can take?
2'147'483'647 lines


Top
   
PostPosted: 23 Sep 2010 18:37 
Offline

Joined: 12 May 2006 01:13
Posts: 319
thanks, but still, using errorlevel is still not recommended as number of lines in files are unpredictable.


Top
   
PostPosted: 24 Sep 2010 01:29 
Offline
Expert

Joined: 04 Jun 2010 20:05
Posts: 177
Location: Russia
not absolutely... file size with such quantity of lines > 4Gb (\r\n + text)


Top
   
PostPosted: 24 Sep 2010 03:41 
Offline

Joined: 12 May 2006 01:13
Posts: 319
amel27 wrote:
not absolutely... file size with such quantity of lines > 4Gb (\r\n + text)

A file with 3 characters per line for example, can have more than 2'147'483'647 lines and the size of file can still less than 200Mb.


Top
   
PostPosted: 24 Sep 2010 06:32 
Offline
Expert

Joined: 04 Jun 2010 20:05
Posts: 177
Location: Russia
3 chars + \r\n = 5 bytes, for 2^31 lines (2147483648)
5*(2^31) = 10'737'418'240 bytes ~ 10Gb:
Code:
@echo off
cls

set /a "l=2147483647"
<nul set /p x="lines    : "& echo %l%
<nul set /p x="bytes    : "& echo %l%*(2+3)
echo.---------------------------

set /a "k=%l%/1024*(2+3)"
<nul set /p x="kilobytes: "& echo %k%

set /a "m=(%k%/1024)"
<nul set /p x="megabytes: "& echo %m%

set /a "g=(%m%/1024)"
<nul set /p x="gigabytes: "& echo %g%

200Mb= 200*(2^20) bytes, if by 5 bytes/line:
lines = 200*(2^20)/5 = 41'943'040
Code:
@echo off
cls

set /a "m=200"
<nul set /p x="megabytes: "& echo %m%

set /a "k=%m%*1024"
<nul set /p x="kilobytes: "& echo %k%

set /a "b=%k%*1024"
<nul set /p x="bytes    : "& echo %b%
echo.--------------------
set /a "l=%b%/(2+3)"
<nul set /p x="lines    : "& echo %l%


Top
   
PostPosted: 24 Sep 2010 07:27 
Offline

Joined: 12 May 2006 01:13
Posts: 319
That's not what i mean. NR is the number of records total being processed. You said that errorlevel number can be up to 2147483648 correct.?
Now, say OP has a file that has 2147483649 lines. So what happens next to %errorlevel% ?


Top
   
PostPosted: 24 Sep 2010 08:33 
Offline
Expert

Joined: 04 Jun 2010 20:05
Posts: 177
Location: Russia
ghostmachine4 wrote:
file that has 2147483649 lines. So what happens next to %errorlevel% ?
errorlevel = -2147483647

most likely for this one "FIND /C" not work as well, since CMD arithmetic is 32-bit integer


Top
   
PostPosted: 24 Sep 2010 09:17 
Offline

Joined: 12 May 2006 01:13
Posts: 319
amel27 wrote:
most likely for this one "FIND /C" not work as well, since CMD arithmetic is 32-bit integer

that's why using errorlevel to capture NR is not recommended in case there are more lines errorlevel can handle.


Top
   
PostPosted: 24 Sep 2010 09:31 
Offline
Expert

Joined: 04 Jun 2010 20:05
Posts: 177
Location: Russia
ghostmachine4 wrote:
that's why using errorlevel to capture NR is not recommended
as well as all others CMD methods (not errorlevel) - SET /A, FIND, FOR, etc.


Top
   
PostPosted: 10 Apr 2012 06:07 
Offline

Joined: 02 Aug 2010 05:25
Posts: 184
hello,

during a batch I find a ctrl-z (Hex 1a) in a file, and gawk interpret it as eof.

There's some workaround to bypass this issue?

Regards
Dario


Top
   
PostPosted: 10 Apr 2012 06:29 
Offline
Expert

Joined: 10 Feb 2012 02:20
Posts: 5889
You can use a 'change' utility of some kind and change it to another character.

Is that good enough?


Top
   
Display posts from previous:  Sort by  
Post new topic  Reply to topic  [ 19 posts ]  Go to page 1 2 Next

All times are UTC-06:00


Who is online

Users browsing this forum: Yahoo [Bot] and 11 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® Forum Software © phpBB Limited