LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 03-01-2010, 06:18 PM   #1
tdnnash25
Member
 
Registered: Apr 2009
Posts: 63

Rep: Reputation: 15
Question 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...
 
Old 03-01-2010, 06:36 PM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
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/
 
Old 03-01-2010, 06:41 PM   #3
hiok
LQ Newbie
 
Registered: Nov 2009
Posts: 3

Rep: Reputation: 0
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.
 
Old 03-01-2010, 07:06 PM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 17,251

Rep: Reputation: 2640Reputation: 2640Reputation: 2640Reputation: 2640Reputation: 2640Reputation: 2640Reputation: 2640Reputation: 2640Reputation: 2640Reputation: 2640Reputation: 2640
See the sed faq
 
Old 03-01-2010, 07:34 PM   #5
tdnnash25
Member
 
Registered: Apr 2009
Posts: 63

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by pixellany View Post
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?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
find and replace (probably with sed) or some other linux commands dbgeek Linux - Newbie 2 12-14-2009 06:57 AM
Find several different words and replace with one using sed. Techno Guy Linux - Newbie 18 07-06-2009 08:16 AM
Use sed to find and replace a url xmrkite Linux - Software 4 10-10-2007 08:20 PM
sed - find and replace command bullshit Programming 9 01-05-2006 04:25 AM
SED;find and replace;help required gd13 Programming 3 12-21-2004 07:33 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 08:57 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration