473,386 Members | 1,736 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,386 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 7796
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

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.