0

From coreutils' manual about pr:

‘-e[in-tabchar[in-tabwidth]]’
‘--expand-tabs[=in-tabchar[in-tabwidth]]’

Expand tabs to spaces on input. Optional argument in-tabchar is the input tab character (default is the TAB character). Second optional argument in- tabwidth is the input tab character’s width (default is 8).

Does -e[in-tabchar[in-tabwidth]] mean to replace each in-tabchar with in-tabwidth number of whitespaces?

In the following two examples, why are a, b and c separated by one whitespace, while 1 and 3 by three whitespaces in the first example, and two whitespaces in the second example? Thanks.

$ printf "a:b:c\n1::3\n" | pr -t -e:2
a b c
1   3

$ printf "a:b:c\n1::3\n" | pr -t -e:1
a b c
1  3
3

pr -e does the same job as the expand command. It expands tabs in a similar way as your terminal emulator typically would. TAB is a control character which terminals understand as moving the cursor to the next tab-stop, it is meant for tabular formatting and alignment.

On most terminals, tab stops by default are every 8 columns. So sending them a TAB will have the same effect as sending a number of spaces ranging from 1 to 8 depending on the current cursor position:

 $ printf '%b\n' 'a\tb' 'abcde\tg'
 a       b
 abcde   g

The first tab moved the cursor 7 columns to the right, the second 3 columns to the right.

pr -e would replace those \t with as many spaces as necessary to have the same effect:

$ printf 'abcde\tg\n' | sed l
abcde\tg$
abcde   g
$ printf 'abcde\tg\n' | pr -te | sed l
abcde   g$
abcde   g

Note that GNU pr suffers from the same limitation as GNU expand in that it assumes all characters are made of one byte and take one column to display.

|improve this answer|||||
  • For more on which, see superuser.com/a/711019/38062 . – JdeBP Jul 30 '18 at 13:32
  • Thanks. "On most terminals, tab stops by default are every 8 columns. So sending them a TAB will have the same effect as sending a number of spaces ranging from 1 to 8 depending on the current cursor position". Could you explain more about how the number of spaces "depends" on the current cursor position? I can't figure it out based on your first example. – Tim Jul 30 '18 at 14:11
  • Thanks. (1) Does printf '%b' always interpret \t as tabular formatting and alignment instead of field separator? (2) In general (not just pr and printf), I always thought \t as field separator, and never realize they are used for column/field alignment. How could I know when to think of \t as field separator and when as field alignment? – Tim Jul 30 '18 at 16:10
  • @Tim printf '%b' '\t' or printf '\t' writes a TAB character to its stdout. If that's written to a tty device with a terminal at the other end, that terminal will receive the TAB character and move its cursor to the next tab stop (start of the next table cell if you want). – Stéphane Chazelas Jul 30 '18 at 16:13
  • Thanks. I wonder why printf "a:b:c\n1::3\n" | pr -t -e:1 print the same first line printf "a:b:c\n1::3\n" | pr -t -e:2, considering they are not supposed to align the fields by the same number of characters. – Tim Jul 30 '18 at 16:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.