SED - minor changes work - Larger doesn't (working and non working code included)
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
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.
SED - minor changes work - Larger doesn't (working and non working code included)
Hi there I am changing strings to other strings in all files in a directory.
Here is an example of what works
CHANGE 1: - The visible copyright notice.
for file in *.html
do
cp $file $file.bak &&
sed 's/Copyright 1999-2005 - All rights reserved/Copyright 1999-2007 - All rights reserved/g' $file.bak >$file
done
CHANGE 2: - The internal copyright notice
(In change 2 I choose # as a delimiter as to keep sed from being confused.)
I think you're just using the wrong tool for the job. sed is good for processing files one line at a time; anything more is pushing it beyond what it's designed for. The error message "sed: -e expression #1, char 46: unterminated `s' command" was complaining about the newline character at the end of the first line of your sed script.
I think you need to load the whole file contents into a variable and then search for and replace the substring. Here's a version using bash. It would probably be much faster written in Python, Perl or Ruby:
Code:
#! /bin/bash
substring='</form>Make a difference - Make a donation!<br>
</td>
</tr>
</tbody>
</table>
<br>
'
replacement='</form>Make a difference - Make a donation!<br>
</td>
</tr>
</tbody>
</table>
<br>
<script type="text/javascript"><!--
google_ad_client = "pub-5045815486985038";
google_ad_width = 728;
google_ad_height = 90;
google_ad_format = "728x90_as";
google_ad_type = "text";
//2007-08-14: globabilityaug2007setup
google_ad_channel = "5631073777";
google_color_border = "000000";
google_color_bg = "FFFFFF";
google_color_link = "0000FF";
google_color_text = "000000";
google_color_url = "008000";
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script><br><br>'
for file in *.html; do
cp $file $file.bak # alternatively use 'mv'
file_contents="$(<$file.bak)"
echo "${file_contents//$substring/$replacement}" >$file
done
As I have understood it the cp will copy a backup file to .bak so you don't loose the original when sed is overwriting the original html...[snip]
Yes, cp will create a copy of the file, leaving the original file. On the other hand mv will rename the original file without the overhead of creating the copy. Since the original file is to be overwritten, by renaming the original you eliminate that overhead.
As to your problem, look into awk.
[edit]
I was looking at your site's source code, thinking I'd see if I could clobber together a simple awk program for you, and noticed that your html does not pass the W3C standards for html 4. (In fact, in the snippets you showed us, we see <br> instead of the expected standard construct: <br />.)
I also noticed that the "home page" included the "google" code, but that it did not seem to be working.
[/edit]
Last edited by PTrenholme; 09-22-2007 at 10:45 AM.
I think you're just using the wrong tool for the job. sed is good for processing files one line at a time; anything more is pushing it beyond what it's designed for. The error message "sed: -e expression #1, char 46: unterminated `s' command" was complaining about the newline character at the end of the first line of your sed script.
I think you need to load the whole file contents into a variable and then search for and replace the substring. Here's a version using bash. It would probably be much faster written in Python, Perl or Ruby:
Code:
#! /bin/bash
substring='</form>Make a difference - Make a donation!<br>
</td>
</tr>
</tbody>
</table>
<br>
'
replacement='</form>Make a difference - Make a donation!<br>
</td>
</tr>
</tbody>
</table>
<br>
<script type="text/javascript"><!--
google_ad_client = "pub-5045815486985038";
google_ad_width = 728;
google_ad_height = 90;
google_ad_format = "728x90_as";
google_ad_type = "text";
//2007-08-14: globabilityaug2007setup
google_ad_channel = "5631073777";
google_color_border = "000000";
google_color_bg = "FFFFFF";
google_color_link = "0000FF";
google_color_text = "000000";
google_color_url = "008000";
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script><br><br>'
for file in *.html; do
cp $file $file.bak # alternatively use 'mv'
file_contents="$(<$file.bak)"
echo "${file_contents//$substring/$replacement}" >$file
done
Yes, cp will create a copy of the file, leaving the original file. On the other hand mv will rename the original file without the overhead of creating the copy. Since the original file is to be overwritten, by renaming the original you eliminate that overhead.
As to your problem, look into awk.
[edit]
I was looking at your site's source code, thinking I'd see if I could clobber together a simple awk program for you, and noticed that your html does not pass the W3C standards for html 4. (In fact, in the snippets you showed us, we see <br> instead of the expected standard construct: <br />.)
I also noticed that the "home page" included the "google" code, but that it did not seem to be working.
[/edit]
Regarding the W3C Validation - It passed the validator tool last time I tinkered... Something I'll be looking into, thanks for the heads up.
Regarding the google code... odd, had people using both IE and FF test the bits that I had manually inserted.
As for the overhead - yup true, but the notion of having a backup is nice, right now the need for speed is not essential.
It works perfectly for me using bash 3.1.17. Either you've made some changes to the script above which have introduced a syntax error, or you're not using a bash shell. I see that you're using Ubuntu - you haven't got bash symlinked to something else have you? Try:
It works perfectly for me using bash 3.1.17. Either you've made some changes to the script above which have introduced a syntax error, or you're not using a bash shell. I see that you're using Ubuntu - you haven't got bash symlinked to something else have you? Try:
On Ubuntu 'sh' is symlinked to 'dash', so when you call the script by going 'sh myscript.sh' it will ignore the '#!/bin/bash' at the head of the script and use dash instead. dash cannot cope with the syntax in line 50 of your script.
The solution is either to run it with 'bash myscript.sh' or just to use 'chmod u+x myscript.sh' to make it executable, then call it with './myscript.sh'.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.