Replace Type with AWK

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
darioit
Posts: 230
Joined: 02 Aug 2010 05:25

Replace Type with AWK

#1 Post by darioit » 23 Sep 2010 00:45

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

amel27
Expert
Posts: 177
Joined: 04 Jun 2010 20:05
Location: Russia

Re: Replace Type with AWK

#2 Post by amel27 » 23 Sep 2010 01:26

Code: Select all

awk "END{exit NR}" "%~2"
set cnt_record=%ERRORLEVEL%

darioit
Posts: 230
Joined: 02 Aug 2010 05:25

Re: Replace Type with AWK

#3 Post by darioit » 23 Sep 2010 03:53

Superb!

TY

Regards
Dario

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: Replace Type with AWK

#4 Post by ghostmachine4 » 23 Sep 2010 04:17

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}"

amel27
Expert
Posts: 177
Joined: 04 Jun 2010 20:05
Location: Russia

Re: Replace Type with AWK

#5 Post by amel27 » 23 Sep 2010 06:07

ghostmachine4 wrote:what is the maximum number that %errorlevel% can take?
2'147'483'647 lines

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: Replace Type with AWK

#6 Post by ghostmachine4 » 23 Sep 2010 18:37

thanks, but still, using errorlevel is still not recommended as number of lines in files are unpredictable.

amel27
Expert
Posts: 177
Joined: 04 Jun 2010 20:05
Location: Russia

Re: Replace Type with AWK

#7 Post by amel27 » 24 Sep 2010 01:29

not absolutely... file size with such quantity of lines > 4Gb (\r\n + text)

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: Replace Type with AWK

#8 Post by ghostmachine4 » 24 Sep 2010 03:41

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.

amel27
Expert
Posts: 177
Joined: 04 Jun 2010 20:05
Location: Russia

Re: Replace Type with AWK

#9 Post by amel27 » 24 Sep 2010 06:32

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%

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: Replace Type with AWK

#10 Post by ghostmachine4 » 24 Sep 2010 07:27

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% ?

amel27
Expert
Posts: 177
Joined: 04 Jun 2010 20:05
Location: Russia

Re: Replace Type with AWK

#11 Post by amel27 » 24 Sep 2010 08:33

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

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: Replace Type with AWK

#12 Post by ghostmachine4 » 24 Sep 2010 09:17

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.

amel27
Expert
Posts: 177
Joined: 04 Jun 2010 20:05
Location: Russia

Re: Replace Type with AWK

#13 Post by amel27 » 24 Sep 2010 09:31

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.

darioit
Posts: 230
Joined: 02 Aug 2010 05:25

Re: Replace Type with AWK

#14 Post by darioit » 10 Apr 2012 06:07

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

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

Re: Replace Type with AWK

#15 Post by foxidrive » 10 Apr 2012 06:29

You can use a 'change' utility of some kind and change it to another character.

Is that good enough?

Post Reply