12

In Linux, I have the following problem with paste from (GNU coreutils) 8.13:

Trying to set another delimiter than the default (TAB) results in either just printing the first character of the defined delimiter or perfectly ignoring it.

Question: How does one define (multiple) delimiters when using paste?

Simply using, e.g. abc-123 as delimiter would be nice. With "multiple" I mean e.g. 2 TABS instead of one.


The patterns enclosing the delimiter(s) I've tried so far were

  • --delimiters="\delimiter"
  • --delimiters='\delimiter'
  • --delimiters=$"\delimiter"
  • --delimiters=$'\delimiter'

All with the same result: Only the first character is accepted or perfectly ignored. I've also tried the short version -d"\" and multiple instances &ndahs; nothing.

Also:

  • --delimiters="\\" → Error message

What works perfectly, though not what I want:

  • --delimiters="\n" → newline
  • --delimiters="\0" → nothing inbetween
  • --delimiters="\t"TAB, the default. Great.
Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
erch
  • 4,890
  • 17
  • 49
  • 81

3 Answers3

19

To have abc inbetween file1 and file2, you can do:

paste -d abc file1 /dev/null /dev/null file2

Or:

paste -d abc file1 - - file2 < /dev/null

If you want two tabs:

paste file1 /dev/null file2
Stéphane Chazelas
  • 522,931
  • 91
  • 1,010
  • 1,501
  • "*using `/dev/null` to create two columns in text processing*" works (besides: is this common knowledge I've missed so far and if not: where did you get this information from?) – erch Feb 18 '14 at 19:38
  • 1
    @Chirp.NotLuke. `/dev/null` acts like an empty file when reading, so you're pasting `file1`, and empty file, and empty file, file2 with separators being, in turn, `a`, `b` and `c`. Details on how `-d` works are in the `paste` man page. – Stéphane Chazelas Feb 18 '14 at 20:40
  • @StéphaneChazelas can you also add `pr -mts'abc' file1 file2` as alternative? – Sundeep Jan 04 '17 at 06:28
4

It's because you are giving it only two files to join, each character of delimiter string is used between each join of lines from each of the input files.

eg

file1 line1 DELIMCHAR0 file2 line1 DELIMECHAR1 file3 line1 ...
file1 line2 DELIMCHAR0 file2 line2 DELIMECHAR1 file3 line2 ...
...
X Tian
  • 10,413
  • 2
  • 33
  • 48
2

On BSD systems (not Linux to my knowledge), there is the lam (as in “laminate”) utility, which is faster and shorter than the above solution:

lam file1 -s delimiter file2
emm
  • 31
  • 1