Page 1 of 2
Replace Type with AWK
Posted: 23 Sep 2010 00:45
by darioit
Hello,
it's possible to change "type" with AWK in this code
Code: Select all
FOR /F %%d IN ('type "%2"^|find "" /v /c') DO SET /a cnt_record=%%d
Regards
Dario
Re: Replace Type with AWK
Posted: 23 Sep 2010 01:26
by amel27
Code: Select all
awk "END{exit NR}" "%~2"
set cnt_record=%ERRORLEVEL%
Re: Replace Type with AWK
Posted: 23 Sep 2010 03:53
by darioit
Superb!
TY
Regards
Dario
Re: Replace Type with AWK
Posted: 23 Sep 2010 04:17
by ghostmachine4
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: Select all
for ..... ('awk .....' ) do (
set records=.....
)
3) Or you can do whatever you want inside awk itself, since its a mini programming language
Code: Select all
awk "END{ print \"total count is \"NR}"
Re: Replace Type with AWK
Posted: 23 Sep 2010 06:07
by amel27
ghostmachine4 wrote:what is the maximum number that %errorlevel% can take?
2'147'483'647 lines
Re: Replace Type with AWK
Posted: 23 Sep 2010 18:37
by ghostmachine4
thanks, but still, using errorlevel is still not recommended as number of lines in files are unpredictable.
Re: Replace Type with AWK
Posted: 24 Sep 2010 01:29
by amel27
not absolutely... file size with such quantity of lines > 4Gb (\r\n + text)
Re: Replace Type with AWK
Posted: 24 Sep 2010 03:41
by ghostmachine4
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.
Re: Replace Type with AWK
Posted: 24 Sep 2010 06:32
by amel27
3 chars + \r\n = 5 bytes, for 2^31 lines (2147483648)
5*(2^31) = 10'737'418'240 bytes ~ 10Gb:
Code: Select all
@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: Select all
@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%
Re: Replace Type with AWK
Posted: 24 Sep 2010 07:27
by ghostmachine4
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% ?
Re: Replace Type with AWK
Posted: 24 Sep 2010 08:33
by amel27
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
Re: Replace Type with AWK
Posted: 24 Sep 2010 09:17
by ghostmachine4
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.
Re: Replace Type with AWK
Posted: 24 Sep 2010 09:31
by amel27
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.
Re: Replace Type with AWK
Posted: 10 Apr 2012 06:07
by darioit
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
Re: Replace Type with AWK
Posted: 10 Apr 2012 06:29
by foxidrive
You can use a 'change' utility of some kind and change it to another character.
Is that good enough?