Shell Script Error


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell Script Error
# 1  
Old 12-01-2011
Shell Script Error

Hi,

My file is
-----------
Code:
#cat html.txt
db1 10 10g
db2 20 20g
#

My script is
-------------
Code:
#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
---------------------------------------------------------------
Code:
Details
db1::
10::
10g::
db2::
20::
20g::

I want the output like:
-----------------------
Code:
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?

Last edited by thomasraj87; 12-01-2011 at 03:12 PM.. Reason: Please use code tags, and don't cross post. Thanks.
# 2  
Old 12-01-2011
Code:
tr ' ' ':' <  html.txt

# 3  
Old 12-01-2011
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)?
# 4  
Old 12-01-2011
Code:
 
#!/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|

# 5  
Old 12-01-2011
Code:
#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.

Code:
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 ----------

Quote:
Originally Posted by 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?
# 6  
Old 12-02-2011
Hi All,
Thanks for your timely help. Now both the scripts are working good.

Code:
$ 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

--------------------------------
Code:
$ 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


Last edited by Scott; 12-02-2011 at 07:26 AM.. Reason: Please use code tags
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script with sql script error

Hi All when I execute from psql prompt, I get the result, when I try to automate using a shell script, the query is not working # `/usr/bin/psql -U postgres -d coba1 -c "select name from users where "Date" > current_date - 30;"` ERROR: column "Date" does not exist LINE 1: select... (2 Replies)
Discussion started by: srilinux09
2 Replies

2. Shell Programming and Scripting

Calling shell script within awk script throws error

I am getting the following error while passing parameter to a shell script called within awk script. Any idea what's causing this issue and how to ix it ? Thanks sh: -c: line 0: syntax error near unexpected token `newline' sh: -c: line 0: `./billdatecalc.sh ... (10 Replies)
Discussion started by: Sudhakar333
10 Replies

3. Shell Programming and Scripting

Error in calling a shell script from another script

HI, We are using two shell scripts, script.sh,env.sh, where env.sh will be called inside script.sh. The variable inside env.sh is used as $var in script.sh.But while running the script its not identifying that variable. Is there any permission needed to call a script inside another script. ... (3 Replies)
Discussion started by: banupriyat
3 Replies

4. Shell Programming and Scripting

Syntax error calling TCL script from shell script

hello everyone i am beginner on shell scripting .and i am working on my project work on ad hoc network i wrote a batch (.sh) to do a looping and execute a tcl script i wrote before in each iteration ..but i got this problem " syntax error near unexpected token `('... (1 Reply)
Discussion started by: marcoss90
1 Replies

5. Shell Programming and Scripting

Error in Shell script

Hello All, I am newbe to scripting and have just taken over following script from previous developer. I am getting following error when running the script. line 70: syntax error near unexpected token `do Could some help me to rectify the error please. Thanks in advance for your... (9 Replies)
Discussion started by: Pahadia
9 Replies

6. Shell Programming and Scripting

How to grep sql error in shell script and exit the script?

I need help in the following script. I want to grep the sql errors insert into the error table and exit the shell script if there is any error, otherwise keep running the scripts. Here is my script #!/bin/csh -f source .orapass set user = $USER set pass = $PASS cd /opt/data/scripts echo... (2 Replies)
Discussion started by: allinshell99
2 Replies

7. Shell Programming and Scripting

Shell script error

Hi, I have the following table in MYSQL: (the structure looks broken in this forum but if you copy/paste it into notepad, it'll look right): +----------------------------+-----------------------+------+-----+---------+----------------+ | Field | Type |... (0 Replies)
Discussion started by: tezarin
0 Replies

8. UNIX for Dummies Questions & Answers

awk Shell Script error : "Syntax Error : `Split' unexpected

hi there i write one awk script file in shell programing the code is related to dd/mm/yy to month, day year format but i get an error please can anybody help me out in this problem ?????? i give my code here including error awk ` # date-month -- convert mm/dd/yy to month day,... (2 Replies)
Discussion started by: Herry
2 Replies

9. UNIX for Dummies Questions & Answers

error in shell script

Hi, I have written a small shell script which logs into each oracle database on the server and displays whether it is in archivelog mode or not.. The script is as under: #!/bin/bash dblist=`ps -ef | grep smon | grep -v grep |cut -d'_' -f3` for ohome in $dblist; do sqlplus -s /nolog <<... (2 Replies)
Discussion started by: jalpan.pota
2 Replies
Login or Register to Ask a Question