TransWikia.com

Merge arguments with a file side by side

Unix & Linux Asked on December 15, 2020

This is the prefix saved in a file

wolf@linux:~$ cat prefix.txt 
A. 
B. 
C. 
wolf@linux:~$ 

I would like to merge this with another input (in verfical format)

This is the sample input AB_CD_EF, however it’s in a horizontal format.

So I used tr to change it to vertical format.

wolf@linux:~$ echo AB_CD_EF | tr _ 'n'
AB
CD
EF
wolf@linux:~$ 

How do I merge these 2 output to produce:

A. AB
B. CD
C. EF

I’ve been thinking to use paste, but it doesn’t work with stdin.

wolf@linux:~$ paste $(echo AB_CD_EF | tr _ 'n') prefix.txt
paste: AB: No such file or directory
wolf@linux:~$ 

This data is not in sequence, I’m not looking for solution such as echo {A..C}

If there’s better way to do this, please let me know.

One Answer

paste does work with standard input, but you need to tell it where (with a - argument), and provide input on standard input:

echo AB_CD_EF | tr _ 'n' | paste -d  prefix.txt -

In your attempt, the text you wish to paste is provided as arguments, not on standard input; paste expects file names there.

Correct answer by Stephen Kitt on December 15, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP