LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-11-2010, 05:36 AM   #1
linuxromeo
Member
 
Registered: Jun 2010
Posts: 45

Rep: Reputation: 15
Thumbs down 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....

Last edited by linuxromeo; 06-11-2010 at 05:41 AM.
 
Old 06-11-2010, 05:39 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
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).
 
Old 06-11-2010, 05:40 AM   #3
alli_yas
Member
 
Registered: Apr 2010
Location: Johannesburg
Distribution: Fedora 14, RHEL 5.5, CentOS 5.5, Ubuntu 10.04
Posts: 559

Rep: Reputation: 92
Hi

Do you have a question to ask? A title and a hat tip don't qualify as a question...
 
Old 06-11-2010, 05:44 AM   #4
linuxromeo
Member
 
Registered: Jun 2010
Posts: 45

Original Poster
Rep: Reputation: 15
Sorry for the trouble...it was a mistake while posting...now the question is edited..
 
Old 06-11-2010, 05:54 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
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
 
Old 06-11-2010, 06:04 AM   #6
linuxromeo
Member
 
Registered: Jun 2010
Posts: 45

Original Poster
Rep: Reputation: 15
Question

Quote:
Originally Posted by colucix View Post
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!!!
 
Old 06-11-2010, 06:06 AM   #7
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Hi,

What's the (error) message you get when executing colucix command (which looks correct).
 
Old 06-11-2010, 06:11 AM   #8
linuxromeo
Member
 
Registered: Jun 2010
Posts: 45

Original Poster
Rep: Reputation: 15
Thumbs down

Quote:
Originally Posted by druuna View Post
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.
 
Old 06-11-2010, 06:14 AM   #9
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
*romeo;
Have you read this?
http://www.grymoire.com/Unix/Sed.html
 
Old 06-11-2010, 06:16 AM   #10
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
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).
 
Old 06-11-2010, 06:18 AM   #11
linuxromeo
Member
 
Registered: Jun 2010
Posts: 45

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by pixellany View Post
*romeo;
Have you read this?
http://www.grymoire.com/Unix/Sed.html
Thanks for the link...I will look over it for the solution.
 
Old 06-11-2010, 06:19 AM   #12
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
Or just redirect to a new file...

eg:

sed 'stuff' oldfile > newfile
 
Old 06-11-2010, 07:47 AM   #13
Gridley
LQ Newbie
 
Registered: Apr 2010
Posts: 10

Rep: Reputation: 1
Looks like homework
 
Old 06-11-2010, 07:49 AM   #14
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
@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.
 
Old 06-11-2010, 07:53 AM   #15
Gridley
LQ Newbie
 
Registered: Apr 2010
Posts: 10

Rep: Reputation: 1
Ummmmm.........because it looks like homework ??
 
  


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
[SOLVED] Adding (not replacing) a pattern match with a similar pattern? b-bri Linux - Newbie 2 08-31-2009 01:36 AM
Replacing a pattern using variable? shivarajM Linux - Software 1 04-30-2009 02:23 AM
Replacing a bunch of lines between a pattern Namachivayam Programming 1 05-21-2007 08:23 PM
Replacing a image filename with a regex pattern in a script jonaster Linux - General 1 09-05-2006 08:02 PM
replacing pattern with sed produces double realos Programming 1 10-17-2002 09:03 PM


All times are GMT -5. The time now is 05:36 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