LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Help with sed (https://www.linuxquestions.org/questions/programming-9/help-with-sed-695667/)

faizlo 01-07-2009 02:58 PM

Help with sed
 
Hi All,

I am beginning my way into sed, I want to do the following:

I have a file "file" which contains few lines. I have two different words in two different lines (word1 in line 1 and word2 in line 4) which I need to change to be "change1" in line 1, and "change2" in line4. They should be replaced in the same file, i.e. keep the file name intact "file".
How would I do that?

Here is exactly what I want:
Code:

void FV(string infile = "test2.xxx",
            bool ele = 0,
            bool exit=1,
            string leafname = "output2",
            int nbins = 75,
            float xmin  = 0,
            float xmax  = 1.0000001 ){

I want "test2.xxx" to be test3.xxx, and "output2" to be "output3"

Thank you so much in advance,

faizlo

bgeddy 01-07-2009 04:45 PM

I have called your file tsed - this will make the changes in place. Note the changes will not be confined to lines 1 and 4 but as I take it the searched expressions are unique this will be OK :
Code:


sed -i -e "s/test2.xxx/test3.xxx/" -e "s/output2/output3/"  tsed


faizlo 01-07-2009 06:49 PM

It worked, Thank you so much,

faizlo

syg00 01-07-2009 07:26 PM

Yes, but do you understand why it worked ?.

bgeddy 01-07-2009 08:05 PM

Quote:

Yes, but do you understand why it worked ?.
Good point.

I would hope that those asking for help would maybe have hit a "brick wall" in their trials and so need assistance or guidance.

Our solutions are all offered freely in the spirit of helpfulness and to further knowledge but lets hope the enquirers are not just taking advantage of this for an easy ride.

pixellany 01-07-2009 10:40 PM

Quote:

Originally Posted by syg00 (Post 3400552)
Yes, but do you understand why it worked ?.

You took the words right out of my mouth. It is so tempting to approach these questions as a teacher---showing the student how to find the answer, but not giving them the answer.

syg00 01-07-2009 11:11 PM

I note the OP has now taken this answer and started a new thread with it. Appears prepared to learn.


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