469,579 Members | 1,112 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,579 developers. It's quick & easy.

Help with script to change text within a file

I need help with my scripts. I am currently running a configuration script with multiple If Then statements using grep to check for the existance of a particulare set of text. If grep returns a null value then the script copies a madified version of the file over the existing file. This is actually working well, but I would rather just edit the lines of text in place without having the do the copy, just in the event that some other setting in the files may be changed in the future.

Any suggestions? I can post some sections of my script if it helps, but as I mentioned they are very basic If then using grep.

Thanks,

Rod
Aug 7 '07 #1
7 7684
Motoma
3,237 Expert 2GB
I need help with my scripts. I am currently running a configuration script with multiple If Then statements using grep to check for the existance of a particulare set of text. If grep returns a null value then the script copies a madified version of the file over the existing file. This is actually working well, but I would rather just edit the lines of text in place without having the do the copy, just in the event that some other setting in the files may be changed in the future.

Any suggestions? I can post some sections of my script if it helps, but as I mentioned they are very basic If then using grep.

Thanks,

Rod
sed should be able to do everything you need in this instance.
Aug 7 '07 #2
Well, I think that I answered my own question after doring some research on sed and reading through some of the other posts on similar questions.

I am still using the If Then and grep to identify if the file needs to be changed, but I am now using sed to transform the line of text within the file.

This seems to do exactly what I was looking for.

But.. I can not figure out how to make a backup copy of the original file in the sed command line. Can anyone help with that?

Thanks,

Rod
Aug 7 '07 #3
Motoma
3,237 Expert 2GB
Well, I think that I answered my own question after doring some research on sed and reading through some of the other posts on similar questions.

I am still using the If Then and grep to identify if the file needs to be changed, but I am now using sed to transform the line of text within the file.

This seems to do exactly what I was looking for.

But.. I can not figure out how to make a backup copy of the original file in the sed command line. Can anyone help with that?

Thanks,

Rod
If you have your regular expressions set up correctly, you should never need to test the file first. This is the exciting nature of regular expressions.

If making a copy of the file is not your style (cp myfile myfile.bak) perhaps one of these two options would work for you:
cat the original file, and pipe it through sed. Redirect the output to your new file. This will leave you with a new file with the changes made to it.
cat the original file, pipe it through tee to your backup file, pipe that through sed, and redirect the output to the original file. This will leave you with a backup of the original file (the argument to tee) and the original file being modified in place.
Aug 7 '07 #4
archonmagnus
113 100+
Here is an example script that I was using recently. It should help others who have similar questions.

Expand|Select|Wrap|Line Numbers
  1. #!bin/sh
  2. # This script replaces text in all files in a directory
  3.  
  4. for fl in *.php; do
  5. mv $fl $fl.old
  6. sed 's/<find>/<replace>/g' $fl.old > $fl
  7. done
  8.  
Aug 8 '07 #5
ghostdog74
511 Expert 256MB
Here is an example script that I was using recently. It should help others who have similar questions.

Expand|Select|Wrap|Line Numbers
  1. #!bin/sh
  2. # This script replaces text in all files in a directory
  3.  
  4. for fl in *.php; do
  5. mv $fl $fl.old
  6. sed 's/<find>/<replace>/g' $fl.old > $fl
  7. done
  8.  
if you have the "rename" command, you can use that too
Aug 10 '07 #6
Motoma
3,237 Expert 2GB
if you have the "rename" command, you can use that too
Isn't rename typically just a symlink to mv?
Aug 10 '07 #7
ghostdog74
511 Expert 256MB
Isn't rename typically just a symlink to mv?
the rename command i am talking about makes it easier (IMO) to rename files. eg
Expand|Select|Wrap|Line Numbers
  1. # ls 
  2. testing1 testing11 testing111
  3. # rename test TEST test*
  4. # ls
  5. TESTing1 TESTing11 TESTing111
  6.  
Aug 13 '07 #8

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

8 posts views Thread by chuck clark | last post: by
1 post views Thread by Rahul | last post: by
2 posts views Thread by reproots | last post: by
19 posts views Thread by pamelafluente | last post: by
1 post views Thread by vikjohn | last post: by
5 posts views Thread by dm3281 | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.