LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-26-2015, 10:20 AM   #1
victorcheung
LQ Newbie
 
Registered: Jun 2015
Posts: 3

Rep: Reputation: Disabled
How to replace a string if another specific string exists in the line


Hi,

A file contains sample data as below:

:
ini.parameter1('_DB_NAME_','PROD');
ini.parameter2('_DB_NAME_','UAT');
ini.parameter3('_DB_NAME_','DEV');
ini.parameter4('_DB_NAME_','STG');
:

and I want to write a bash script to replace the value after the '_DB_NAME_' as 'TEST', so, the new file will become:

:
ini.parameter1('_DB_NAME_','TEST');
ini.parameter2('_DB_NAME_','TEST');
ini.parameter3('_DB_NAME_','TEST');
ini.parameter4('_DB_NAME_','TEST');
:

how to do this?

Thanks!
Victor

Last edited by victorcheung; 06-26-2015 at 10:30 AM.
 
Old 06-26-2015, 10:48 AM   #2
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Hi.

Code:
$ sed "/_DB_NAME_/s/,'[^']*'/,'TEST'/" infile
:
ini.parameter1('_DB_NAME_','TEST');
ini.parameter2('_DB_NAME_','TEST');
ini.parameter3('_DB_NAME_','TEST');
ini.parameter4('_DB_NAME_','TEST');
:
 
3 members found this post helpful.
Old 06-26-2015, 01:47 PM   #3
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,616

Rep: Reputation: 7962Reputation: 7962Reputation: 7962Reputation: 7962Reputation: 7962Reputation: 7962Reputation: 7962Reputation: 7962Reputation: 7962Reputation: 7962Reputation: 7962
Quote:
Originally Posted by victorcheung View Post
Hi,
A file contains sample data as below:

ini.parameter1('_DB_NAME_','PROD');
ini.parameter2('_DB_NAME_','UAT');
ini.parameter3('_DB_NAME_','DEV');
ini.parameter4('_DB_NAME_','STG');

and I want to write a bash script to replace the value after the '_DB_NAME_' as 'TEST', so, the new file will become:

ini.parameter1('_DB_NAME_','TEST');
ini.parameter2('_DB_NAME_','TEST');
ini.parameter3('_DB_NAME_','TEST');
ini.parameter4('_DB_NAME_','TEST');

how to do this?
firstfire gave you a solution. However, you need read the LQ Rules and "Question Guidelines" link in my posting signature. You need to show some effort of your OWN, rather than asking people to just hand you an answer. Also, the way this question is written and your user name point to this not being your first post on LQ....
 
Old 06-26-2015, 07:20 PM   #4
victorcheung
LQ Newbie
 
Registered: Jun 2015
Posts: 3

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by firstfire View Post
Hi.

Code:
$ sed "/_DB_NAME_/s/,'[^']*'/,'TEST'/" infile
:
ini.parameter1('_DB_NAME_','TEST');
ini.parameter2('_DB_NAME_','TEST');
ini.parameter3('_DB_NAME_','TEST');
ini.parameter4('_DB_NAME_','TEST');
:
Hi firstfire,

Thank you very much, it works!

Victor
 
  


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
How to show selected string using grep from file and replace it with new input string prasad1990 Linux - Software 2 03-19-2015 08:02 AM
[SOLVED] Check Multiple line string exists in a file Arun Shankar Programming 12 09-30-2013 07:27 AM
Search and replace specific string in different files in a directory using shell. Lucifer999 Programming 8 06-26-2011 06:37 AM
how do i replace a text string in a file with a random string? (with sed etc) steve51184 Linux - Software 16 09-02-2010 11:05 AM
How to identify a line and replace another string on that line using Shell script? Sid2007 Programming 10 10-01-2007 08:49 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:21 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
Open Source Consulting | Domain Registration