Variable print problem


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Variable print problem
# 1  
Old 01-31-2012
Variable print problem

Hi,
I'm facing some problem with printing the values in the variables

I have passed all the database values into the variable, but when i echo the variable i get the values printed in one line
Code:
var=`echo <query> | database`
echo $var
or 
awk 'BEGIN {printf("%s\n","'"${var}"'")}'

this prints
Code:
table1 <value> table2 <value> table3 <value> table4 <value>

But the above needs to be printed as
Code:
table1 <value>
table2 <value>
table3 <value> and so on

Can anyone please help me

Moderator's Comments:
Mod Comment Please use next time code tags for your code and data
# 2  
Old 01-31-2012
It would help to see the output from
Code:
echo <query> | database

Your problem is that Shell Environment Variables are not files and the line break character is lost.

But. assuming that the database writes the output in pairs (with a line terminator), we need to read them as pairs:
Code:
echo <query> | database | while read table value
do
       echo "${table} ${value}"
done

# 3  
Old 01-31-2012
Thanks Methyl

The output i get from the command is

Code:
echo <query> | database

table1 <value>
table2 <value>
table3 <value>

but if i pass the values to the variable then i'm getting

Code:
table1 <value> table2 <value> table3 <value> table4 <value>


Last edited by methyl; 01-31-2012 at 02:09 PM.. Reason: please use code tags
# 4  
Old 01-31-2012
Try echo "$var". Without that, shell expansion is going to squeeze all the whitespace together.
# 5  
Old 01-31-2012
Try my solution from post #2 . Should work.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

To print value for a $variable inside a $variable or file

Hi guys, I have a file "abc.dat" in below format: FILE_PATH||||$F_PATH TABLE_LIST||||a|b|c SYST_NM||||${SRC_SYST} Now I am trying to read the above file and want to print the value for above dollar variables F_PATH and SRC_SYST. The problem is it's reading the dollar variables as... (5 Replies)
Discussion started by: abcabc1103
5 Replies

2. Shell Programming and Scripting

awk print variable then fields in variable

i have this variable: varT="1--2--3--5" i want to use awk to print field 3 from this variable. i dont want to do the "echo $varT". but here's my awk code: awk -v valA="$varT" "BEGIN {print valA}" this prints the entire line. i feel like i'm so close to getting what i want. i... (4 Replies)
Discussion started by: SkySmart
4 Replies

3. Shell Programming and Scripting

awk print using a variable

hey, just want to ask how to do this. ex. g="hi i am john" h=`echo $g | awk '{print $2}'` echo $h OUTPUT is 'i' What if I want to use a variable instead of using '2', how do I do that? Because this one does not work: a=2 h=`echo $g | awk '{print ${$a}}'` this one also does not... (3 Replies)
Discussion started by: h0ujun
3 Replies

4. Shell Programming and Scripting

Print variable on screen

Hi, I've stored the output of a command into a variable. The variable contains the following output: outputline1 outputline2 outputline3 ... How can I echo the variable so that the output is as follow and not one line: outputline1 outputline2 outputline2 ... Thanks a lot! (4 Replies)
Discussion started by: Sego
4 Replies

5. Shell Programming and Scripting

awk print variable

I have list of files: ls a.pdf b.pdf c.pdf and so on... and I have a file like this: cat file1 apple mango pear and so on... I want to rename my file like this: (7 Replies)
Discussion started by: zorrox
7 Replies

6. Shell Programming and Scripting

Print arguments with the help of variable

Let's say I want to print the arguments $4 till $#, how can I do this? $# contains the number of arguments $@ contain all the arguments as string What i need is something like for i in $4_till_$#; do #do something with $i convert $i ~/$output done The first 3 arguments are used as options... (6 Replies)
Discussion started by: hakermania
6 Replies

7. Shell Programming and Scripting

How to print a value in the variable using awk ?

:b:Hi All, I have a part of a script below: var1="value1" awk 'BEGIN {printf("%36s \n ","value1")}' Instead of directly giving the "value1" , I need to give using "var1" in the above awk statement. Is this possible? If so, what is the modified awk command? Thanks in advance JS (1 Reply)
Discussion started by: jisha
1 Replies

8. HP-UX

Print Problem in UNIX. Need to know the option to specify the print paper size

Hi, Could any one please let me know what is the option available in UNIX to print by specifying the paper size? We are using Unix11i. I could n't see any option specified in the 'lp' command to print the report by specifying the size of the paper. It would be of great help to me, if... (1 Reply)
Discussion started by: ukarthik
1 Replies

9. Shell Programming and Scripting

print the name of variable

How can I pass the name of the variable inside a for statment I have a script something like below: classA=12 classB=14 classC=16 # classD = 20 all_class="classA classB classC" for i in $all_class do echo $i = $($i) #<---- this one not work #echo $i = $(echo $i) ... (1 Reply)
Discussion started by: sun-guy
1 Replies

10. Shell Programming and Scripting

print variable in awk

i read the variable ph from file and i wanna to print it in awk. example ph=`cat tmpbatch` tail h.txt|grep "| |"|awk -F"|" '{ print "@unpdx.sql",$5 }'"$ph" i try this but it does not work (8 Replies)
Discussion started by: kazanoova2
8 Replies
Login or Register to Ask a Question