LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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, 04: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, 04:45 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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, 04: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 04:50 AM. Reason: trying to give more detail
 
Old 11-06-2013, 04:56 AM   #4
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
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 04:57 AM.
 
Old 11-06-2013, 04:56 AM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Have you tried playing around with my suggestion?

Code:
$ sed "/userPassword/s/'.*'/'test'/" shadow.file > shadowtemp.file
 
Old 11-06-2013, 05: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, 05:02 AM   #7
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
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



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

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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