1

var $abc contains:

abc  jkl
def  mno
ghi  pqr

var $def contains:

stu
vwx
yz

Expected output:

abc  jkl  stu
def  mno  vwx
ghi  pqr  yz

heemayl's solution:

I tried paste <(echo "$abc") <(echo "$def") but it is giving output as below

ASFSFGFGGRRFDFFFFFH     33566
AHSHDFFBORDASHFYEHFYUCH 33568
FASFSSFHJUYRT   33371
FASIFIDFGGGDDDDD        33364
AFDDDGGGGGDER   33371
FDGGGGHJJK      16225
AISJFKDJFKDDKFJKDJFF    33568
KDFJKDJFKDJFKDFJK       33567

How to align the second column correctly?

Solution:

paste <(echo "$abc") <(echo "$def") | column -t
Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
Koshur
  • 1,359
  • 1
    Just a general note: Don't store the contents of files in variables. It is very seldom needed, and it's more error-prone to work with than just reading directly from the output of a command or from a file. – Kusalananda Apr 19 '17 at 10:37
  • @Kusalananda So if I would need say only first column from a file, i usually do awk '{print $1}' and save it in a variable, and then use it further. How else can we do it if not store it in a variable? – Koshur Feb 14 '18 at 10:19
  • 1
    See the question that your question is marked as a duplicate of. Using paste <( command_used_for_abc ) <( command_for_def ) would solve it for you. command_for_abc might, for example be awk '{ print $1 }' somefilename. – Kusalananda Feb 14 '18 at 10:23

1 Answers1

1

Using paste, with help from process substitution to get two file descriptors for paste to operate on:

paste <(echo "$abc") <(echo "$def")

Be careful with the quoting of the variables.

Example:

$ echo "$abc"
abc jkl
def mno
ghi pqr

$ echo "$def"
stu
vwx
yz

$ paste <(echo "$abc") <(echo "$def")
abc jkl stu
def mno vwx
ghi pqr yz
heemayl
  • 56,300
  • It works! However I need to format the output to display the second column correctly. I have updated the question with an example. How do we fix this? – Koshur Apr 19 '17 at 03:14
  • 1
    @Koshur Tack column: paste <(echo "$abc") <(echo "$def") | column -t – heemayl Apr 19 '17 at 03:16
  • Is there a way to redirect this output to a CSV or Excel file? – Koshur Apr 20 '17 at 17:22
  • 1
    @Koshur Use a CSV then: paste ... | sed 's/[[:blank:]]\+/,/g' (assuming no native comma in any field) – heemayl Apr 21 '17 at 02:39