LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Pattern replacing in a file (https://www.linuxquestions.org/questions/linux-newbie-8/pattern-replacing-in-a-file-813523/)

linuxromeo 06-11-2010 04:36 AM

Pattern replacing in a file
 
Hi

I want to replace a pattern in a file.The file format is given below.

.
.
111111
path=/home/fun/

222222
path=/home/fun/


333333
path=/home/fun

.
.
.
.
.

I want to replace "path=/home/fun" with another pattern, but only under "111111", all the others should be the same.

Thank you in advance for your valuable help....

druuna 06-11-2010 04:39 AM

Hi,

Did you find a solution or did something else go wrong while posting?

If you did find a solution, please post it for future reference (others might have the same problem).

alli_yas 06-11-2010 04:40 AM

Hi

Do you have a question to ask? A title and a hat tip don't qualify as a question...

linuxromeo 06-11-2010 04:44 AM

Sorry for the trouble...it was a mistake while posting...now the question is edited..

colucix 06-11-2010 04:54 AM

What have you tried so far? It should be a simple task for sed if you are a bit experienced with it. For example (not tested) by reading the next line when it encounters the pattern 111111 and doing the substitution accordingly:
Code:

sed '/^111111$/{n;s%path=/home/fun%something%}' file

linuxromeo 06-11-2010 05:04 AM

Quote:

Originally Posted by colucix (Post 3999993)
What have you tried so far? It should be a simple task for sed if you are a bit experienced with it. For example (not tested) by reading the next line when it encounters the pattern 111111 and doing the substitution accordingly:
Code:

sed '/^111111$/{n;s%path=/home/fun%something%}' file


Thanks for your suggestion, but it is not working...

I know that it can be done using sed, but dont know how to!!!

druuna 06-11-2010 05:06 AM

Hi,

What's the (error) message you get when executing colucix command (which looks correct).

linuxromeo 06-11-2010 05:11 AM

Quote:

Originally Posted by druuna (Post 4000006)
Hi,

What's the (error) message you get when executing colucix command (which looks correct).



There is no error messages seen.

But no editing is done in the file, it remains the same.

pixellany 06-11-2010 05:14 AM

*romeo;
Have you read this?
http://www.grymoire.com/Unix/Sed.html

druuna 06-11-2010 05:16 AM

Hi,

Add the -i.bak switch to the command (and do take a look at the link pixellany provided).

sed -i.bak '/^111111$/{n;s%path=/home/fun%something%}' file

A backup of the original file is made (with .bak as extension).

linuxromeo 06-11-2010 05:18 AM

Quote:

Originally Posted by pixellany (Post 4000018)
*romeo;
Have you read this?
http://www.grymoire.com/Unix/Sed.html

Thanks for the link...I will look over it for the solution.

pixellany 06-11-2010 05:19 AM

Or just redirect to a new file...

eg:

sed 'stuff' oldfile > newfile

Gridley 06-11-2010 06:47 AM

Looks like homework

druuna 06-11-2010 06:49 AM

@Gridley: That's very nice, especially after this is already solved.

BTW: Why do you think this, or are you just increasing your post count.

Gridley 06-11-2010 06:53 AM

Ummmmm.........because it looks like homework ??


All times are GMT -5. The time now is 03:15 PM.