Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place! |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
11-10-2009, 05:57 AM
|
#1
|
LQ Newbie
Registered: Nov 2009
Posts: 3
Rep:
|
how to replace lots of text
Hi everyone, I am new to Linux and unable to solve a problem. I have a html file and need to replace its text regularly using cronjob.
If example, the html file has
<font face=arial>Just a test.</font>
<font face=arial>This is a test.</font>
<font face=arial>This is just a test.</font>
<font face=arial>This is also a test.</font>
<font face=arial>This is only a test.</font>
I would require it to be replaced to the following regularly
<font face=arial>just a test.</font>
<font face=arial>this is a test.</font>
<font face=arial><b>this is just a test.</b></font>
<font face=arial><b>this is also a test.</b></font>
<font face=arial>this is only a test.</font>
What would be the easiest way to have a shell script replace everything from <font face=arial>J*only a test.</font> to a new chunk of text?
I was looking at sed examples but it seems to be good at changing only a few words. I am concerned about the file having special characters too, ie. <>?$& and not sure if sed could do the words + characters replacement well.
Pls help...
|
|
|
11-10-2009, 07:03 AM
|
#2
|
Member
Registered: Jul 2009
Posts: 31
Rep:
|
sed is a stream editior, could possibly use that to modify something. You can use multiples in a line, for example:
A file called "stuff.txt" with:
I have two CD's for music
I have one CD for games
I have three CD's for a new distro
sed 's/CD/DVD/g' -e 's/distro/monkey/g' stuff.txt
Will replace the Word CD with DVD and distro with monkey, so afterwards the file "stuff.txt" becomes:
I have two DVD's for music
I have one DVD for games
I have three DVD's for a new monkey
You can also looks for only lines containing something:
sed -e '/three/s/new/naughty/g' stuff.txt
I have two CD's for music
I have one CD for games
I have three CD's for a naughty distro
Or you can put the lot together for:
sed -e '/three/s/new/naughty/g' -e 's/CD/DVD/g' -e 's/distro/monkey/g' stuff.txt
I have two DVD's for music
I have one DVD for games
I have three DVD's for a naughty monkey
Depending on what you want to achieve, could this help? Sed is pretty powerful, and there's a lot more too it depending on what you want to achieve. It is non-interactive, awk is possibly something else to use, it's a little more complex though. Definitely two things to look at.
Last edited by CmdoColin; 11-10-2009 at 07:19 AM.
Reason: sorry mong moment - just did a basic sed example
|
|
|
11-10-2009, 04:30 PM
|
#3
|
Member
Registered: Nov 2005
Distribution: CentOS 5, Fedora 23
Posts: 218
Rep:
|
or you can use perls file handler abilities to take line x, do whatever to it then recreate the whole file using the modified line(s). Sed is probably the way to go but it's a bit complicated
|
|
|
11-10-2009, 08:05 PM
|
#4
|
LQ Guru
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 11,066
|
There are several "power tools" which can be applied here.
- sed is the "stream editor" which accepts a single stream of input, applies some filter or edit to it, and passes the result as its stream of output.
- awk is a tool that is designed for processing more complicated files. Here you have a series of conditions that awk is to test for, along with rules that are to be applied to those records which meet each condition.
- Perl is a full-fledged programming language that is especially suited to text manipulation. (And, well, just about anything and everything else that you could possibly think of...)
|
|
|
11-10-2009, 09:18 PM
|
#5
|
LQ Newbie
Registered: Nov 2009
Posts: 3
Original Poster
Rep:
|
Thanks for the replies.
I guess the sed examples won't work. It becomes too difficult to edit because there are a lot of text.
Ideally the perl or shell script should do the following:
1) check example.html file and look for this part
<html>*</h1>
the asterix means include everything that appears from <html> to </h1>, it does not matter what text is inside it, ultimately this whole part will be changed to something else, getting it to recognise the correct part (from <html> to </h1>) is more important
2) replace the above part into something else for example
<html>
<head>
<title>this is new title</title>
<script></script>
</head>
<body>
<h1>this is new text and is only a test</h1>
3) Text information in example.html is updated with the new text information from <html> to </h1>
Getting sed to look for lines won't work because the lines in example.html keep changing. As long as it could replace everything from <html> to </h1>, job is done.
What is the best way to tackle the above?
Last edited by acider; 11-10-2009 at 09:22 PM.
|
|
|
11-11-2009, 12:38 AM
|
#6
|
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,419
|
|
|
|
11-11-2009, 12:42 AM
|
#7
|
LQ Newbie
Registered: Nov 2009
Posts: 3
Original Poster
Rep:
|
Wow thats too much for me to understand.
All I need is an example of a working script.
|
|
|
11-11-2009, 12:51 AM
|
#8
|
Senior Member
Registered: Aug 2006
Posts: 2,697
|
Code:
awk -F">" '{
s=$1">" tolower(substr($2,1,1)) substr($2,2)">"
print s
}' file
output
Code:
$ ./shell.sh
<font face=arial>just a test.</font>
<font face=arial>this is a test.</font>
<font face=arial>this is just a test.</font>
<font face=arial>this is also a test.</font>
<font face=arial>this is only a test.</font>
you did not say how the <B> comes about...so i will leave it as that.
|
|
|
All times are GMT -5. The time now is 09:19 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|