Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


From   Sergiy Radyakin <[email protected]>
To   [email protected]
Subject   Re: st: log file lines numbers - can they be created/inserted
Date   Thu, 18 Nov 2010 12:50:25 -0500

On Thu, Nov 18, 2010 at 11:31 AM, Ben Hoen <[email protected]> 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
> [email protected]
>
>
>
> *
> *   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/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index