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 11-06-2013, 05:08 AM   #1
agarrett5
LQ Newbie
 
Registered: May 2012
Posts: 16

Rep: Reputation: Disabled
Question replace text inside all ' (single quotes)


How do I inside sed replace all text inside single quotes? What I have so far is. sed '/userPassword /{N;N;s/$/ test/}' shadow.file > shadowtemp.file


The single quotes comes in a line where there is userPassword, but the string I am replacing I do not know, but I want to change it to something I do know (test). I am writing it to a new file.

It is only the text in single quotes I want to change
 
Old 11-06-2013, 05:45 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
Use double quotes instead of single quotes: sed "s///" instead of sed 's///'

You don't provide an example of the original, so here's a general form:
Code:
sed "s/'.*'/'foobar'/" infile
Sed is greedy, so this example will work when there is just one set of single quotes.

Example:
Code:
$ cat infile
userPassword='foo1'
userPassword='foo2' userPassword='foo3'

$ sed "/userPassword/s/'.*'/'bar'/" infile
userPassword='bar'
userPassword='bar'
The second line shows sed's greediness.
 
Old 11-06-2013, 05:48 AM   #3
agarrett5
LQ Newbie
 
Registered: May 2012
Posts: 16

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by druuna View Post
Use double quotes instead of single quotes: sed "s///" instead of sed 's///'

You don't provide an example of the original, so here's a general form:
Code:
sed "s/'.*'/'foobar'/" infile
Sed is greedy, so this example will work when there is just one set of single quotes.

Example:
Code:
$ cat infile
userPassword='foo1'
userPassword='foo2' userPassword='foo3'

$ sed "/userPassword/s/'.*'/'bar'/" infile
userPassword='bar'
userPassword='bar'
The second line shows sed's greediness.
Here is an example of original:
zmprov ma example@jarsat.pl userPassword '{crypt}example'

to change to

zmprov ma example@jarsat.pl userPassword 'test'

Sorry for not giving an example before

I want to output the changed file to shadowtemp.file and keep the original shadow.file intact.

Last edited by agarrett5; 11-06-2013 at 05:50 AM. Reason: trying to give more detail
 
Old 11-06-2013, 05:56 AM   #4
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
I find your description very confusing , I have no certainty of what you require

please provide an input sample, as well as your desired output

for example, ( loosely based on your description )

Input
Code:
This is some random text
this line contains 'a random string of characters ' in single quotes
Desired Output
Code:
This is some random text
this line contains 'test' in single quotes
this sed would achieve that
Code:
sed "s/'.*'/'test'/" Input

Edit, I should have refreshed before posting

Last edited by Firerat; 11-06-2013 at 05:57 AM.
 
Old 11-06-2013, 05:56 AM   #5
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
Have you tried playing around with my suggestion?

Code:
$ sed "/userPassword/s/'.*'/'test'/" shadow.file > shadowtemp.file
 
Old 11-06-2013, 06:01 AM   #6
agarrett5
LQ Newbie
 
Registered: May 2012
Posts: 16

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by druuna View Post
Have you tried playing around with my suggestion?

Code:
$ sed "/userPassword/s/'.*'/'test'/" shadow.file > shadowtemp.file
Drunna, thats perfect. Firerat, sorry I will try to make it clearer next time.
 
Old 11-06-2013, 06:02 AM   #7
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
druuna's works fine

Code:
This is some random text
this line contains 'a random string of characters ' in single quotes
zmprov ma example@jarsat.pl userPassword '{crypt}example'
Code:
sed "/userPassword/s/'.*'/'test'/"
results in
Code:
This is some random text
this line contains 'a random string of characters ' in single quotes
zmprov ma example@jarsat.pl userPassword 'test'
the rest is just basic redirection ( shadow.file > shadowtemp.file )
 
  


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] Replace text string with sequential numbers inside a textfile K-Veikko Programming 3 04-07-2013 04:23 AM
translate value from single quotes to double quotes venkateshrupineni Linux - Newbie 2 06-14-2012 04:03 PM
running a command that contains single quotes with a variable inside babouyes Linux - Server 3 02-29-2012 01:15 PM
Using single quotes vs double quotes in PHP strings vharishankar Programming 6 07-11-2005 12:41 PM
trying to search and replace text file for single & multiple line breaks separately brokenfeet Programming 7 08-29-2003 02:56 PM


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