Welcome to Linux Forums! With a comprehensive Linux Forum, information on various types of Linux software and many Linux Reviews articles, we have all the knowledge you need a click away, or accessible via our knowledgeable members.
Find the answer to your Linux question:
New to Linux Forums? Register here for free!
    Linux Forums > GNU Linux Zone > Linux Programming & Scripting > Specific find and replace

Forgot Password?
 Linux Programming & Scripting   C, Perl, PHP, Bash Scripts, anything programming or script related post in here!

Site Navigation
Linux Articles
Linux Forums
Linux Downloads
Linux Hosting
Free Magazines
Job Board
IRC Chat
RSS Feeds


Linux Forum Topics
Linux Forums
Your Distro
Linux Resources
GNU Linux Zone
The Community
Reply
 
Thread Tools Display Modes
Old 3 Weeks Ago   #1 (permalink)
Just Joined!
 
Join Date: Aug 2009
Location: Mumbai, India
Posts: 46
Specific find and replace

Hi,

I have a text file which is split into various sections with different names and those sections have the same variables and values defined. eg.
[section 1]
ipaddr=192.168.1.1
user=user1
passwd=password1

[variable 2]
ipaddr=192.168.1.1
user=user2
passwd=password2

.... and so on...

I am looking for a command which would help me replace a specific string like the ipaddr value in a specific section - say the second section coz i need to integrate it into a bash script. I don't wanna do a global find and replace.

Assistance appreciated.

--Syd
syd05 is offline  


Reply With Quote
Old 3 Weeks Ago   #2 (permalink)
Just Joined!
 
Join Date: Jan 2007
Location: Tamil Nadu, India
Posts: 16
If you unique sections, then you can very well use 'sed' command to replace the contents under a particular section.
mvenkatapathy is offline   Reply With Quote
Old 3 Weeks Ago   #3 (permalink)
Linux User
 
Join Date: Aug 2006
Posts: 434
Quote:
Originally Posted by syd05 View Post
Hi,

I have a text file which is split into various sections with different names and those sections have the same variables and values defined. eg.
[section 1]
ipaddr=192.168.1.1
user=user1
passwd=password1

[variable 2]
ipaddr=192.168.1.1
user=user2
passwd=password2

.... and so on...

I am looking for a command which would help me replace a specific string like the ipaddr value in a specific section - say the second section coz i need to integrate it into a bash script. I don't wanna do a global find and replace.

Assistance appreciated.

--Syd
see here (example 3) for similar example
ghostdog74 is offline   Reply With Quote
Old 2 Weeks Ago   #4 (permalink)
Just Joined!
 
Join Date: Aug 2009
Location: Mumbai, India
Posts: 46
Quote:
Originally Posted by ghostdog74 View Post
see here (example 3) for similar example
Hi,

Thanx for pointing out to an URL. Of course I'll have to brush up on my knowledge of sed since the code mentioned in the URL there did not make much sense to me.

From whatever little bit i managed to understand i assume the code groups multiple lines of the section and assists with search and replace. In the file that I have, not all the sections are of equal length. I mean some initial sections span 10-15 lines and the latter ones 4 - 5 lines but the sections on which i want the search and replace to work are of similar length.Would the code work in this scenario as well?

Meantime since the code helps me get started somewhere atleast, I'll get on with understanding sed. Thanx...

--Syd
syd05 is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Free Magazines
Run Your Own Web Server Using Linux & Apache - Free 191 Page Preview
Learn about everything you'll need to build and maintain your Linux servers, and to deploy Web applications to them.
subscribe
Open Source Security Myths Dispelled
Dispel the five major myths surrounding Open Source Security and gain the tools necessary to make a truly informed decision for your IT organization
subscribe
InformationWeek
InformationWeek is the only newsweekly you'll need to stay on top of the latest developments in information technology.
subscribe



All times are GMT. The time now is 05:36 PM.






© 2000 - 2009 - All Rights Reserved - Property of  MAS Media

Content Relevant URLs by vBSEO 3.3.0 RC2