LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Fedora
User Name
Password
Fedora This forum is for the discussion of the Fedora Project.

Notices


Reply
  Search this Thread
Old 01-22-2007, 03:39 AM   #1
clpl1980
Member
 
Registered: Dec 2006
Posts: 32

Rep: Reputation: 15
Bash - txt replace


Hi eveybody,

is it possible, inside a bash script to do the following?

. find all (HTML) files in a folder that contain a given keyword
. replace that keyword with a text contained inside a given text files (saved on a local path)

how?

Thank you all!
 
Old 01-22-2007, 05:38 AM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
is it possible, inside a bash script to do the following?
Sure, but easier (tool for the purpose, efficient, generic) would be to use rpl since it fully replaces your script and is faster.


how?
Since you didn't explicitly ask us to write the script for you: you'll need a "while loop", find, cat, sed and mv. Use cat' to get the change text into a variable, 'find' to find the filenames by extension, pipe the output so it can be used in the "while loop" (and maybe do some tests like the file exists and is not a symlink, access permissions), ''sed' to rewrite the text to a tempfile and 'mv' to move the tempfile back to the filename.
Code:
function help() { echo "Bash scripting guides:
http://www.tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html 
http://www.tldp.org/LDP/Bash-Beginners-Guide/html/index.html 
http://www.tldp.org/LDP/abs/html/"; }

BTW, threads like these are usually found in the "Programming" forum. I'll move it there since this ain't distro-specific.
 
Old 01-22-2007, 05:44 AM   #3
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 31
Sure. Use the "find" command to find all HTML files within a specified folder. (This will drill down to subfolders if necessary.)

Then on those files, use the "sed" command to replace the source text with the replacement text. You may wish to do this by using the sed command to read the file containing the keyword and writing another file containing the sed command to use on each HTML file. This sed command should write from the original file to some temporary file name, and then use the "cp" command to copy the new temporary file back to the original file.

For further info, try these commands:

man find
man sed
man cp

This should get you started. Once you've written your script, check it out on a new directory in which you've written some fake test data, just to make sure it doesn't wipe out all your data.

If you have difficulty with your script, write back!

Hope this helps.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Help Strip / Remove attachments (Replace with TXT file if Possible) Vince0000 Linux - General 2 09-17-2010 06:46 PM
Does anyone know of a bash script can search & replace txt in a file. jimwelc Linux - Newbie 6 09-15-2008 12:13 AM
How do i change spaces inside a txt file, and replace space with backslash \? repolona Linux - Newbie 1 05-24-2006 05:08 AM
Read a line in a txt file with bash orgazmo Programming 5 05-03-2005 07:10 AM
Read a line in a txt file with bash orgazmo Linux - Newbie 3 05-03-2005 04:16 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Fedora

All times are GMT -5. The time now is 08:01 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration