Visit Our UNIX and Linux User Community


Script to loop line in a file and add info or do echo


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to loop line in a file and add info or do echo
# 1  
Old 05-02-2012
Script to loop line in a file and add info or do echo

I have a record.txt it will update weekly, and it could be 2 lines or more ...
it just echo each line to the script
Code:
san jose,23.34%
tampa,2.15%
dallas,30.20%
seattle,44.29%
Unknown,16.72%

How do i write a shell script to give me a test.pl or bash file which contain
Code:
#!/home/perl
	print "Content-type: text/html\n\n";
	print "<HTML><HEAD><TITLE>Testing</TITLE>\n";
	
	

	print <<EOM; 
	<!-- graph code begins here-->
	<script type=\"text/javascript\" src=\"./wz_jsgraphics.js\"></script>
	<script type=\"text/javascript\" src=\"./pie.js\">
	
	<!-- Pie Graph script-By Balamurugan S Java, Apache, Perl, CSS, DHTML, graph, chart, bar, pie, line, Free Resource, Spiritual, OM, Travelouge, Enlightenment //-->
	<!-- Script featured/ available at Dynamic Drive code: Dynamic Drive DHTML(dynamic html) & JavaScript code library //-->
	
	</script>
	<center><div id=\"pieCanvas\" style=\"overflow: auto; position:relative;height:350px;width:400px;\"></div>
	
	<script type=\"text/javascript\">
	var p = new pie();
	p.add("san jose",23.34);
	p.add("tampa",2.15);
	p.add("dallas",30.20);
       p.add("seatle",44.29);
	p.add("Unknown",16.72);
        p.render("pieCanvas", "Pie Graph")
	</script></center>
<!-- graph code ends here-->
EOM
	
        print "	</BODY></HTML>\n";


Or something like this bash file?
Code:
#!/bin/bash
echo "Content-type: text/html"
echo ""
echo "<html><head><title>test"
echo "</title></head><body>"
echo "<!-- graph code begins here-->
echo "	<script type=\"text/javascript\" src=\"./wz_jsgraphics.js\"></script>
echo "	<script type=\"text/javascript\" src=\"./pie.js\">
	
echo "	<!-- Pie Graph script-By Balamurugan S Java, Apache, Perl, CSS, DHTML, graph, chart, bar, pie, line, Free Resource, Spiritual, OM, Travelouge, Enlightenment //-->
echo "	<!-- Script featured/ available at Dynamic Drive code: Dynamic Drive DHTML(dynamic html) & JavaScript code library //-->
	
echo "	</script>
echo "	<center><div id=\"pieCanvas\" style=\"overflow: auto; position:relative;height:350px;width:400px;\"></div>
	
echo "	<script type=\"text/javascript\">
echo "	var p = new pie();
echo " p.add("san jose",23.34);
echo "	p.add("tampa",2.15);
echo "	p.add("dallas",30.20);
 echo "      p.add("seatle",44.29);
echo "	p.add("Unknown",16.72);
echo "        p.render("pieCanvas", "Pie Graph")
echo "	</script></center>
echo "<!-- graph code ends here-->
echo "</body></html>"


Last edited by Scrutinizer; 05-02-2012 at 09:33 PM.. Reason: Changed <code> to [code],,
# 2  
Old 05-02-2012
Code:
#!/bin/bash
cat <<'EOF'
all your html here
EOF

while IFS=, read key val; do
    printf '  p.add("%s",%s)\n' "$key" "${val%?}"
done < record.txt

cat <<'EOF'
more html here
EOF

# 3  
Old 05-03-2012
I use your script but got error because it has extra un wanted
Code:
 var p = new pie();
	p.add("san jose",23.34);
	p.add("tampa",2.15);
	p.add("dallas",30.20);
       p.add("seatle",44.29);
	p.add("Unknown",16.72);
  p.add("
",);
  p.add("
",);

# 4  
Old 05-03-2012
Then don't put blank lines in the file, or skip them. Before printf try
Code:
[[ $val ]] || contine

# 5  
Old 05-03-2012
Thanks, it works now.

---------- Post updated at 02:33 PM ---------- Previous update was at 01:20 PM ----------

Can we round off decimal to a whole number when in one script?
Code:
#!/bin/bash
cat <<'EOF'
all your html here
EOF

while IFS=, read key val; do
[[ $val ]] || continue
    printf '  p.add("%s",%s)\n' "$key" "${val%?}"
done < record.txt

cat <<'EOF'
more html here
EOF

so i can get the result instead
Code:
var p = new pie();
	p.add("san jose",23.34);
	p.add("tampa",2.15);
	p.add("dallas",30.20);
       p.add("seatle",44.29);
	p.add("Unknown",16.72);

I will get
Code:
var p = new pie();
	p.add("san jose",23);
	p.add("tampa",2);
	p.add("dallas",30);
       p.add("seatle",44);
	p.add("Unknown",17);

Thanks

Moderator's Comments:
Mod Comment code tags for code and data, please.

Last edited by Corona688; 05-03-2012 at 05:39 PM..
# 6  
Old 05-03-2012
shell doesn't handle floating point too well, let's try awk instead.

Code:
#!/bin/bash
cat <<'EOF'
All your html here
EOF

awk -F"," '{ sub(/%/,""); printf("p.add(\"%s\", %d)\n", $1, $2+0.5); }' inputfile

cat <<'EOF'
more html here
EOF

# 7  
Old 05-03-2012
Code:
printf '  p.add("%s",%.0f)\n' "$key" "${val%?}"


Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Echo For ... Loop output into a file

Hi, All: I have one for ... loop code to generate a list of rename statement. However, echo the loop output on screen is OK. But store the echo output into a file is not working. So come here to seek help. My basic code is: ! /bin/ksh echo > DAS_VetFed.txt dir=/mydirectory cd $dir files=`ls... (6 Replies)
Discussion started by: duke0001
6 Replies

2. Shell Programming and Scripting

Echo print on same line while loop using variable

Currently using below script but echo it print the output in two line. Input file all-vm-final-2.txt CEALA08893 SDDC_SCUN DS_SIO_Workload_SAPUI_UAT_01 4 CEALA09546 SDDC_SCUN DS-SIO-PD5_Workload_UAT_SP1_Flash_07 4 CEALA09702 SDDC_SCUN DS-VSAN-RMP-WORKLOAD01 4 DEALA08762 SDDC_LDC... (3 Replies)
Discussion started by: ranjancom2000
3 Replies

3. Shell Programming and Scripting

BASH - Need to echo for loop output to one line

I'm trying to echo the release version of some of our Linux servers. Typically I do these types of things by "catting" a text file with the host names, "ssh-ing" to the host and running my string. This is what I've written for i in `cat versions.txt` ; do echo $i ; ssh $i cat /etc/issue |... (5 Replies)
Discussion started by: lombardi4851
5 Replies

4. Shell Programming and Scripting

Adding line in a file using info from previous line

I have a shell script that looks something like the following: mysql -uroot db1 < db1.sql mysql -uroot db2 < db2.sql mysql -uroot db3 < db3.sql mysql -uroot db4 < db4.sql .... different db names in more than 160 lines. I want to run this script with nohup and have a status later. So,... (6 Replies)
Discussion started by: MKH
6 Replies

5. Shell Programming and Scripting

How to read each line from input file, assign variables, and echo to output file?

I've got a file that looks like this (spaces before first entries intentional): 12345650-000005000GL140227 ANNUAL HELC FEE EN 22345650-000005000GL140227 ANNUAL HELC FEE EN 32345650-000005000GL140227 ANNUAL HELC FEE EN I want to read through the file line by line,... (6 Replies)
Discussion started by: Scottie1954
6 Replies

6. Shell Programming and Scripting

HELP: Shell Script to read a Log file line by line and extract Info based on KEYWORDS matching

I have a LOG file which looks like this Import started at: Mon Jul 23 02:13:01 EDT 2012 Initialization completed in 2.146 seconds. -------------------------------------------------------------------------------- -- Import summary for Import item: PolicyInformation... (8 Replies)
Discussion started by: biztank
8 Replies

7. Shell Programming and Scripting

How to pull info under headers in file(awk,grep,while loop)

below is an extract from my file and I am trying to use Awk and grep and a while loop to pull infomation from under neath "HBA WWN=".HBA WWN=" reoccurs all over the file but the 100000c.....number are unique and I want to be able to pull and reference specifi information under this header ever time... (2 Replies)
Discussion started by: kieranfoley
2 Replies

8. UNIX for Dummies Questions & Answers

echo a line number when script is executing

BTW, this is my first post and I'm fairly new to Unix. :D I'm having issues with a korn shell script running abnormally long. What we would like to do is echo the line number the job is executing at the time. Is there any sort of function or environment variable that allows such a thing? ... (1 Reply)
Discussion started by: gavineq
1 Replies

9. Shell Programming and Scripting

trying to write a script to loop through a port info file

Below is part of a script i have written to loop through part of a port info file. How do i continue the script to get info for OS Device Name, manufacturer and then put information into an array? HBA Port WWN: 10000000c9420b4b OS Device Name: /dev/cfg/c10 Manufacturer: Emulex... (5 Replies)
Discussion started by: rcon1
5 Replies

10. Shell Programming and Scripting

how to echo the file contents LINE BY LINE

hello, i have a listing (let say ABC) consists of the below: : public database link public synonym role rollback segment : when i run the below for loop, for i in `more ABC` do echo "$i" done it gives me, : public database (4 Replies)
Discussion started by: newbie168
4 Replies

Featured Tech Videos