tcl help – removing non-printable characters

Clovertech Forums Read Only Archives Cloverleaf Tcl Library tcl help – removing non-printable characters

  • Creator
    Topic
  • #55246
    Paul Bishop
    Participant

      This one has me stumped.  I have a process that runs using Korn shell that executes the following command:

      Code:

      tr -cd ‘11121540-176’ < in_file.txt > out_file.txt

      This will read in the complete file and output only the specified characters (octals 11, 12, 15, and 40 through 176 inclusive).  any ideas on how I can do this in TCL?

      Thanks!

      Paul Bishop
      Carle Foundation Hospital
      Urbana, IL

    Viewing 4 reply threads
    • Author
      Replies
      • #84696
        Charlie Bursell
        Participant

          It looks like you are trying to get rid of unprintable characters with the exception of the tab, line feed and carriage return.

          I prefer to deal in hex but something like this should work

          regsub -all — {[^trnw]} $str {} newstr

          It says any thing that is not a tab. CR, LF or ASCII character, delete

        • #84697
          Russ Ross
          Participant

            There is also a couple of TCL commands listed below

            Russ Ross
            RussRoss318@gmail.com

          • #84698
            Paul Bishop
            Participant

              Thanks Charlie and Russ.  

              I tried using exec with the tr command but tr uses standard input so I couldn’t get it to work.  I was able to get the regsub to work that Charlie posted with one exception – there were xFF characters scattered through the file which the regsub kept converting to xC3BF.  I got around that by string mapping xFF to null first, then doing the regsub.

              Thanks!

              Paul Bishop
              Carle Foundation Hospital
              Urbana, IL

            • #84699
              Charlie Bursell
              Participant

                Weird!  The regexp should exclude all but was was in the list.  I know /w does not include XFF

              • #84700
                David Barr
                Participant

                  Here’s how you can call “tr” from Tcl:

                  Code:

                  set str “abcx123”
                  set res [exec tr -d x << $str]
                  puts $res

                  I wouldn’t recommend it in this case. I’d probably use regsub instead.

              Viewing 4 reply threads
              • The forum ‘Tcl Library’ is closed to new topics and replies.