Visit Our UNIX and Linux User Community


Display 3 variable side by side


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Display 3 variable side by side
# 1  
Old 09-19-2013
Display 3 variable side by side

Hi. i have 3 variable:

echo $a will give:
Code:
file1
more file
file4

echo $b will give:
Code:
date1
date 2
date 3

echo $c will give:
Code:
user1  
user2
sam jackson

how do i make then display side by side:
Code:
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

Last edited by pikamon; 09-20-2013 at 04:28 AM.. Reason: Added code tags
# 2  
Old 09-19-2013
Code:
#!/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.

Last edited by MDominok; 09-19-2013 at 06:34 AM.. Reason: Forgot 5th segment.
This User Gave Thanks to MDominok For This Post:
# 3  
Old 09-19-2013
Another way...
Code:
ar=($a); br=($b); cr=($c)
for((i=0;i<5;i++)); 
do   
  printf "${ar[i]} ${br[i]} ${cr[i]}\n"
done

--ahamed
This User Gave Thanks to ahamed101 For This Post:
# 4  
Old 09-19-2013
Hi,
Also, you can try:
Code:
paste -d' ' <(echo $a| tr ' ' '\n') <(echo $b|tr ' ' '\n') <(echo $c|tr ' ' '\n')

Regards.
This User Gave Thanks to disedorgue For This Post:
# 5  
Old 09-20-2013
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?

Last edited by pikamon; 09-20-2013 at 03:34 AM..
# 6  
Old 09-20-2013
Quote:
Originally Posted by pikamon
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).

Code:
#!/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.

Last edited by MDominok; 09-20-2013 at 04:17 AM.. Reason: Added p.s.
# 7  
Old 09-20-2013
You should not edit the original request, create a new post.

Add all info to one variable
Code:
d="$a
$b
$c"

Gives a new variable $d
Code:
echo "$d"
file1
more file
file4
date1
date 2
date 3
user1
user2
sam jackson

Code:
awk '{a[++t]=$0} END {for (i=1;i<=3;i++) print a[i],a[i+3],a[i+6]}' <<<"$d"
file1 date1 user1
more file date 2 user2
file4 date 3 sam jackson


Previous Thread | Next Thread
Test Your Knowledge in Computers #314
Difficulty: Easy
Pointers were not used in the original C programming language and were added later in in C++.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to run 2 python scripts at the same time side by side on the same line?

Could I run 2 python scripts at the same time side by side output on the same line in this same format but with scripts? from itertools import izip_longest with open("file1") as textfile1, open("file2") as textfile2: for x, y in izip_longest(textfile1, textfile2, fillvalue=""): x =... (4 Replies)
Discussion started by: bigvito19
4 Replies

2. Shell Programming and Scripting

Output variable side by side

how do you display variable that contain multi line side by side dynamically without knowing the number of variable and line for each variable ? Example echo $a a b c echo $b 1 2 3 echo $c q w e how to output the result in (3 Replies)
Discussion started by: pikamon
3 Replies

3. Shell Programming and Scripting

printing 3 files side by side based on similar values in rows

Hi I'm trying to compare 3 or more files based on similar values and outputting them into 3 columns. For example: file1 ABC DEF GHI file2 DEF DER file3 ABC DER The output should come out like this file1 file2 file3 ABC ABC (4 Replies)
Discussion started by: zerofire123
4 Replies

4. Shell Programming and Scripting

Paste two file side by side together based on specific pattern match problem

Input file_1: P78811 P40108 O17861 Q6NTW1 P40986 Q6PBK1 P38264 Q6PBK1 Q9CZ49 Q1GZI0 Input file_2: (6 Replies)
Discussion started by: patrick87
6 Replies

5. Web Development

Cannot access Apache web server from Wan side, only Lan side.

I have installed WAMPSERVER 2.0 on my windows vista x64 system but still am having issues with getting the webserver to be seen outside my local network. It is working fine within my local network. Been through several setup tutorials so far, no dice still. For testing purposes I have... (1 Reply)
Discussion started by: davidmanvell
1 Replies

6. UNIX for Dummies Questions & Answers

display whats in an array side by side

ok, so ive got some values that are inputed by a user and are stored in an array. Now what i want to do is change this piece of code below to show ina different way. } for (( num=0; num<9; num++ )) do echo ${arr} done } which outputs the array contents each on a seperate line. I want... (3 Replies)
Discussion started by: strasner
3 Replies

7. Shell Programming and Scripting

How to Merge / combine / join / paste 2 text files side-by-side

I have 2 text files, both have one simple, single column. The 2 files might be the same length, or might not, and if not, it's unknown which one would be longer. For this example, file1 is longer: ---file1 Joe Bob Mary Sally Fred Elmer David ---file2 Tomato House Car... (3 Replies)
Discussion started by: cajunfries
3 Replies

8. Shell Programming and Scripting

Script to place selected columns from a group of files side by side in a new file

Hi Everyone, I need a shell/perl script to bring selected columns from all the files located in a directory and place them in a new file side by side. File1: a b c d 2 3 4 5 f g h i .......... File2: I II III IV w x y z .............. and so on many files are there...... (8 Replies)
Discussion started by: ks_reddy
8 Replies

9. Shell Programming and Scripting

How to print two sql query outputs side by side in excel

Hi, I have to sql queries like select sno,sname from temptable; select deptno,dname from depttable; In excel i want to specify the column number to which my output should be displayed. please help me in this... thanks in advance... (6 Replies)
Discussion started by: prasee
6 Replies

10. UNIX for Dummies Questions & Answers

Having a Unix system installed side to side with Windows?

Atm i have Windows 98. Can i install a Unix system like Redhat on my computer, and on boot choose which one to go to? Like i can with Win95 & 98... Please help me. I dont know alot about unix so im still learning. I just know its better than Win for the internet and it doesnt crash. :) (14 Replies)
Discussion started by: Pcslider
14 Replies

Featured Tech Videos