LinuxQuestions.org
Visit Jeremy's Blog.
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 10-01-2012, 09:48 AM   #1
roopakl
Member
 
Registered: Sep 2011
Posts: 95

Rep: Reputation: Disabled
search and replace particular word with sed using variable


Hi All,
I want to search username and replace "home" word with "sftp" word in passswd file.
Code:
$ grep roopa passwd 
roopa:x:1001:1001::/home/roopa/:/sbin/nologin
I changed "home" to "sftp" using
Code:
$ sed -i '/roopa:x/ s/home/sftp/g' passwd
$ grep roopa passwd 
roopa:x:1001:1001::/sftp/roopa/:/sbin/nologin
Working fine.

Now I want to again change back from sftp to home, for that I used
Code:
$ username=roopa
$ sed -i '/$username:x/ s/sftp/home/g' passwd
$ grep roopa passwd 
roopa:x:1001:1001::/sftp/roopa/:/sbin/nologin
But it is not working, so need your help.
Thanks in advance.

Last edited by roopakl; 10-01-2012 at 09:51 AM.
 
Old 10-01-2012, 10:34 AM   #2
rosehosting.com
Member
 
Registered: Jun 2012
Location: Missouri, USA
Posts: 222

Rep: Reputation: 62
The single quotes prevent expansion of the variable. You should use double quotes instead of the single ones.

Quote:
Originally Posted by roopakl View Post
Hi All,
I want to search username and replace "home" word with "sftp" word in passswd file.
Code:
$ grep roopa passwd 
roopa:x:1001:1001::/home/roopa/:/sbin/nologin
I changed "home" to "sftp" using
Code:
$ sed -i '/roopa:x/ s/home/sftp/g' passwd
$ grep roopa passwd 
roopa:x:1001:1001::/sftp/roopa/:/sbin/nologin
Working fine.

Now I want to again change back from sftp to home, for that I used
Code:
$ username=roopa
$ sed -i '/$username:x/ s/sftp/home/g' passwd
$ grep roopa passwd 
roopa:x:1001:1001::/sftp/roopa/:/sbin/nologin
But it is not working, so need your help.
Thanks in advance.
 
1 members found this post helpful.
Old 10-01-2012, 11:08 AM   #3
roopakl
Member
 
Registered: Sep 2011
Posts: 95

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by rosehosting.com View Post
The single quotes prevent expansion of the variable. You should use double quotes instead of the single ones.
Thank you very much rosehosting.com, you solved it.
 
  


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] Search and Replace from Word List - Scripting ivn Linux - Newbie 12 10-02-2011 09:21 PM
Search word and delete only the word and the line using Sed command kbmukesh Linux - Newbie 4 06-28-2011 06:35 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
Sed search for variable and delete entire line, but variable contains forward /'s Passions Programming 2 11-10-2008 03:44 PM
sed 1st word to replace all instances of ( donnied Programming 5 08-21-2008 06:43 PM


All times are GMT -5. The time now is 02:28 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration