LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 12-08-2016, 04:11 AM   #1
asteroid4u
Member
 
Registered: Jun 2015
Posts: 58

Rep: Reputation: Disabled
sed seach and replace word script


Hi,

I need to search keyword and replace the next key word (that is after = ). Below is the file output


file1
bob=123
cape=abc
bod=xyz

search cape and replcae its value

I am trying below script but it is changing to all the lines. I execute script like sh




sh test.sh cape


a=Grear
b=$1
sed -i "/.*/${b}=${a}" /root/file

I am expecting file out put looks like below

file1
bob=123
cape=Gear
bod=xyz

Please help
 
Old 12-08-2016, 04:18 AM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,359
Blog Entries: 3

Rep: Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767
It will help to wrap the code in [code] [/code] tags so it is more readable.

But as to the content of your question, you'll need to use s in sed. It is usually used like this:

Code:
sed -e "s/old/new/"
There are a lot of tutorials on sed around. Here is a good one to get an overview from:

http://www.grymoire.com/Unix/Sed.html

Then you can broaden or narrow your search.

Last edited by Turbocapitalist; 12-08-2016 at 04:23 AM.
 
Old 12-08-2016, 04:27 AM   #3
asteroid4u
Member
 
Registered: Jun 2015
Posts: 58

Original Poster
Rep: Reputation: Disabled
Hi Turbocapitalist,

I won't get old value to search and replace.

I have key word exit in file say cape=xyz and I want to replace xyz with new value that produced by script.
 
Old 12-08-2016, 04:37 AM   #4
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,359
Blog Entries: 3

Rep: Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767
Quote:
Originally Posted by asteroid4u View Post
Hi Turbocapitalist,

I won't get old value to search and replace.

I have key word exit in file say cape=xyz and I want to replace xyz with new value that produced by script.
Then you'll still need the s command in sed and when you have the following

Code:
sed -e "s/old/new/"
you can use a pattern for "old". See man 7 regex for the hairy details.

Don't try to use the -i option until you have a working pattern.

So you might try something like this:

Code:
#!/bin/sh
a=Grear
b=$1
sed -e "s/^${b}=.*$/${b}=${a}/" /root/file
But there are several other ways to do it as well.
 
1 members found this post helpful.
Old 12-08-2016, 04:45 AM   #5
asteroid4u
Member
 
Registered: Jun 2015
Posts: 58

Original Poster
Rep: Reputation: Disabled
Thanks a lot it worked
 
Old 12-08-2016, 04:52 AM   #6
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,359
Blog Entries: 3

Rep: Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767
Great. Be sure to take a look at the link in #2 above and then skim through the regex manual page. It is more important that you understand why it worked than whether it worked or not.

The $a and $b are environment variables and being wrapped in double quotes means that the shell will interpret them early on. Then ^ stands for the start of the line itself and $ stands for the end of the line itself.
 
Old 12-08-2016, 01:55 PM   #7
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,832

Rep: Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219
Or with a back-reference: \1 puts back what matched within the \( \)
Code:
sed "s/^\(${b}=\).*/\1${a}/" /root/file
 
  


Reply

Tags
linux, programing, scripting, sed bash



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
bash: replace word/s on screen with sed? webking Programming 8 05-12-2016 03:46 PM
[SOLVED] how replace exact word using sed santhosh.ananthula Linux - Desktop 4 01-27-2016 05:10 PM
[SOLVED] search and replace particular word with sed using variable roopakl Linux - Newbie 2 10-01-2012 11:08 AM
[SOLVED] Find/Replace shell script that replaces word with other word in text, filenames yanom Programming 8 09-12-2012 12:29 AM
Trying to use sed to replace a number that is in a string with a word simpsonc Linux - Newbie 3 09-12-2010 07:55 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 08:19 PM.

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