LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-07-2010, 10:11 PM   #1
casperdaghost
Member
 
Registered: Aug 2009
Posts: 349

Rep: Reputation: 16
bash echo variables


lets just say i have two files foo and bar
cat foo
5 5 5 5 5 5

and cat bar
4 4 4 4 4 4

when i assign the file contents to a variable
i=$(cat foo)
j=$(cat bar)
echo $i , $j
what i get is :
5 5 5 5 5 5 4 4 4 4 4 4

the output i am looking for echoing $i and $j together is

5 4
5 4
5 4
5 4
5 4
5 4

how can i get the 5 4 output?

i have some scripts where commands results get fed into variables - and the output is related to each other and needs to get printed into columns next to each other.

Last edited by casperdaghost; 03-08-2010 at 12:04 AM.
 
Old 03-07-2010, 10:30 PM   #2
William Shotts
LQ Newbie
 
Registered: Mar 2010
Posts: 1

Rep: Reputation: 0
Try:

paste foo bar
 
Old 03-07-2010, 10:38 PM   #3
casperdaghost
Member
 
Registered: Aug 2009
Posts: 349

Original Poster
Rep: Reputation: 16
paste works - on the file names. however this is just an example - i have commands that feed a column of values into a variable - i want to print those two columns along each side of each other. the output of both the variables is related to each other.

I want to output:

first value of $i (next to) first value of $j
second value of $i (next to) first value of $j
third value of $i (next to) third value of $j
ect.

Last edited by casperdaghost; 03-07-2010 at 10:41 PM.
 
Old 03-07-2010, 11:38 PM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
As long as the words in the input data are of identical length as described, this loop construct can be used to load an array
Code:
i=0
while read
do
    array[$i]=$REPLY
    let i=i+1
done <<< "$(echo "5 5 5 5 5" | fmt --width=1)"
The echo "5 5 5 5 5" can, of course, be replaced by cat input.txt.
 
Old 03-07-2010, 11:48 PM   #5
casperdaghost
Member
 
Registered: Aug 2009
Posts: 349

Original Poster
Rep: Reputation: 16
i guess that is kind of what I am looking for, something that will read the first line of the two variables , line by line then print the output of the first line of $i next to the first line of $j, print a newline then read and print the second lines of each variable, print a newline then read and prints the thirds line of each variable - giving me something in columns like this. i guess i need a loop or an array.

4 5 (value of first line of $i , $j)
4 5 (value of second line of $i , $j)
4 5 (value of third line of $i , $j)
4 5 (value of fourth line of $i , $j)
ect.

Last edited by casperdaghost; 03-07-2010 at 11:59 PM.
 
Old 03-08-2010, 12:23 AM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Quote:
Originally Posted by casperdaghost View Post
i guess i need a loop or an array.
Both. Definitely a loop for the output. Maybe something like this (not tested)
Code:
read -a array1 < input1.txt
read -a array2 < input2.txt
for ((i=0;i<${array1[#]};i++))
do
    echo ${array1[$i]} ${array2[$i]} 
done
 
Old 03-08-2010, 04:28 AM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Hi, catkin! Just a little debugging to the not-tested code: the length of the array is ${#array1[@]} or ${#array[*]}. Bye.
 
1 members found this post helpful.
Old 03-08-2010, 04:33 AM   #8
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Quote:
Originally Posted by colucix View Post
Hi, catkin! Just a little debugging to the not-tested code: the length of the array is ${#array1[@]} or ${#array[*]}. Bye.
Hello colucix! Thanks for the correction.
 
Old 03-10-2010, 02:13 PM   #9
casperdaghost
Member
 
Registered: Aug 2009
Posts: 349

Original Poster
Rep: Reputation: 16
Thanks catkin and colucix - I always appreciate your assistance - the scope of the assignments that I have been getting is all pointing toward arrays and hashes. I really need to focus on mastering them.

What I should have mentioned at first is that this is just an example for a problem, not the problem itself - I have two commands which load their output into variables - these variables are bond prices with decimals - some of the prices go out to 1/8th of a cent - what I have been doing in running the scripts and pasting the output into a spreadsheet. I can combine the commands to run concurrently - but when I echo out the output, it prints out in one long column.
I have to figure out a way to print out the different command results into columns - because running the commands separately and cutting and pasting the columns into a spreadsheet, they copying the whole spreadsheet into a text file is getting TEDIOUS.

I am going to start out putting my commands into something like this:

1 read -a array1 < input1.txt
2 read -a array2 < input2.txt
3 for ((i=0;i<${array1[#]};i++))
4 do
5 echo ${array1[$i]} ${array2[$i]}
6 done

colucix - which line should I add your changes to?

Thanks again guys.

I have two commands that load their output into variables
command1 > $foo
command2 > $bar
cat foo
5 5 5 5 5 5

and cat bar
4 4 4 4 4 4

when i assign the file contents to a variable
i=$(cat foo)
j=$(cat bar)
echo $i , $j
what i get is :
5 5 5 5 5 5 4 4 4 4 4 4

the output we are looking for echoing $i and $j together is

5 4
5 4
5 4
5 4
5 4
5 4
 
Old 03-10-2010, 05:21 PM   #10
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Quote:
Originally Posted by casperdaghost View Post
Thanks catkin and colucix - I always appreciate your assistance - the scope of the assignments that I have been getting is all pointing toward arrays and hashes. I really need to focus on mastering them.

I am going to start out putting my commands into something like this:

1 read -a array1 < input1.txt
2 read -a array2 < input2.txt
3 for ((i=0;i<${array1[#]};i++))
4 do
5 echo ${array1[$i]} ${array2[$i]}
6 done

colucix - which line should I add your changes to?
the expression ${#array1[@]} gives the length of the array "array1". Now you can try to figure out where the length of the array is needed in the line of codes above!

Quote:
I have two commands that load their output into variables
command1 > $foo
command2 > $bar
cat foo
5 5 5 5 5 5
This is not stated clearly. Actually each of the two variables "foo" and "bar" contains a string, which is interpreted as a filename. They should have been previously assigned using something like:
Code:
foo=foo
bar=bar
Then the shell performs variable expansion and substitute $foo and $bar with their respective values. The resulting commands are:
Code:
command1 > foo
command2 > bar
and these are actually executed by the shell. In other words, the commands do not "load their output into variables", but the output of each command is redirected to a file. Is this correct?

Putting this all together and following the catkin's idea, you can avoid to create the two files "foo" and "bar". Just assign the output of the two commands (using command substitution) to the arrays:
Code:
array1=($(command1))
array2=($(command2))
then you can print the elements of the two arrays at your pleasure (for example using the suggested loop)
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash Script: parse active process stderr, strip, dump into variables, use variables TimeFade Programming 1 02-13-2010 07:09 AM
echo variables into config file. Karas Linux - Newbie 1 11-27-2009 12:15 PM
BASH: instad of echo-ing, I just want to assing to a bash variable... how?? rylan76 Linux - Newbie 9 11-28-2008 09:46 AM
(bash) echo "#!/bin/bash" event not found - trying to generate profiles automatically jimieee Programming 9 05-03-2006 11:24 AM
How to echo variables and commands in one line? hindll01 Programming 1 09-10-2004 07:02 AM


All times are GMT -5. The time now is 12:07 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration