Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Convert tabs to spaces in tcl??

Posted on 2008-11-11
8
Medium Priority
?
1,251 Views
Last Modified: 2013-12-21
Using tcl, I'm reading through a file generated by a report-writer. There are some records that have tabs in them, some don't. I need to parse out the fields (columns in the report) positionally. The problem is using string range or crange views the \t in the records as single characters

I tried doing
regsub -all $rec \t "     " rec
but since the amount of whitespace in a tab is not static, I was getting undesired results.

Example of report line:
10/27/08                   801062347       801062347                .00    Charge                  70.99       N
Corresponding example of octal display from the report file:
0001460   \r  \n   1   0   /   2   7   /   0   8                  \t  \t
0001500                8   0   1   0   6   2   3   4   7  \t
0001520    8   0   1   0   6   2   3   4   7  \t  \t                   .
0001540    0   0                   C   h   a   r   g   e  \t  \t
0001560        7   0   .   9   9                               N  \r  \n

I'd prefer to handle this in the tcl script but if necessary, I suppose I can manipulate the file using ksh beforehand, although I'm not sure how I would do that either.

Any ideas?
0
Comment
Question by:sjmont
6 Comments
 
LVL 24

Expert Comment

by:fridom
ID: 22939148
Well  this would be a very big suprise. The tab-width does not change. At least this would be the first time I heard of it. As you can see however there are quite a few \t in there. I'd probably go another route just split the line into pieces limited by whitespace. Or you can use another regular expression
which matches the given text better

Regards
Friedrich
0
 
LVL 2

Expert Comment

by:leh327
ID: 23315587
Can this be used:
% set conv_rec [lrang $rec 0 6]
10/27/08 801062347 801062347 .00 Charge 70.99 N
0
 

Author Comment

by:sjmont
ID: 23316435
Yes, that's a valid thought. I guess I should have mentioned that the reason I was parsing the data positionally was because there sometimes is a field missing from the record, for example:

------------------------------------------------------------------------------------------------------------------------------------
  DATE      TRANS #       CUSTOMER     RESPONSIBLE PARTY      BALANCE     PMT/CHG      TYPE     AMOUNT       POSTED
------------------------------------------------------------------------------------------------------------------------------------

11/01/08                   801000349       801000349                .00    Charge                  63.00       N

11/01/08    00566317       801000886       801000886                .00    Charge                  11.25       N


So in such a case using a list would offset the data in the list elements
0
New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

 
LVL 2

Expert Comment

by:leh327
ID: 23316960
Does defining start and end of each field, then use "string range" work for you? For example:
set dt_start 0
set dt_end 7
set trans_start 8
set trans_end 19
...
...
set dt [string range $rec $dt_start $dt_end]
set trans_nu [string range $rec $trans_start $trans_end]
...
...
0
 
LVL 2

Accepted Solution

by:
leh327 earned 2000 total points
ID: 23317778

Since each field has its own length, replacing each "\t" with a constant number of space is giving you incorrect result, right?

Do you already know how many spaces to replace "\t" for each field?

od -t a yourfile.txt
should have shown how many spaces should be used to replace one "\t" for each field.

For example:
When trans_nu comes back missing or doesn't match some rule, a positional replacing "\t\t" with "    \t" (four spaces)

set conv_rec [join "{[regsub -all \t\t [string range $rec $trans_start $trans_end] "    \t"]} {[string range $rec $cus_start end]}" ""]


0
 
LVL 6

Expert Comment

by:bummerlord
ID: 25983545
If I understand your quest correctly, you want to replace tabs with spaces while keeping the same "positioning"  as the tabs produce in a terminal?
The reason for this being that you intend to match the position of the headers with the data fields, or something like that? I think that is also what has been suggested above kind of?

The attached proceedure could perhaps be of some assistance with replacing tabs with space at least. Probably very slow, but might be good enough for small amounts of data, or you can just use the idea, and write a better performing version :-)
It simply walks through the original string one char at a time, and if there is a tab at the position it will be substituted with enough spaces to pad up to the next tab-stop (column width defined by the tabstop variable).
proc tab2space { str } { 
        set tabstop 8 
        set n 0
        set nspc 0 
        set strlen [string length $str]
        while { $n < $strlen } {
                set c [string index $str $n]
                if { "$c" == "\t" } {
                        set nn [expr $n + $nspc]
                        set spaces [expr $tabstop - ( $nn % $tabstop )]
                        incr nspc [expr $spaces - 1]
                        append newstr "[string repeat " " $spaces]"
                } else {
                        append newstr $c
                }
                incr n
        }
        return "$newstr"
}

Open in new window

0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
Suggested Courses
Course of the Month21 days, 6 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question