log file lines numbers - can they be created/inserted

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

log file lines numbers - can they be created/inserted

Ben Hoen
Hello listers:

I often present output to the team of researchers working on the project I
have, and have found the log files to be the most useful to present results
in these preliminary stages (before any final output is needed to be
produced) Plots are exported via a separate bit of code.  

It would be extremely helpful to have line numbers display in a log file so
we all could quickly move to an output somewhere in a long file.  Is there a
way to do that?  Alternatively is there a way to have the output show the
line number of the .do file from which it is produced?

Any help would be greatly appreciated.  Thanks,

Ben


Ben Hoen
LBNL
[hidden email]



*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
Reply | Threaded
Open this post in threaded view
|

Re: log file lines numbers - can they be created/inserted

Maarten buis
--- On Thu, 18/11/10, Ben Hoen wrote:
> I often present output to the team of researchers working
> on the project I have, and have found the log files to be
> the most useful to present results in these preliminary
> stages (before any final output is needed to be produced)
> Plots are exported via a separate bit of code. 
>
> It would be extremely helpful to have line numbers display
> in a log file so we all could quickly move to an output
> somewhere in a long file.  Is there a way to do that? 

Below is a rough first version of a command -log_ln- that
add line numbers to a log file.

*-------------------- begin example ----------------------
program drop _all

// program begins
*! version 1.0.0 MLB 18Nov2010
program log_ln
    version 11.1
    syntax using/, save(string) [replace append]

    confirm file `"`using'"'
    if "`replace'`append'" == "" {
        confirm new file `"`save'"'
    }

    tempname fh_r fh_w
    local linenum = 0
    file open `fh_r' using `"`using'"', read
    file open `fh_w' using `"`save'"', write `replace' `append'
    file read `fh_r' line

    while r(eof)==0 {
        local linenum = `linenum' + 1
        file write `fh_w' %4.0f (`linenum') `"   `macval(line)'"' _n
        file read `fh_r' line
    }
    file close `fh_r'
    file close `fh_w'
end
// program ends

// example begins
cd c:\temp

log using foo, replace

sysuse auto, clear
reg price mpg

log close

log_ln using "c:\temp\foo.smcl", save("c:\temp\foo2.smcl") replace
// example ends

// view results from example
view "c:\temp\foo2.smcl"
type "c:\temp\foo2.smcl"
*----------------------------- end example ----------------------------

Hope this helps,
Maarten

--------------------------
Maarten L. Buis
Institut fuer Soziologie
Universitaet Tuebingen
Wilhelmstrasse 36
72074 Tuebingen
Germany

http://www.maartenbuis.nl
--------------------------


     

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
Reply | Threaded
Open this post in threaded view
|

Re: log file lines numbers - can they be created/inserted

Sergiy Radyakin
In reply to this post by Ben Hoen
On Thu, Nov 18, 2010 at 11:31 AM, Ben Hoen <[hidden email]> wrote:

> Hello listers:
>
> I often present output to the team of researchers working on the project I
> have, and have found the log files to be the most useful to present results
> in these preliminary stages (before any final output is needed to be
> produced) Plots are exported via a separate bit of code.
>
> It would be extremely helpful to have line numbers display in a log file so
> we all could quickly move to an output somewhere in a long file.  Is there a
> way to do that?  Alternatively is there a way to have the output show the
> line number of the .do file from which it is produced?
>
> Any help would be greatly appreciated.  Thanks,
>
> Ben

Dear Ben,

this code will print the numbered lines on the screen and create a new
file with line numbers:

program define inslnums

        syntax using , [save(string) replace]
        local mask = "0000"
       
        tempfile newfile
        tempname fh1

        if (`"`save'"'!="") {
                tempname fh2
                file open `fh2' using `"`save'"', write text `replace'
        }

        file open `fh1' `using', read text
        file read `fh1' oneline

        local linenum=0

        while r(eof)==0 {
                local linenum=`linenum'+1
                local flinenum =
substr("`mask'",1,length("`mask'")-length("`linenum'"))+"`linenum'"
                display as input `"`flinenum'"' as text `" `oneline'"'

                if (`"`save'"'!="") {
                        file write `fh2' `"`flinenum' `oneline'"' _n
                }
                file read `fh1' oneline
        }
        file close `fh1'
        file close `fh2'
end

Also see help for -file read-, where there is an example showing the
numbered lines on the screen.

Adjust the mask to regulate the line numbers width (4 digits, etc) for
your files.

Here is an example how to call the program:

inslnums using test.log, save("linenums.txt") replace

Best, Sergiy Radyakin


>
>
> Ben Hoen
> LBNL
> [hidden email]
>
>
>
> *
> *   For searches and help try:
> *   http://www.stata.com/help.cgi?search
> *   http://www.stata.com/support/statalist/faq
> *   http://www.ats.ucla.edu/stat/stata/
>

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
Reply | Threaded
Open this post in threaded view
|

RE: log file lines numbers - can they be created/inserted

Nick Cox
In reply to this post by Ben Hoen
Interesting question. Absent any option for this in -log- that I can see, an evident alternative is to do it after the fact.

In my own favourite text editor, there is an easy way to show line numbers, and probably a way to add line numbers, and what one smart text editor can do, another can.

Alternatively, this quick and dirty utility passed precisely one test:

*! 0.0.1 NJC 18 Nov 2010
program lineno
        version 11.0
        syntax anything(name=file) , saving(str) [ replace ]

        if "`replace'" == "" {
                confirm new file `"`saving'"'
        }  

        tempname hi ho
        file open `hi' using `"`file'"', r
        file open `ho' using `"`saving'"', w `replace'
        file read `hi' line
        local i = 1
       
        while r(eof) == 0 {
                file write `ho' "`i' " `"`macval(line)'"' _n
                file read `hi' line
                local ++i
        }
       
        file close `ho'
end

Perhaps 11.0 can be decreased; meaning, I didn't test this in Stata 10 or lower.

The code points up one tricky point: in just copying lines, you must be careful not to inhibit macro substitution.

Nick
[hidden email]

Ben Hoen

I often present output to the team of researchers working on the project I
have, and have found the log files to be the most useful to present results
in these preliminary stages (before any final output is needed to be
produced) Plots are exported via a separate bit of code.  

It would be extremely helpful to have line numbers display in a log file so
we all could quickly move to an output somewhere in a long file.  Is there a
way to do that?  Alternatively is there a way to have the output show the
line number of the .do file from which it is produced?

Any help would be greatly appreciated.  Thanks,


*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
Reply | Threaded
Open this post in threaded view
|

RE: RE: log file lines numbers - can they be created/inserted

Nick Cox
Nefarious negative needs negation: You must be careful to inhibit macro substitution.

Nick
[hidden email]

Nick Cox

Interesting question. Absent any option for this in -log- that I can see, an evident alternative is to do it after the fact.

In my own favourite text editor, there is an easy way to show line numbers, and probably a way to add line numbers, and what one smart text editor can do, another can.

Alternatively, this quick and dirty utility passed precisely one test:

*! 0.0.1 NJC 18 Nov 2010
program lineno
        version 11.0
        syntax anything(name=file) , saving(str) [ replace ]

        if "`replace'" == "" {
                confirm new file `"`saving'"'
        }  

        tempname hi ho
        file open `hi' using `"`file'"', r
        file open `ho' using `"`saving'"', w `replace'
        file read `hi' line
        local i = 1
       
        while r(eof) == 0 {
                file write `ho' "`i' " `"`macval(line)'"' _n
                file read `hi' line
                local ++i
        }
       
        file close `ho'
end

Perhaps 11.0 can be decreased; meaning, I didn't test this in Stata 10 or lower.

The code points up one tricky point: in just copying lines, you must be careful not to inhibit macro substitution.

Nick
[hidden email]


*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
Reply | Threaded
Open this post in threaded view
|

RE: log file lines numbers - can they be created/inserted

Ben Hoen
In reply to this post by Ben Hoen
Thanks Guys!

Nick, what, pray-tell, is the "smart" text editor you use?  (Mine - Notepad
- is evidently as dumb as a doornail.)

Ben

=============
Nefarious negative needs negation: You must be careful to inhibit macro
substitution.

Nick
[hidden email]

Nick Cox

Interesting question. Absent any option for this in -log- that I can see, an
evident alternative is to do it after the fact.

In my own favourite text editor, there is an easy way to show line numbers,
and probably a way to add line numbers, and what one smart text editor can
do, another can.

Alternatively, this quick and dirty utility passed precisely one test:

*! 0.0.1 NJC 18 Nov 2010
program lineno
        version 11.0
        syntax anything(name=file) , saving(str) [ replace ]

        if "`replace'" == "" {
                confirm new file `"`saving'"'
        }  

        tempname hi ho
        file open `hi' using `"`file'"', r
        file open `ho' using `"`saving'"', w `replace'
        file read `hi' line
        local i = 1
       
        while r(eof) == 0 {
                file write `ho' "`i' " `"`macval(line)'"' _n
                file read `hi' line
                local ++i
        }
       
        file close `ho'
end

Perhaps 11.0 can be decreased; meaning, I didn't test this in Stata 10 or
lower.

The code points up one tricky point: in just copying lines, you must be
careful not to inhibit macro substitution.

Nick
[hidden email]

Ben Hoen
LBNL
Office: 845-758-1896
Cell: 718-812-7589
[hidden email]



*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
Reply | Threaded
Open this post in threaded view
|

RE: log file lines numbers - can they be created/inserted

Nick Cox
Vim is my favourite.

Nick
[hidden email]

Ben Hoen

Thanks Guys!

Nick, what, pray-tell, is the "smart" text editor you use?  (Mine - Notepad
- is evidently as dumb as a doornail.)

Nick Cox

Interesting question. Absent any option for this in -log- that I can see, an
evident alternative is to do it after the fact.

In my own favourite text editor, there is an easy way to show line numbers,
and probably a way to add line numbers, and what one smart text editor can
do, another can.

Alternatively, this quick and dirty utility passed precisely one test:

*! 0.0.1 NJC 18 Nov 2010
program lineno
        version 11.0
        syntax anything(name=file) , saving(str) [ replace ]

        if "`replace'" == "" {
                confirm new file `"`saving'"'
        }  

        tempname hi ho
        file open `hi' using `"`file'"', r
        file open `ho' using `"`saving'"', w `replace'
        file read `hi' line
        local i = 1
       
        while r(eof) == 0 {
                file write `ho' "`i' " `"`macval(line)'"' _n
                file read `hi' line
                local ++i
        }
       
        file close `ho'
end

Perhaps 11.0 can be decreased; meaning, I didn't test this in Stata 10 or
lower.

The code points up one tricky point: in just copying lines, you must be
careful to inhibit macro substitution.


*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/