0

I had a file with contents as below, with three columns which needed to be formatted/aligned:

ABCD  XYZAB 1234
PQRSTUV   STU  9113
LMN        OPRQM   8966

I came up with the following shell script:

while read -r col1 col2 col3;
do printf "%s%-80s%-80s\n" "$col1" "$col2" "$col3";
done <spaced-define.txt > tabbed-define.txt

and managed to get them aligned like this, with spaces in between:

ABCD     <SPACES>  XYZAB  <SPACES>  1234
PQRSTUV  <SPACES>  STU    <SPACES>  9113
LMN      <SPACES>  OPRQM  <SPACES>  8966

What I am unable to figure out is how to achieve the same alignment using tabs instead of spaces ?

ABCD    <TABS>  XYZAB  <TABS>  1234
PQRSTUV <TABS>  STU    <TABS>  9113
LMN     <TABS>  OPRQM  <TABS>  8966
4

1 回答 1

2

If you pipe your output to col it will reduce sequences of spaces to TABs when appropriate.

while read -r col1 col2 col3;
do printf "%-20s%-80s%-80s\n" "$col1" "$col2" "$col3";
done <spaced-define.txt | col > tabbed-define.txt
于 2013-03-29T21:24:09.190 回答