Shell Script Error

Hi,

My file is
-----------

#cat html.txt
db1 10 10g
db2 20 20g
#

My script is
-------------

#cat script.sh
#!/bin/sh
echo "Details"
for i in `cat html.txt`
do
  c1=`echo "$i"|awk '{print $1}'`
  c2=`echo "$i"|awk '{print $2}'`
  c3=`echo "$i"|awk '{print $2}'`
  echo "$c1:$c2:$c3"
done
#

when i run script.sh script i am getting output like below
---------------------------------------------------------------

Details
db1::
10::
10g::
db2::
20::
20g::

I want the output like:
-----------------------

Details
db1:10:10g
db2:20:20g

I think the issue is
---------------------
for i in `cat html.txt` command returns only 'db1 ' to the variable i. But i need to return entire line to the variable i..
i.e
when i is 1 it should return ''db1 10 10g'
when i is 2 it should return 'db2 20 20g'

Can anybody help me on this?... What change i need to make in my script to get the above output?

tr ' ' ':' <  html.txt

vgersh99.. Thanks for your reply..

But i need to pass the value to the variables c1,c2,& c3..

Can u help me how to change in a script (i.e in for loop)?

 
#!/bin/ksh
exec 9</tmp/html.txt
while read -u9 dataline
do
  c1=`echo "$dataline"|awk -F' ' '{print $1}'`
  c2=`echo "$dataline"|awk -F' ' '{print $2}'`
  c3=`echo "$dataline"|awk -F' ' '{print $3}'`
  echo "|$c1|$c2|$c3|"
done
exec 9<&-

 
output
 
|db1|10|10g|
|db2|20|20g|
 
#cat script.sh
#!/bin/sh
echo "Details"
for i in `cat html.txt`
do
  c1=`echo "$i"|awk '{print $1}'`
  c2=`echo "$i"|awk '{print $2}'`
  c3=`echo "$i"|awk '{print $2}'`
  echo "$c1:$c2:$c3"
done
#

That's a dangerous use of backticks, incorrect use of backticks ( because you get splitting on spaces, not lines like you want), useless use of cat, and multiple useless uses of awk. You can do this all in one shell loop with no external commands instead of running awk 9000 individual times to process 3000 individual lines.

while read c1 c2 c3
do
        echo "${c1}:${c2}:${c3}"
done < html.txt

---------- Post updated at 01:41 PM ---------- Previous update was at 01:40 PM ----------

If you're using 'while read' anyway, why not let read do the splitting instead of awk?

Hi All,
Thanks for your timely help. Now both the scripts are working good.

$ cat script1.sh
#!/bin/sh
while read c1 c2 c3
do
        file1="${c1}-${c2}-${c3}"
for i in $file1
do
  c1=`echo "$i"|awk -F '-' '{print $1}'`
  c2=`echo "$i"|awk -F '-' '{print $2}'`
  c3=`echo "$i"|awk -F '-' '{print $3}'`
  echo "$c1:$c2:$c3"
done
done < html.txt

--------------------------------

$ cat script2.sh
#!/bin/sh
file='html.txt'
file1=`tr ' ' '-' < $file`
for i in $file1
do
  c1=`echo "$i"|awk -F '-' '{print $1}'`
  c2=`echo "$i"|awk -F '-' '{print $2}'`
  c3=`echo "$i"|awk -F '-' '{print $3}'`
  echo "$c1:$c2:$c3"
done