2

Consider this usage of rs(1):

rs -n -z -c, <<CSV
a,b,c
dd,ee,ff
CSV

The above snippet emits:

a  b  c  dd  ee  ff

But I was expecting to see something like this:

a   b   c
dd  ee  ff

The manual suggests that the output array shape should match the input array shape unless configured otherwise.

rs -h using the same input emits 2 3, which suggests that rs does correctly identify the input array shape.

What am I misunderstanding in the usage of rs? Is there a way to have it automatically format the output with the same shape as the input?

shadowtalker
  • 1,328
  • 1
    It's an ugly hack, but doing a -T (pure transpose) then piping the result through a second rs -T seems to work for me – steeldriver Jan 31 '23 at 22:09
  • 1
    Not answering your rs question but column -s, -t <<CSV ... does what you want. – glenn jackman Feb 01 '23 at 03:00
  • @glennjackman I had problems with column when a leading cell is "blank", which is how I found rs in the first place! I made a follow-up question about it: https://unix.stackexchange.com/q/733969/73256 – shadowtalker Feb 01 '23 at 16:27
  • Actually, rs has the same problem as column in that example! It's a little surprising that these basic tools behave so weirdly. I'm tempted to write my own AWK script and ignore all these. – shadowtalker Feb 01 '23 at 16:27
  • rs is used to reshape data. It seems that you don't actually want to reshape the data, which in turn means rs might be the wrong tool for the job. – Kusalananda Feb 13 '23 at 12:10
  • @Kusalananda that's entirely possible! But it does seem like the docs and the implementation disagree with each other here. – shadowtalker Feb 14 '23 at 16:43
  • I'm not sure I understand where you see a disagreement. The only option that appears not to require specifying at at least one of rows and cols is the -T option, which transposes the input. All other options rely on the user specifying either rows or cols explicitly to reshape the data to that specification. – Kusalananda Feb 14 '23 at 17:32

1 Answers1

2

column does not behave as expected with leading empty fields. You might want this

awk 'BEGIN {FS = ","; OFS = "\t"} {$1 = $1} 1' <<DATA
a,b,c
,x,y
dd,ee,ff
DATA
a   b   c
    x   y
dd  ee  ff
glenn jackman
  • 85,964