# Shell Script Error

**URL:** <https://community.unix.com/t/shell-script-error/300312>\
**Category:** Shell Programming and Scripting\
**Created:** [December 1, 2011, 2:05pm UTC](https://community.unix.com/t/shell-script-error/300312 "2011-12-01T14:05:49Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![thomasraj87](https://community.unix.com/letter_avatar/thomasraj87/32/6_22734026db12803ebb3e059622ac3534.png) [@thomasraj87](https://community.unix.com/u/thomasraj87)\
**Post date:** [December 1, 2011, 2:05pm UTC](https://community.unix.com/t/shell-script-error/300312/1 "2011-12-01T14:05:49Z")

</div>

Hi,

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

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

```

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

```nohighlight
#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  
---------------------------------------------------------------

```nohighlight
Details
db1::
10::
10g::
db2::
20::
20g::

```

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

```nohighlight
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?

---

<div class="post-metadata">

**Author:** ![vgersh99](https://community.unix.com/user_avatar/community.unix.com/vgersh99/32/14851_2.png) [@vgersh99](https://community.unix.com/u/vgersh99)\
**Post date:** [December 1, 2011, 2:12pm UTC](https://community.unix.com/t/shell-script-error/300312/2 "2011-12-01T14:12:08Z")

</div>

```nohighlight
tr ' ' ':' < html.txt

```

---

<div class="post-metadata">

**Author:** ![thomasraj87](https://community.unix.com/letter_avatar/thomasraj87/32/6_22734026db12803ebb3e059622ac3534.png) [@thomasraj87](https://community.unix.com/u/thomasraj87)\
**Post date:** [December 1, 2011, 2:20pm UTC](https://community.unix.com/t/shell-script-error/300312/3 "2011-12-01T14:20:31Z")

</div>

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)?

---

<div class="post-metadata">

**Author:** ![BeefStu](https://community.unix.com/letter_avatar/beefstu/32/6_22734026db12803ebb3e059622ac3534.png) [@BeefStu](https://community.unix.com/u/BeefStu)\
**Post date:** [December 1, 2011, 2:39pm UTC](https://community.unix.com/t/shell-script-error/300312/4 "2011-12-01T14:39:21Z")

</div>

```nohighlight
 
#!/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|
 

```

---

<div class="post-metadata">

**Author:** ![Corona688](https://community.unix.com/letter_avatar/corona688/32/6_22734026db12803ebb3e059622ac3534.png) [@Corona688](https://community.unix.com/u/Corona688)\
**Post date:** [December 1, 2011, 2:41pm UTC](https://community.unix.com/t/shell-script-error/300312/5 "2011-12-01T14:41:50Z")

</div>

```plaintext
#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](http://partmaps.org/era/unix/award.html#backticks), incorrect use of backticks ( because you get splitting on spaces, not lines like you want), [useless use of cat](http://partmaps.org/era/unix/award.html), 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.

```plaintext
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 ----------

> [@beefstu](#):
>
> [code]while read -u9 dataline  
> do  
> c1=`echo "$dataline"|awk -F' ' '{print $1}'`  
> ...

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

---

<div class="post-metadata">

**Author:** ![thomasraj87](https://community.unix.com/letter_avatar/thomasraj87/32/6_22734026db12803ebb3e059622ac3534.png) [@thomasraj87](https://community.unix.com/u/thomasraj87)\
**Post date:** [December 2, 2011, 2:07am UTC](https://community.unix.com/t/shell-script-error/300312/6 "2011-12-02T02:07:20Z")

</div>

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

```nohighlight
$ 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

```

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

```nohighlight
$ 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

```
