Solved

help with sed - unix  command

Posted on 2007-04-04
10
858 Views
Last Modified: 2013-12-26
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
0
Comment
Question by:aarch1
10 Comments
 
LVL 58

Expert Comment

by:amit_g
ID: 18853113
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.
0
 

Author Comment

by:aarch1
ID: 18853354
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
0
 
LVL 58

Expert Comment

by:amit_g
ID: 18853371
That should have worked. Adding i after the last # should make the match case-insensitive. Could you try again.
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 7

Expert Comment

by:nixfreak
ID: 18859028
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.$$
0
 
LVL 58

Expert Comment

by:amit_g
ID: 18859525
There is no need to use [bB] expression as i flag is already there for this situation.
0
 
LVL 7

Expert Comment

by:nixfreak
ID: 18860640
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?

0
 
LVL 58

Expert Comment

by:amit_g
ID: 18860687
Oh you are right. I am using gnu sed.
0
 
LVL 1

Accepted Solution

by:
Computer101 earned 0 total points
ID: 22505743
PAQed with points refunded (500)

Computer101
EE Admin
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question