Replacing variable values in html tags


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replacing variable values in html tags
# 1  
Old 09-26-2011
Java Replacing variable values in html tags

Hi please help me with this .

I have a file test.txt with following content
$cat test.txt
<td>$test</td>
<h2>$test2</h2>

and I have a ksh with following content
Code:
$cat test.ksh

#!/bin/ksh
test=3
test2=4
while read line
do
echo $line
done < test.html

I am expecting the output as
<td>3</td>
<h2>4</h2>

but I get the out put as


<td>$test</td>
<h2>$test2</h2>



please let me know how can I get the expected output and where my logic is failing Smilie

Last edited by vbe; 09-26-2011 at 12:27 PM..
# 2  
Old 09-26-2011
It doesn't work because shell doens't do that. It doesn't evaluate variables inside variables inside variables inside variables -- and that's mostly a good thing, you could tie yourself in knots with that.

I'd modify the file a little bit, into this:

Code:
cat <<EOF
<td>$test</td>
<h2>$test2</h2>
EOF

Then use it like
Code:
. test.txt

That will do it.
# 3  
Old 09-27-2011
Hi Corona688,

Thanks for the reply and sorry to bother you again.

I am pretty new to this and I am afraid I didn't understand the how to use the file that is modified. Please explain. I have changed the content of file to

$cat test
cat <<EOF
<td>$test</td>
<h2>$test2</h2>
EOF

But I am not sure how to use this in the script to have the variable values replaced.
# 4  
Old 09-27-2011
Quote:
Originally Posted by panduandpavan
Hi Corona688,

Thanks for the reply and sorry to bother you again.

I am pretty new to this and I am afraid I didn't understand the how to use the file that is modified. Please explain. I have changed the content of file to

$cat test
cat <<EOF
<td>$test</td>
<h2>$test2</h2>
EOF

But I am not sure how to use this in the script to have the variable values replaced.
So you must write all lines manually..
shell writes to stdoutput what just read from your file..
Code:
# test=3;test2=4
# echo "<td>$test</td>"
<td>3</td>
# echo "<td>$test2</td>"
<td>4</td>

or maybe test with sed (also manually)
Code:
# test=3;test2=4;sed 's/$test\b/'$test'/;s/$test2/'$test2'/' test.html
<td>3</td>
<h2>4</h2>

regards
ygemici
# 5  
Old 09-28-2011
Thanks EveryOne

I have just worked around the cat and EOF,got the desired out put. Thanks for your help.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Help in replacing column values

Hello All, I am having the file as below .I need to replace column 9-12 with some other values. In the below file I need to replace 1509 to 1508 and 1508 to 1507 .Can you please help me in how to do that Thanks, Arun ... (10 Replies)
Discussion started by: arunkumar_mca
10 Replies

2. Shell Programming and Scripting

How to remove the values inside the html tags?

Hi, I have a txt file which contain this: <a href="linux">Linux</a> <a href="unix">Unix</a> <a href="oracle">Oracle</a> <a href="perl">Perl</a> I'm trying to extract the text in between these anchor tag and ignoring everything else using grep. I managed to ignore the tags but unable to... (6 Replies)
Discussion started by: KCApple
6 Replies

3. Shell Programming and Scripting

awk file to read values from Db2 table replacing hard coded values

Hi, I want to replace a chain of if-else statement in an old AWK file with values from Db2 table or CSV file. The part of code is below... if (start_new_rec=="true"){ exclude_user="false"; user=toupper($6); match(user, "XXXXX."); if (RSTART ==2 ) { ... (9 Replies)
Discussion started by: asandy1234
9 Replies

4. UNIX for Dummies Questions & Answers

Replacing HTML tags with sed

Ok, so this is stupid simple, and I know I am going to feel like an idiot when I get help. I am altering a HTML report that has contraband in it so that the links to said contraband and the images are not shown. The link/img pairs are in the form of : <a... (5 Replies)
Discussion started by: twjolson
5 Replies

5. Shell Programming and Scripting

How to get values from href in HTML to sh

Hi, This is a part of my code: case "$1" in home) echo '<h2> Select a student: </h2>' echo '<br>' for stunum in $(ls ./) do echo "<a href=edit?numeric_stu=$stunum>`sed -n 1p ./$stunum/info`</a>" echo '<br>' done edit)... (1 Reply)
Discussion started by: jerrywangzi
1 Replies

6. Shell Programming and Scripting

Removing all except couple of html tags from html file

I tried to find elegant (or at least simple) way to remove all but couple of html tags from html file, but all examples I found dealt with removing all the tags. The logic of the script would be: - if there is <li> or <ul> on the line, do nothing (=write same line to output) - if there is:... (0 Replies)
Discussion started by: juubuntu
0 Replies

7. Shell Programming and Scripting

Parsing HTML, get text between 2 HTML tags

Hi there, I'm quite new to the forum and shell scripting. I want to filter out the "166.0 points". The results, that i found in google / the forum search didn't helped me :( <a href="/user/test" class="headitem menu" style="color:rgb(83,186,224);">test</a><a href="/points" class="headitem... (1 Reply)
Discussion started by: Mysthik
1 Replies

8. Shell Programming and Scripting

searching & replacing/removing only certain HTML tags

I generally save a lot of web pages for reading offline which works out great for school. Now I have to spend a lot of time on the bus and I am looking for the best way to read some of these webpages using my Nokia 7610. I have uploaded the files to my phone, but they are deadly deadly slow to... (2 Replies)
Discussion started by: naphelge
2 Replies

9. Shell Programming and Scripting

Replacing File values

Currently I am using the tr command in 3 scenarios for a ksh script. 1) Replacing any spaces in the file with a ~ tr ' ' '~' <$orignalFile> $newFile 2) After certain processing is done at the end of the scirpt i convert the Tilde back to spaces tr ' ' '~' <$newFile> $newFile2 3) Last... (4 Replies)
Discussion started by: hgjdv
4 Replies

10. Shell Programming and Scripting

replacing variable values in all files in directories

Hi Guys, I have some basic unix knowlege but new to shell scripting. I want to replace variable values in all files in sub-directories. Any sugestion will be helpful. Example. Main Dir: X Sub Dir: X1, X2, X3, X4, X5, X6.... X10 Each sub directory has file which contains variable A = 10. ... (3 Replies)
Discussion started by: newtoshell
3 Replies
Login or Register to Ask a Question