formatting output using echo


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting formatting output using echo
# 1  
Old 01-09-2009
formatting output using echo

i have the following script:

Code:
while [ $j -lt $outer ];do

b=${ARRAY2[j]}
i=0
while [ $i -lt $count ];do
a=${ARRAY1[k]}
fin[i]=`echo $a - $b | bc`
echo -e  "${fin[i]}," >> try
((i++))
((k++))
done
echo -n >> try
#add statement to format output here
#printf "\t" >> try
#echo -e '\t' >> nik
((j++))
echo $j
done

the file i get is this:
Code:
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
 
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
 
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,

i would like to get:
Code:
0,0,0
0,0,0
0,0,0
0,0,0
0,0,0
0,0,0
0,0,0
0,0,0
0,0,0
0,0,0
0,0,0
0,0,0
0,0,0

i tried playing with echo (commented out) but was not able to get the next set of zeroes to start on a new column..is there an easy way to do this without modifying the above script too much? whatever statement that is going to be added is shown by the comment.
thanks
# 2  
Old 01-09-2009
Quote:
Originally Posted by npatwardhan
i have the following script:

That is only a snippet of a script; it cannot be run as is.

Many variables are not initialized.
Quote:
Code:
while [ $j -lt $outer ];do


Neither $j nor $outer is initialized.
Quote:
Code:
b=${ARRAY2[j]}
i=0
while [ $i -lt $count ];do


$count is not initialized.
Quote:
Code:
a=${ARRAY1[k]}
fin[i]=`echo $a - $b | bc`


What are the values of $a and $b? Are they integers? If so, you don't need to call an external command.
Quote:
Code:
echo -e  "${fin[i]}," >> try


Is this what you want:

Code:
printf "%s," "${fin[i]}," >> try

Quote:
Code:
((i++))
((k++))


It's better to use the standard format for arithmetic:

Code:
i=$(( $i + 1 ))
k=$(( $k + 1 ))

Quote:
Code:
done
echo -n >> try
#add statement to format output here
#printf "\t" >> try
#echo -e '\t' >> nik
((j++))


See above.
# 3  
Old 01-11-2009
ok thanks for your help..
sorry for the confusion but i would just like to write my data into different columns instead of rows.
is there any way you can change the offset in the file so that it is on the next column instead of row? this way i can keep appending to the file with a new column.
# 4  
Old 01-11-2009
Quote:
Originally Posted by npatwardhan
ok thanks for your help..
sorry for the confusion but i would just like to write my data into different columns instead of rows.
is there any way you can change the offset in the file so that it is on the next column instead of row? this way i can keep appending to the file with a new column.

The file is written sequentially; you cannot insert new columns without rebuilding the entire file.

You could write to multiple files and paste them together:

Code:
for n in 1 2 3
do
  x=1
  while [ $x -le 4 ]
  do
    echo $n$x
    x=$(( $x + 1 ))
  done > "file$n"
done
paste file1 file2 file3 > file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Output formatting

Hi , can anyone help me in this Input file Name= XYZ Company= ALPHA City= Stockhomn Equipment=computer Country=Sweden Name=MNC Company=Beta City=Kaulampur Equipment=computer Country=Malaysia Name=JVC Company=gamma City=Kiruna (3 Replies)
Discussion started by: swets
3 Replies

2. Shell Programming and Scripting

Formatting echo o/p

Hi Team, Need a small help. I want to format my echo o/p. let say echo " helo helo helo helo helo helo helo helo helo helo helo helo helo helo helo helo helo helo helo " I want o/p to be displayed in below format helo helo helo helo helo helo helo helo helo helo helo helo... (6 Replies)
Discussion started by: gvkumar25
6 Replies

3. Shell Programming and Scripting

Formatting the output

Hi, I have a file which contents entries in this form. Only in /data4/temp abc.000001 Only in /data4/temp abc.000003 Only in /data4/temp abc.000012 Only in /data4/temp abc.000120 Only in /data4/temp abc.000133 Only in /data4/temp abc.001444 i want to read line by line and format... (2 Replies)
Discussion started by: arijitsaha
2 Replies

4. Shell Programming and Scripting

Formatting output

Hi, I have a file like this -------------------------- 1 aaa xxx 55 -------------------------- 1 aaa www 32 -------------------------- 2 bbb yyy 11 -------------------------- 2 bbb zzz 34 ------------------------- 2 bbb ttt ... (3 Replies)
Discussion started by: tdev457
3 Replies

5. Shell Programming and Scripting

formatting output

my script is as follows cnt=`ps -ef |grep pmon|grep -v grep|awk 'END {{print NR}}'` cnt2=`ps -ef |grep tns|grep -v grep|awk 'END {{print NR}}'` if then if then rman target/ catalog recdb/recdb@recdb cmdfile report_need_backup.sql > report_need_backup.txt ... (1 Reply)
Discussion started by: swkambli
1 Replies

6. Shell Programming and Scripting

Formatting ls output

I am using find and ls to search for "warez" files on my server. find /home/ -regex ".*\.\(avi\|mp3\|mpeg\|mpg\|iso\)" -print0 | xargs -0 ls -oh This command produces this: -rw-r--r-- 1 1000 3.2M Feb 18 2009 /home/user/public_html/lupus.mp3 I want to only get this 3.2M... (4 Replies)
Discussion started by: bonrad
4 Replies

7. Shell Programming and Scripting

more help with formatting ls output...

Ok, for a fun project, my goal is to replicate the style of "catalog" on an old apple ] *A 002 SOMEAPPLESOFTFILE B 004 SOMEFILE T 006 SOMETEXT I 002 SOMEINTEGERFILE The first character is either " " or "*" depending on if the file is locked or not. Next is the filetype, so in... (1 Reply)
Discussion started by: patrick99e99
1 Replies

8. Shell Programming and Scripting

formatting the output

Is it possible to convert the attached file to the format mentioned. Here for a particular job the table name and the corresponding instance name from one test run "X" is compared with the table name and the instance name from the second test run "Y" for output rows,affected rows,applied... (1 Reply)
Discussion started by: ragavhere
1 Replies

9. Shell Programming and Scripting

formatting output

Hi need some advice.. #grep -i hostname test.csv (gives the below output) HOSTNAME,name,host_test,,,,,,,, Now I need to format the above output as below. HOSTNAME: name=host_test Any easy way of doing this using awk or sed or printf? (4 Replies)
Discussion started by: balaji_prk
4 Replies

10. Shell Programming and Scripting

Formatting the output

Hi all, Have the following code(1) producing the results(2 & 3). Would like to know if there is a way to format the two reports created in a similar fashion. IE - The first is formatted nicely as a result of the echo "$xmpbdate $xavgs" >> $xmpbrpt However when I attempt to do the same on... (7 Replies)
Discussion started by: Cameron
7 Replies
Login or Register to Ask a Question