pikamon
September 19, 2013, 5:13am
1
Hi. i have 3 variable:
echo $a
will give:
file1
more file
file4
echo $b
will give:
date1
date 2
date 3
echo $c
will give:
user1
user2
sam jackson
how do i make then display side by side:
file1 date1 user1
more file date 2 user2
file4 date3 same jackson
previously the question is without spacing and some use space and delimiter to separate them. but now some of them have space in between them i have no idea which one have space and which one don't
all help are appreciated
#!/bin/bash
a="aa bb cc dd ee"
b="11 22 33 44 55"
c="AA BB CC DD EE"
for segment in 1 2 3 4 5; do
echo $a | cut -d' ' -f${segment} | tr -d "\n"
echo " " | tr -d "\n"
echo $b | cut -d' ' -f${segment} | tr -d "\n"
echo " " | tr -d "\n"
echo $c | cut -d' ' -f${segment}
done
But i'm sure there are more elegant solutions to this.
1 Like
Another way...
ar=($a); br=($b); cr=($c)
for((i=0;i<5;i++));
do
printf "${ar} ${br} ${cr}\n"
done
--ahamed
1 Like
Hi,
Also, you can try:
paste -d' ' <(echo $a| tr ' ' '\n') <(echo $b|tr ' ' '\n') <(echo $c|tr ' ' '\n')
Regards.
1 Like
pikamon
September 20, 2013, 2:35am
5
Thanks for all the reply
---------- Post updated at 02:35 PM ---------- Previous update was at 12:30 AM ----------
just changed the question a little. How do you go about doing it without using delimiter as space?
You can select the chars with cut byte- or character-wise:
From manpage:
-c list The list following -c specifies character
positions \(for instance, -c1-72 would pass
the first 72 characters of each line\).
#!/bin/bash
a="aabbccddee"
b="1122334455"
c="AABBCCDDEE"
for segment in 1 3 5 7 9; do
echo $a | cut -c${segment}-`expr ${segment} + 1` | tr -d "\n"
echo " " | tr -d "\n"
echo $b | cut -c${segment}-`expr ${segment} + 1` | tr -d "\n"
echo " " | tr -d "\n"
echo $c | cut -c${segment}-`expr ${segment} + 1`
done
Cheers
Michael
p.s.: Just noticed that you changed the original post totally. Not a good idea. Will confuse peeps reading the answers....
The above posted solution will not work with the current "original" post.
Jotne
September 20, 2013, 3:32am
7
You should not edit the original request, create a new post.
Add all info to one variable
d="$a
$b
$c"
Gives a new variable $d
echo "$d"
file1
more file
file4
date1
date 2
date 3
user1
user2
sam jackson
awk '{a[++t]=$0} END {for (i=1;i<=3;i++) print a,a[i+3],a[i+6]}' <<<"$d"
file1 date1 user1
more file date 2 user2
file4 date 3 sam jackson
Another example without delimiter but with position:
$ echo $a
aabbccddee
$ echo $b
1122334455
$ echo $c
AABBCCDDEE
$ paste -d' ' <(echo $a | sed 's/.\{2\}/&\n/g') <(echo $b | sed 's/.\{2\}/&\n/g') <(echo $c | sed 's/.\{2\}/&\n/g')
aa 11 AA
bb 22 BB
cc 33 CC
dd 44 DD
ee 55 EE
Regards.