Linux - Server This forum is for the discussion of Linux Software used in a server related context. |
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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
 |
03-01-2010, 05:18 PM
|
#1
|
Member
Registered: Apr 2009
Posts: 63
Rep:
|
problems using sed to find and replace....
I have a php file that contains a couple of variables:
$test=0;
$test2=0;
How can I find and replace only the first 0 so that it is $test=1?
This is just an example, not my actual code or variables. I essentially want to find a string in the file, and only the first occurrence of that string and replace it. The string will be on more than 1 line, but I only want the first occurrence on the first line it comes across to be replaced:
this doesn't work:
sed -i -e 's/0/1/'
that replaces all instances of 0 in the file with 1. Help please...
|
|
|
03-01-2010, 05:36 PM
|
#2
|
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809
|
Do you mean: - The first line in the file?
- The first time that "$test" appears?
- The first time that "0" appears?
read up on SED addressing---eg:
sed '/address/command' ##act on all lines containing "address"
sed '/stop/,/start/command' ##act on all lines from one containing "start" to one containing "stop", inclusive
sed '4,8command' ##act on line numbers 4-8
Good SED tutorial here:
http://www.grymoire.com/Unix/
|
|
|
03-01-2010, 05:41 PM
|
#3
|
LQ Newbie
Registered: Nov 2009
Posts: 3
Rep:
|
How about grep and awk?
Lately I found out grep and awk are extremely usefull tools for situations like that.
I would have to think a little more about how to combine grep/awk with sed, but I guess that'd be one way to go.
|
|
|
03-01-2010, 06:06 PM
|
#4
|
LQ Veteran
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,361
|
|
|
|
03-01-2010, 06:34 PM
|
#5
|
Member
Registered: Apr 2009
Posts: 63
Original Poster
Rep:
|
Quote:
Originally Posted by pixellany
Do you mean: - The first line in the file?
- The first time that "$test" appears?
- The first time that "0" appears?
read up on SED addressing---eg:
sed '/address/command' ##act on all lines containing "address"
sed '/stop/,/start/command' ##act on all lines from one containing "start" to one containing "stop", inclusive
sed '4,8command' ##act on line numbers 4-8
Good SED tutorial here:
http://www.grymoire.com/Unix/
|
I mean, the first time 0 appears. I'm sorry for the original post not being very clear.
<?php
$test=0;
$variable=280;
?>
In the above php, I'd only want the first time that 0 appears to be change. Otherwise, it could affect $variable...make sense?
|
|
|
All times are GMT -5. The time now is 01:51 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|