10

I have two variables which contain multiline information and i want to column them.

varA returns

Aug 01
Aug 04
Aug 16
Aug 26

and varB returns

04:25
07:28
03:39
10:06

if i print both variables, it returns

Aug01
Aug04
Aug16
Aug26
04:25
07:28
03:39
10:06

What i want to do is the following:

Aug01 04:25
Aug04 07:28
Aug16 03:39
Aug26 10:06

I'm new in Linux and i would appreciate some advice, thanks.

  • Please include the bash code you used in your attempt to print the two variable. It is so that we can see what you have done wrong in your attempt. – Bernard Wei Aug 17 '18 at 22:17
22

Meet paste, part of the preinstalled GNU core utilities:

$ paste <(printf %s "$varA") <(printf %s "$varB")
Aug 01  04:25
Aug 04  07:28
Aug 16  03:39
Aug 26  10:06

paste takes files and not variables as input, so I used bash Process Substitution and just printed the variable content with printf. The default delimiter between columns is TAB, you can change that with the -d option, e.g. paste -d" " for a single space character. To learn more about paste have a look at the online manual or run info '(coreutils) paste invocation'.

  • 1
    @BNairb If this answer solved your issue, please take a moment and accept it by clicking on the check mark to the left. That will mark the question as answered and is the way thanks are expressed on the Stack Exchange sites. – dessert Aug 21 '18 at 6:38
7

If you just want to simply display the text variables side by side, @dessert has the most simple (best?) solution using print. However if you want to be able to manipulate each piece individually, you could easily convert the vars to arrays instead, and loop through that.

#!/bin/bash

# declare the multi-line variables
var1="1
2
3
4"
var2="a
b
c
d"

# backup internal field separator to be safe
IFSave=$IFS

# set IFS to newline so vars will use newline to split into array
IFS=$'\n'

# split variables into array
foo=($var1)
bar=($var2)

#restore IFS to original value to be safe
IFS=$IFSave

# loop array foo, and cross reference key in array bar
for i in "${!foo[@]}"; do 
  printf "${foo[$i]} : ${bar[$i]}\n"
done

# you can allso now print single corresponding lines:
line=3

let id=$line-1 # arrays start at 0, so need to remove one

printf "\nPrinting line number $line\n"
printf "${foo[$id]} : ${bar[$id]}\n"
3

If you wanted to avoid external utilities and do it natively in the shell, you could use read with separate file descriptors / here strings for each variable:

while IFS= read -r -u3 a && read -r -u4 b; do 
  printf '%s\t%s\n' "$a" "$b"
done 3<<<"$varA" 4<<<"$varB"
Aug 01  04:25
Aug 04  07:28
Aug 16  03:39
Aug 26  10:06

Although it's often considered bad practice to use the shell for text processing, it might be excused in the case that you already have the data in shell variables.

3

You can do this with the POSIX tool pr:

varA='Aug 01
Aug 04
Aug 16
Aug 26'
varB='04:25
07:28
03:39
10:06'
pr -2 -t <<eof
$varA
$varB
eof

Result:

Aug 01                              04:25
Aug 04                              07:28
Aug 16                              03:39
Aug 26                              10:06

Or for single tab:

pr -2 -t -s

Or for single space:

pr -2 -t -s' '

http://pubs.opengroup.org/onlinepubs/9699919799/utilities/pr.html

Or with column from the util-linux package:

column -c 20 <<eof
$varA
$varB
eof

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.