tcl equivalent of NR in awk 
Author Message
 tcl equivalent of NR in awk

Hi
I'm new to this. And I have convert an awk script to tcl script. I have
to read some arbitrary lines of a particular file and print it to another
file. The reading has to be done by picking some specific lines here and
there, and back and forth, and reading off some fields in the line into
another file. awk does this by using the NR command. I wanted to
find out if there is an equivalent of NR in tcl, instead of using many
'if' loops, which may take a long cpu time.
Also if there's no such equivalent, what is the best way to do this.
Please help

Bye Sriram

*******************************************************************************

Georgia Institute of Technology,         Phone: Office (404) 894-0318
Atlanta Georgia, 30332                          Resi   (404) 872-1294
*******************************************************************************



Wed, 07 May 2003 03:00:00 GMT  
 tcl equivalent of NR in awk

Quote:

> Hi
> I'm new to this. And I have convert an awk script to tcl script. I have
> to read some arbitrary lines of a particular file and print it to another
> file. The reading has to be done by picking some specific lines here and
> there, and back and forth, and reading off some fields in the line into
> another file. awk does this by using the NR command. I wanted to
> find out if there is an equivalent of NR in tcl, instead of using many
> 'if' loops, which may take a long cpu time.
> Also if there's no such equivalent, what is the best way to do this.
> Please help

Since the awk NR is the record number, I assume that you're trying to
get specific lines from a file by their line number.  The "Tcl way" to
do that, for small files that can be read entirely into memory, is to
read the data in one fell swoop, then split it into a list, like this:

  set filename "myfile.dat"
  set fp [open $filename "r"]
  set data [split [read $fp [file size $filename]] "\n"]
  close $fp

Then the variable 'data' contains a list of lines.  You can get at a
specific line by using the list index function:

  set p [lindex $data 122]
  puts "line 122:  '$p'"

If the original data file is very large, then you're stuck reading each
line in a loop.  If you're planning on matching lines, then look at the
regular expression man pages.  This is much more powerful than awk could
ever hope to be.

Bob
--

Mayo Foundation                                           (507) 284-2702
Rochester MN, 55905 USA   http://www.mayo.edu/sppdg/sppdg_home_page.html



Fri, 09 May 2003 03:00:00 GMT  
 tcl equivalent of NR in awk


Quote:
>I'm new to this. And I have convert an awk script to tcl script. I have
>to read some arbitrary lines of a particular file and print it to another
>file. The reading has to be done by picking some specific lines here and
>there, and back and forth, and reading off some fields in the line into
>another file. awk does this by using the NR command. I wanted to
>find out if there is an equivalent of NR in tcl, instead of using many
>'if' loops, which may take a long cpu time.
>Also if there's no such equivalent, what is the best way to do this.

Its been a while since I fiddled with AWK; NR is the number of lines in
the file?  If so and your data file is not too big (a condition which
depends very much on the size of computer you're using) then you are
probably best off loading the whole file into Tcl at once and processing
it in memory - [for] loops are *way* cheaper than system calls.

  set f [open $filename r]
  set lines [split [read $f [file size $filename]] "\n"]
  close $f
  set NR [llength $lines];       # Make things easy for yourself!  :^)

If the amount of data you are dealing with is "large" then you'll need a
more creative solution.  But that's a big topic.

Donal.
--
Donal K. Fellows (at home)
--
FOOLED you!  Absorb EGO SHATTERING impulse rays, polyester poltroon!!
      (WARNING: There is precisely one error in this message.)



Fri, 09 May 2003 03:00:00 GMT  
 tcl equivalent of NR in awk


Quote:
>If the original data file is very large, then you're stuck reading each
>line in a loop.

You can also do some tricks with [seek] provided you've got a real file.

Quote:
>If you're planning on matching lines, then look at the
>regular expression man pages.  This is much more powerful than awk could
>ever hope to be.

I thought AWK could do regexp stuff (though its RE language is not as
rich as that of recent versions of Tcl.)  The real advantage of a
language like Tcl over AWK comes when you throw in multiple files (or
extensions.  :^)

Donal.
--
Donal K. Fellows (at home)
--
FOOLED you!  Absorb EGO SHATTERING impulse rays, polyester poltroon!!
      (WARNING: There is precisely one error in this message.)



Fri, 09 May 2003 03:00:00 GMT  
 tcl equivalent of NR in awk

In case anyone is interested, I started a
<URL: http://purl.org/thecliff/tcl/wiki/> page for the topic of awk like
solutions in Tcl - I hope people will add the idioms that they have
found make Tcl more powerful than awk.
See http://mini.net/cgi-bin/wikit/1063.html for details.
--
"See, he's not just anyone ... he's my son."  Mark Schultz

Even if explicitly stated to the contrary, nothing in this posting
should be construed as representing my employer's opinions.



Sat, 10 May 2003 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. awk-equivalent to e.g. cut -f5-

2. Seeking equivalent if EOF in awk

3. awk equivalent for casting char to int?

4. Tcl equivalent for Perl's Win32::OLE, LWP::UserAgent, HTTP::Request, Net::Telnet

5. TIP #203: Create tclConfig.sh-Equivalent in Tcl

6. TCL equivalent of PHP "phpinfo()"?

7. TCL equivalent of PHP's ORD() function

8. tcl equivalent of system() in C

9. tcl equivalent pack/unpack?

10. TCL equivalent of #include ?

11. htons(0) equivalent in Tcl

12. equivalent of #define in Tcl?

 

 
Powered by phpBB® Forum Software