View unanswered posts | View active topics It is currently 19 Dec 2014 01:54



Post new topic Reply to topic  [ 19 posts ]  Go to page 1, 2  Next
Replace Type with AWK 
Author Message

Joined: 02 Aug 2010 05:25
Posts: 169
Post Replace Type with AWK
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


23 Sep 2010 00:45
Profile
Expert

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


23 Sep 2010 01:26
Profile

Joined: 02 Aug 2010 05:25
Posts: 169
Post Re: Replace Type with AWK
Superb!

TY

Regards
Dario


23 Sep 2010 03:53
Profile

Joined: 12 May 2006 01:13
Posts: 319
Post Re: Replace Type with AWK
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}"


23 Sep 2010 04:17
Profile
Expert

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


23 Sep 2010 06:07
Profile

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


23 Sep 2010 18:37
Profile
Expert

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


24 Sep 2010 01:29
Profile

Joined: 12 May 2006 01:13
Posts: 319
Post Re: Replace Type with AWK
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.


24 Sep 2010 03:41
Profile
Expert

Joined: 04 Jun 2010 20:05
Posts: 177
Location: Russia
Post Re: Replace Type with AWK
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%


24 Sep 2010 06:32
Profile

Joined: 12 May 2006 01:13
Posts: 319
Post Re: Replace Type with AWK
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% ?


24 Sep 2010 07:27
Profile
Expert

Joined: 04 Jun 2010 20:05
Posts: 177
Location: Russia
Post Re: Replace Type with AWK
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


24 Sep 2010 08:33
Profile

Joined: 12 May 2006 01:13
Posts: 319
Post Re: Replace Type with AWK
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.


24 Sep 2010 09:17
Profile
Expert

Joined: 04 Jun 2010 20:05
Posts: 177
Location: Russia
Post Re: Replace Type with AWK
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.


24 Sep 2010 09:31
Profile

Joined: 02 Aug 2010 05:25
Posts: 169
Post Re: Replace Type with AWK
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


10 Apr 2012 06:07
Profile

Joined: 10 Feb 2012 02:20
Posts: 4501
Post Re: Replace Type with AWK
You can use a 'change' utility of some kind and change it to another character.

Is that good enough?


10 Apr 2012 06:29
Profile
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 19 posts ]  Go to page 1, 2  Next


Who is online

Users browsing this forum: Bing [Bot], Google [Bot], ShadowThief, 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 © 2000, 2002, 2005, 2007 phpBB Group.
Forum style by Vjacheslav Trushkin for Free Forums/DivisionCore.