Issue while using echo command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Issue while using echo command
# 1  
Old 06-25-2013
Issue while using echo command

Hi,
I have below shell script..
Code:
$ cat /tmp/1.txt
NAME-HOST-APPLIED TIME-DATE
$ cat /tmp/1.sh
#!/bin/ksh
echo "<html><table border size=1>" > /tmp/mail.html
for i in `cat /tmp/1.txt`
do
a1=`echo $i |cut -d "-" -f1`
a2=`echo $i |cut -d "-" -f2`
a3=`echo $i |cut -d "-" -f3`
a4=`echo $i |cut -d "-" -f4`
echo "<tr><td>$a1</td><td>$a2</td><td>$a3</td><td>$a4</td></tr>" >> /tmp/mail.html
done
echo "</table></html>" >> /tmp/mail.html
$

When i execute the script /tmp/1.sh i am getting the output like below..
Code:
$ sh /tmp/1.sh
$ cat /tmp/mail.html
<html><table border size=1>
<tr><td>NAME</td><td>HOST</td><td>APPLIED</td><td></td></tr>
<tr><td>TIME</td><td>DATE</td><td></td><td></td></tr>
</table></html>
$

But i want the output like
Code:
<html><table border size=1>
<tr><td>NAME</td><td>HOST</td><td>APPLIED TIME</td><td>DATE</td></tr>
</table></html>

I am not getting the desired ouptput since "APPLIED TIME" have space in between words.

Please help how can i get desired ouptput... i.e.,
Code:
"<tr><td>NAME</td><td>HOST</td><td>APPLIED TIME</td><td>DATE</td></tr>"


Last edited by Franklin52; 06-25-2013 at 09:22 AM.. Reason: Please use code tags for data and code samples
# 2  
Old 06-25-2013
Hi thomas,

Try this

Code:
awk 'BEGIN {FS="-";print "<html><table border size=1>"}{
printf("<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>\n",$1,$2,$3,$4);
}
END {print "</table></html>"}' /tmp/1.txt >> /tmp/mail.html

# 3  
Old 06-25-2013
Thanks kumar...

Its working fine...

But can i use my script to get the desired output by doing any change in that? i.e (i.e same flow and for loop)
# 4  
Old 06-25-2013
Use a while loop instead of for loop. Set IFS to - and read fields into separate variables.
Code:
echo "<html><body>"
echo "<table border size=1>"

while IFS="-" read f1 f2 f3 f4
do
        echo "<tr>"
        echo "<td>${f1}</td>"
        echo "<td>${f2}</td>"
        echo "<td>${f3}</td>"
        echo "<td>${f4}</td>"
        echo "</tr>"
done < /tmp/1.txt

echo "</table>"
echo "</body></html>"

If output looks good, redirect it to another file.
# 5  
Old 06-27-2013
Hi Thomas,
Yes..You can use n number of parameters,jus need to add only in the printf statements in terms of %s and $n.

regards,
kumar
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need to echo command successful if command is executed successfully

Hello, I have written a command n shell script : srvctl relocate service -d t1 -s s1 -i i1 -t t1 -f If the above command executes successfully without error I need to echo "Service relocated successfully and If it errors out I need to trap the errors in a file and also need to make... (1 Reply)
Discussion started by: Vishal_dba
1 Replies

2. Shell Programming and Scripting

Variable value substitution issue with awk command issue

Hi All, I am using the below script which has awk command, but it is not returing the expected result. can some pls help me to correct the command. The below script sample.ksh should give the result if the value of last 4 digits in the variable NM matches with the variable value DAT. The... (7 Replies)
Discussion started by: G.K.K
7 Replies

3. Shell Programming and Scripting

Echo output from command

Hello i am trying to do some calculation from output command for example ls -l if then echo "error" else echo "$" fi its something like this which get strings from output command and try to sum total lines of this string if exceed certain number it display error if not it... (3 Replies)
Discussion started by: mogabr
3 Replies

4. UNIX for Dummies Questions & Answers

Help me with Echo Command

Hi All I have the big statement which needs to be appended to the file MANUALLY without opening the file. So, i tried with echo command echo " failure ( 4446): for host xx.xx.xx.xxx trying to GET /index.html, wl-proxy reports: exception occurred for backend host 'xx.xx.xxx.xx/12210/12210':... (11 Replies)
Discussion started by: Kalaiela
11 Replies

5. Shell Programming and Scripting

Echo Command Help

Hi All I want to use the echo command to had some lines to a file but the lines i want to have contain " and ; here is my command system("echo user_pref("accessibility.typeaheadfind.flashBar", 0); > $profile"); but i think there are too many " and so it barfs n e ideas thanks A (5 Replies)
Discussion started by: ab52
5 Replies

6. Shell Programming and Scripting

Issue in echo

Hi Friends, I am trying a very simple thing to print through bash shell scripting using echo but failing. My script is- A=Abha B=Biha C=2011 D=5 echo " The name is $A_$B$C0$D" It should print- "The name is Abha_Biha201105" but it's printing something else. Is my script wrong?... (2 Replies)
Discussion started by: NARESH1302
2 Replies

7. Linux

set echo off command issue

Hi all, I am executing a Oracle SQL statement in a shell script and spooling the output of the query into a File with spool command. I want to ensure that only output of the query appears in file excluding the SQL statement but even set echo off command is not working. Please help (7 Replies)
Discussion started by: sumi_mn
7 Replies

8. Shell Programming and Scripting

What does following echo command do?

Can anybody tell me what does following command do? # echo gsoc1 | ./configure_cmd.sh Regards, akash mahakode (1 Reply)
Discussion started by: akash_mahakode
1 Replies

9. UNIX for Dummies Questions & Answers

Echo command with $$ $# $@

Hi, Good morning. Would you please explain to me what does it mean by the following command. echo "$$ $# $@" >> /opt/ftp_generic_send.log I know that something is being directed to log file, but not sure what exactly mean by those $$ and $# and $@ means. Thank you for your help.... (3 Replies)
Discussion started by: howdy
3 Replies

10. Shell Programming and Scripting

echo statement issue

Hi All, I am pasting my code below if # e means file exists then echo OFR_Configlist exists >> OFR_Backup_Configfiles.log else echo OFR_Configlist Not exists >> OFR_Backup_Configfiles.log exit fi How can i show the echo message in console also at the same time? I dont want to write... (3 Replies)
Discussion started by: subin_bala
3 Replies
Login or Register to Ask a Question