Technical article 104298

How do I convert tabs to spaces in a text file using SCO UNIX/XENIX/OpenServer?

OpenServer 6.0.0OpenServer 5.0.0OpenServer 5.0.2OpenServer 5.0.4OpenServer 5.0.5OpenServer 5.0.6UnixWare 7.0.0UnixWare 7.0.1UnixWare 7.1.0UnixWare 7.1.1UnixWare 7.1.2UnixWare 7.1.3UnixWare 7.1.4Open UNIX 8

Written · Last revised

Problem

I want to convert all of the tabs in my text file to spaces. Reasons for this include improper indentation when printing, a discrepancy between the operating environment and the file tab size, files included in e-mail, and so on.

Cause

If you insert text in vi(C) while the 'ai' (autoindent) is set, then embedded tab characters may be put in the file. It is also possible that these tab characters were explicitly entered by you or some application program.

NOTE: Many UNIX/XENIX utilities that process files are
line-oriented, meaning that the lines are <NL>-terminated.
Also, most assume that the the operating environment default
is for tabs to be equivalent to 8 characters.  If the file you
are converting had a different assumption, then the conversion
program will need an additional option.  When applicable, the
examples in this document are given for the default tab size
of 8, and for a tab size of 4.

Solution

You can convert embedded tabs to spaces either I) at the shell
command line or II) from within the vi editor.  There are
separate recommended solutions for A) SCO OpenServer Release 5.0,
B) SCO UNIX (prior to release 5.0) and C) SCO XENIX.  Additionally,
the appendix contains a sample shell script front-end for converting
tabs to spaces.
I. Command-line utilities:
--------------------------
A. SCO OpenServer Release 5.0

The expand(C) filter can be used to expand tabs to spaces as follows:

tabsize=8
---------
cat filename.old | expand > filename.new
or the more effecient...
<filename.old  expand  >filename.new
tabsize=4
---------
<filename.old  expand -t4  >filename.new

(where filename.old is the name of the file containing tabs and filename.new is the name of the converted file)

NOTE: The expand utility is not included in SCO Operating Systems prior to OpenServer Release 5.0.

B. SCO UNIX, Open Desktop, Open Server (before release 5.0)

The col(C) filter can be used to expand tabs to spaces as follows:

tabsize=8
---------
cat filename.old | col -x > filename.new
or the more effecient...
<filename.old  col -x  >filename.new
tabsize=4
---------
pr -te4 filename.old > filename.new

(where filename.old is the name of the file containing tabs and filename.new is the name of the converted file)

NOTE: The col utility is not included with SCO XENIX. It is included with the SCO XENIX Text Processing package.

C. SCO XENIX

The pr(C) filter can be used to expand tabs to spaces as follows:

tabsize=8
---------
pr -te filename.old > filename.new
tabsize=4
---------
pr -te4 filename.old > filename.new

(where filename.old is the name of the file containing tabs and filename.new is the name of the converted file)

NOTE: The pr utility is included with SCO UNIX, but it is
      better to use col with UNIX.  The UNIX pr utility
      expands the first tab found on the first line of each page
      by one too many spaces.  This problem can be alleviated by
      using the flag "-l10000" ("10000 lines per page").
II. Within 'vi' editor:
-----------------------
     You can also convert tabs to spaces when editing the text
     file from within the vi editor.  First, you may want to
     see whether there are any tabs embedded in the file.
     Enter the following sequence:
<ESC> 1 G /<TAB> <ENTER>

If you get a message such as "Pattern not found" or "Address search hit BOTTOM without matching pattern", that means that there are no <TAB> characters embedded within the file.

Otherwise, vi did find tab character(s) in the file.  So
next, to actually convert the tabs to spaces, bring up the
'ex' command line by typing colon (:).

Then at the ex command line ':' prompt, type:

A. SCO OpenServer Release 5.0
tabsize=8             tabsize=4
---------             ------------
%!expand              %!expand -t4
B. SCO UNIX, Open Desktop, Open Server
tabsize=8
---------
%!col -x
C. SCO XENIX
tabsize=8             tabsize=4
---------             ---------
%!pr -te              %!pr -te4
NOTE:  If after the above command the file appears corrupted,
       simply press "u" to undo the last operation.

See also

man pages:  col(C), pr(C), vi(C), umask(C), login(M),
umask(S), expand(C)
Appendix
========
          This shell script will convert a file with tabs into one
          with its tabs replaced by spaces.  Save this to a file
          named "exp8" under your local bin directory, change mode
          to 0755, and then link the file to "exp4".
-- cut here --- cut here --- cut here --- cut here --- cut here--
:
# 'exp4/exp8: expand tab4/8 to spaces'
#      input: filename with embedded tabs
#     output: filename with tabs converted
#     syntax: exp8 <filename>
#             exp4 <filename>
#

trap "echo 'no change'; exit 0" 1 2 3 15 trap "rm -f /tmp/exp$$" 0

if [ $# -ne 1  -o  ! -w "$1" ]; then
    echo "usage: $0 filename"
    exit 1
fi
FULLNAME=$1
if [ "`basename $0`" = exp4 ]; then
    SPACE=4
else
    SPACE=8
fi
pr -t -e$SPACE $FULLNAME > /tmp/exp$$  &&
  cp /tmp/exp$$ $FULLNAME

-- cut here --- cut here --- cut here --- cut here --- cut here--

NOTE:     This command will also process a filename with spaces within the
          filename:
eg, here's a sample file_list:

data/cyrus/imap/user/falkirk_hiredrive/Sent Items/core data/cyrus/imap/user/fiat_fleet/Sent Items/core data/cyrus/imap/user/fpaton/Sent Items/core data/cyrus/imap/user/gblack/Memo Scott Willis/core data/cyrus/imap/user/yrae/Sent Items/core data/cyrus/imap/user/gizat/Sent Items/core data/cyrus/imap/user/gjohnston/Peugeot, PFS &- Branch/core data/cyrus/imap/user/glasgow_daewoo/AC Memos/core data/cyrus/imap/user/glasgow_hyundai/AC Memos/core data/cyrus/imap/user/glasgow_hyundai/Sent Items/core data/cyrus/imap/user/glasgow_hyundai/Martin Geary/core data/cyrus/imap/user/glasgow_hyundai/Sandy Biggart/core data/cyrus/imap/user/jbertram/from franchise/core data/cyrus/imap/user/grangemouth_renault/Sent Items/core data/cyrus/imap/user/citroendev/Sent Items/core data/cyrus/imap/user/gtraining/Sales Academy/core data/cyrus/imap/user/gtraining/Management Academy/core data/cyrus/imap/user/hhamilton/Junk E-mail/core data/cyrus/imap/user/inverness_citroen/Sent Items/core data/cyrus/imap/user/janderson/Sent Items/core data/cyrus/imap/user/prebooking/Sent Items/core data/cyrus/imap/user/jclarke/Sent Items/core data/cyrus/imap/user/lhenderson/Sent Items/core

and to remove them:
# ( IFS=$'\n' ; rm $(cat file_list ) )

From the Xinuos technical article database, record 104298. Reproduced with permission. The original is at www.sco.com/ta/104298, which is reachable but blocked from search engines.

Next step

Need this fixed rather than explained?

Tell us the release, what the system runs and what it is doing. We will reply by email with what we would do next.

  • You deal with the people who do the work
  • No obligation, and no upgrade required before we will help
  • We tell you plainly if something is not worth doing

Send an enquiry

We reply by email. If a server is down, say so in the first line. You can also write to help@scoexpert.com.