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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
12-10-2014, 08:31 PM
|
#1
|
Member
Registered: Oct 2003
Location: Canada
Posts: 924
Rep:
|
Help with "sed" | Need to insert a $
Hey LQ,
I just followed this example here: http://www.isaacsukin.com/news/2013/...iles-directory
I'm using this command to update some PHP scripts and I need to find a pattern and replace it with a $var. Sed doesn't seem to want to work with the $ sign as it does everything else I expect it to except insert $var.
Any ideas?
Code:
grep -lr --exclude-dir=".git" -e "oldword" . | xargs sed -i "s/oldword/newword/g"
|
|
|
12-10-2014, 08:34 PM
|
#2
|
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,038
|
Doesn't seem to be any $ signs in your example??
|
|
|
12-10-2014, 08:46 PM
|
#3
|
Member
Registered: Oct 2003
Location: Canada
Posts: 924
Original Poster
Rep:
|
Quote:
Originally Posted by grail
Doesn't seem to be any $ signs in your example??
|
I was just pasting the generic command. Would it actually help if I showed you I wanted a dollar sign in it?
Code:
grep -lr --exclude-dir=".git" -e "someword" . | xargs sed -i "s/someword/$var,someword/g"
|
|
|
12-10-2014, 11:35 PM
|
#4
|
Member
Registered: Aug 2009
Location: Bangaluru, India
Distribution: CentOS 6.5, SuSE SLED/ SLES 10.2 SP2 /11.2, Fedora 11/16
Posts: 665
Rep: 
|
WARNING: Do not use any copied code untill you are quite sure what it is doing.
One more thing, Please show us some effort of your own instead of copying code and asking us to correct it.
Quote:
Originally Posted by wh33t
Code:
grep -lr --exclude-dir=".git" -e "someword" . | xargs sed -i "s/someword/$var,someword/g"
|
The grep will list all files having the someword in your curent directory unless you exclude, so any mistake of yours can give you a bad day at work.
I would prefer wrapping the variables
Code:
${variable}
------------
$var,someword
would be treated as a variable name unless you separate the variable to be identified by the nmae you gave it.
Last edited by SAbhi; 12-10-2014 at 11:36 PM.
Reason: typo correction
|
|
|
12-11-2014, 02:13 AM
|
#5
|
Member
Registered: Oct 2003
Location: Canada
Posts: 924
Original Poster
Rep:
|
Quote:
Originally Posted by SAbhi
WARNING: Do not use any copied code untill you are quite sure what it is doing.
One more thing, Please show us some effort of your own instead of copying code and asking us to correct it.
The grep will list all files having the someword in your curent directory unless you exclude, so any mistake of yours can give you a bad day at work.
I would prefer wrapping the variables
Code:
${variable}
------------
$var,someword
would be treated as a variable name unless you separate the variable to be identified by the nmae you gave it.
|
I apologize but I do not administer linux systems often. This is a random happenstance that comes up from time to time. Isn't the point of forums to get help? Perhaps you don't realize how I like to learn. I need examples to build a bit of confidence and then I go from there. Linux system administration to me, is a very daunting task. I'm always intimidated by it.
Would you mind explaining how I can wrap "$var" I tried with single quotes and it didn't work.
|
|
|
12-11-2014, 02:19 AM
|
#6
|
Moderator
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,374
|
Quote:
Originally Posted by wh33t
I apologize but I do not administer linux systems often. This is a random happenstance that comes up from time to time. Isn't the point of forums to get help? Perhaps you don't realize how I like to learn. I need examples to build a bit of confidence and then I go from there. Linux system administration to me, is a very daunting task. I'm always intimidated by it.
Would you mind explaining how I can wrap "$var" I tried with single quotes and it didn't work.
|
He means to "wrap" it in the curlybraces as shown...
Code:
$variable
...when wrapped becomes...
${variable}
... where the variable name goes inside the {...}.
UPDATE ****
But this is really only relevant if it goes into a shell script - potentially a problem inside a PHP script which is your actual target! So see next reply...
Last edited by astrogeek; 12-11-2014 at 02:35 AM.
|
|
|
12-11-2014, 02:29 AM
|
#7
|
Moderator
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,374
|
But now that I have read the whole thread (I had only read the last post previously...) I see that no one actually answered your question...
Try this...
Code:
grep -lr --exclude-dir=".git" -e "someword" . | xargs sed -i "s/someword/\$var,someword/g"
Note the backslash before the $.
In the context of the resultant php file the wrapping is potentially irrelevant or even an error, so do it as shown above.
As cautioned by others - test this first on an example file - do not copy, paste and use on your live files!
If you could post a snippet of an actual PHP file that you want to change, including an example of exactly what you want it changed to, we could give you a working example much more easily.
Last edited by astrogeek; 12-11-2014 at 02:53 AM.
|
|
1 members found this post helpful.
|
12-11-2014, 07:56 AM
|
#8
|
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,038
|
Could you not just use single quotes to stop the variable from expanding ... or have I missed something?
|
|
|
12-11-2014, 09:29 PM
|
#9
|
Member
Registered: Aug 2009
Location: Bangaluru, India
Distribution: CentOS 6.5, SuSE SLED/ SLES 10.2 SP2 /11.2, Fedora 11/16
Posts: 665
Rep: 
|
Quote:
Originally Posted by wh33t
with the $ sign as it does everything else I expect it to except insert $var.
Code:
grep -lr --exclude-dir=".git" -e "someword" . | xargs sed -i "s/someword/$var,someword/g"
|
I think i misunderstood the question, Sorry if it is so. You want $var to be inserted in the file but not its value, right ?
you can follow @astrogeek or @grail's solution.
Last edited by SAbhi; 12-11-2014 at 09:30 PM.
Reason: my head is bumping aroung the dollar sign, correcting typo :P
|
|
|
12-11-2014, 09:33 PM
|
#10
|
Member
Registered: Oct 2003
Location: Canada
Posts: 924
Original Poster
Rep:
|
Quote:
Originally Posted by astrogeek
But now that I have read the whole thread (I had only read the last post previously...) I see that no one actually answered your question...
Try this...
Code:
grep -lr --exclude-dir=".git" -e "someword" . | xargs sed -i "s/someword/\$var,someword/g"
Note the backslash before the $.
In the context of the resultant php file the wrapping is potentially irrelevant or even an error, so do it as shown above.
As cautioned by others - test this first on an example file - do not copy, paste and use on your live files!
If you could post a snippet of an actual PHP file that you want to change, including an example of exactly what you want it changed to, we could give you a working example much more easily.
|
Ahh yes, I knew there would be a way to escape characters somehow. This is it. Thank you.
|
|
|
12-12-2014, 12:39 AM
|
#11
|
Moderator
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,374
|
Quote:
Originally Posted by wh33t
Ahh yes, I knew there would be a way to escape characters somehow. This is it. Thank you.
|
You are welcome, glad that worked!
|
|
|
All times are GMT -5. The time now is 08:27 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|