Link to home
Start Free TrialLog in
Avatar of aarch1
aarch1

asked on

help with sed - unix command

I have 100 files looking like this.
<html>
<body>
.....
</body>
</html>
---
I need to add the below line right above the <body> line in each of the above 100 files.

<SCRIPT LANGUAGE='JavaScript'>parent.frames.help.location.href = "/sun1/p1?in_p=test";</SCRIPT>

I need a sed script to do this please. I am using kshell in sun solaris unix box.

AMIT G. ALREADY GAVE ME A SOLUTION FOR THE ABOVE.

The solution is
for i in "*.html"
do
    sed 's#<body>#<SCRIPT LANGUAGE="JavaScript">parent.frames.help.location.href
= "/sun1/p1?in_p=test";</SCRIPT><body>#' $i > /tmp/$i.$$
    mv /tmp/$i.$$
done

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

THE NEW NEED IS,
WHEN THE <BODY> TAG IS IN MIXED CASE THE ABOVE SOLUTION DOES NOT WORK.

Thanks
Avatar of amit_g
amit_g
Flag of United States of America image

Change

sed 's#<body>#<SCRIPT LANGUAGE="JavaScript">parent.frames.help.location.href
= "/sun1/p1?in_p=test";</SCRIPT><body>#' $i > /tmp/$i.$$

to

sed 's#<body>#<SCRIPT LANGUAGE="JavaScript">parent.frames.help.location.href
= "/sun1/p1?in_p=test";</SCRIPT><body>#i' $i > /tmp/$i.$$

You could have asked this as a followup in the previous question itself.
Avatar of aarch1
aarch1

ASKER

Hi,
That does not work.
I know a way to fix it.
Below takes care of Body or body.
But then I need to do for each char.
sed 's#<[Bb]ody>#<SCRIPT LANGUAGE="JavaScript">parent.frames.help.location.href
= "/sun1/p1?in_p=test";</SCRIPT><body>#' $i > /tmp/$i.$$

Do you know of any other solution?



Thanks
That should have worked. Adding i after the last # should make the match case-insensitive. Could you try again.
amit,

Give this a try:

sed 's#<\([bB][oO][dD][yY]\)>#<SCRIPT LANGUAGE="JavaScript">parent.frames.help.
location.href = "/sun1/p1?in_p=test";</SCRIPT><\1>#' $1 > /tmp/$i.$$
There is no need to use [bB] expression as i flag is already there for this situation.
Amit,

Oops, instead of the OP I put your name by mistake.
BTW, I tried the i flag but it doesn't work in Solaris. Perhaps you are using some other version of sed like gnu sed?

Oh you are right. I am using gnu sed.
ASKER CERTIFIED SOLUTION
Avatar of Computer101
Computer101
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial