Tcl file READ command…

Clovertech Forums Read Only Archives Cloverleaf Cloverleaf Tcl file READ command…

  • Creator
    Topic
  • #50709
    Tom Rioux
    Participant

      I’m on 5.6 AIX….

      I haven’t had to use this command very much and I can’t seem to find it documented anywhere, but here is what I’m looking for.   I have a new line file and I want to ignore the first few header lines in the file.  Is there a way that I can specify to start reading on a certain line?

      Thanks….

      Tom Rioux

    Viewing 7 reply threads
    • Author
      Replies
      • #67190

        With tcl read I don’t think you can specify to read in a file from x to y. I think you can read a certain number of bites, but not specify where to start. Charlie will know if there’s a way to do this.

        When I’m working with files, I normally will slurp the entire file into a variable and then work with the file from there.

        An example:

        Code:

        set fileName [lindex $argv 0]
        # Usage: test.tcl file.txt

        set f [open $fileName r]
        fconfigure $f -translation binary
        set msg [read $f]
        close $f

        # You whole file is now in $msg

        foreach line [split $msg n] {
           # Skip blank lines
           # if {$line == “”} {continue}
        }

        I’m sure there are other ways, even “better” ways to this, but this has worked for me in the past.

        -- Max Drown (Infor)

      • #67191
        Jim Kosloskey
        Participant

          Tom,

          I opewn the file getting a file handle.

          Then I use a while loop checking for eof

                while ![eof $filehandle]{

          Inside the while loop I use gets to get each line. I also check for eof and break inside the while (wearing suspenders and a belt is really safe 😆 ).

          You could then keep a counter and as long as the counter is less than what you want continue inside the while. Once you are at the lines you want, start processing.

          The read command won’t do what you want as reads in the entire file or just so many characters.

          email: jim.kosloskey@jim-kosloskey.com 29+ years Cloverleaf, 59 years IT - old fart.

        • #67192

          Here’s an example from the web of what Jim is referring to:

          set f [open /home/keith/.profile]
          while {[gets $f line] >= 0} {

          [code]set f [open /home/keith/.profile]
          while {[gets $f line] >= 0} {

          -- Max Drown (Infor)

        • #67193
          Tom Rioux
          Participant

            Thanks guys for all your help.  The counter was the way to go.  Here is what got it to work for me:

                       set fd [open $msgdir/$filename r+]

                       set ctr 1

                       foreach line [split [read $fd] n] {

                         if [expr $ctr > 8] {

                           lappend contents $line

                         }

                         incr ctr

                       }

                       close $fd

            Tom

          • #67194
            Charlie Bursell
            Participant

              The simplest method to read a file is:

              set fd [open file]

              while {[gets $fd rec] >= 0} {

                   process the record

              }

              close $fd

              As for reading parts of a file refer to the seek and tell commands

               

              tell channelId  returns the current file offset

              seek channelId offset ?origin?”   moves to a specific offset

            • #67195
              Russ Ross
              Participant

                Another handy alerternative at the command line to say output starting at line 100 to the end of the file is to use the tail command at the UNIX prompt as follows:

                tail +100 myFile

                Russ Ross
                RussRoss318@gmail.com

              • #67196
                Keith McLeod
                Participant

                  Another command that I like when reading one line at a time is

                  for_file line {

                  do something with the line

                  }

                  Like other for loops you can throw in a counter if you wish….

                • #67197

                  Russ Ross wrote:

                  Another handy alerternative at the command line to say output starting at line 100 to the end of the file is to use the tail command at the UNIX prompt as follows:

                  tail +100 myFile

                  Good one!

                  in Tcl it would be something like set msg [exec tail +100 $fileName]

                  -- Max Drown (Infor)

              Viewing 7 reply threads
              • The forum ‘Cloverleaf’ is closed to new topics and replies.