473,322 Members | 1,287 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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 7794
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

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

Similar topics

8
by: chuck clark | last post by:
Hi, I am sorry if this is not the most appropriate group to ask this in (if not, please point me in the right direction). I am following examples from this page,...
8
by: baustin75 | last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie only when debugging in php designer 2005 -------------------------------------------------------------------------------- ...
1
by: Rahul | last post by:
Hi Everybody I have some problem in my script. please help me. This is script file. I have one *.inq file. I want run this script in XML files. But this script errors shows . If u want i am...
2
by: reproots | last post by:
hi there, i am having trouble with a span command and would like to know if someone can help me, or point me in the correct direction. basically, i want a little textbox to pop up when a mouse...
19
by: pamelafluente | last post by:
Hi Guys, I am trying to include my little script in my html report. I have done an external JS file which contains it. If you remember, you have helped me to detect if the asp page was present...
0
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted...
9
by: dli07 | last post by:
Hello, I'm trying to convert a piece of code that creates a dynamic vertical resizing bar in a table from internet explorer to firefox. It's based on a post from...
1
by: vikjohn | last post by:
I have a new perl script sent to me which is a revision of the one I am currently running. The permissions are the same on each, the paths are correct but I am getting the infamous : The specified...
5
by: dm3281 | last post by:
I'm really starting to hate writing services -- or trying to, anyway. Why do I need to rename my project to the service name? Why do I need to set the "ServiceName" property to my service name?...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.