LinuxQuestions.org
Visit Jeremy's Blog.
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 12-10-2014, 08:31 PM   #1
wh33t
Member
 
Registered: Oct 2003
Location: Canada
Posts: 922

Rep: Reputation: 61
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"
 
Old 12-10-2014, 08:34 PM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Doesn't seem to be any $ signs in your example??
 
Old 12-10-2014, 08:46 PM   #3
wh33t
Member
 
Registered: Oct 2003
Location: Canada
Posts: 922

Original Poster
Rep: Reputation: 61
Quote:
Originally Posted by grail View Post
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"
 
Old 12-10-2014, 11:35 PM   #4
SAbhi
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: Reputation: Disabled
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 View Post
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
 
Old 12-11-2014, 02:13 AM   #5
wh33t
Member
 
Registered: Oct 2003
Location: Canada
Posts: 922

Original Poster
Rep: Reputation: 61
Quote:
Originally Posted by SAbhi View Post
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.
 
Old 12-11-2014, 02:19 AM   #6
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,268
Blog Entries: 24

Rep: Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195
Quote:
Originally Posted by wh33t View Post
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.
 
Old 12-11-2014, 02:29 AM   #7
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,268
Blog Entries: 24

Rep: Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195
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.
Old 12-11-2014, 07:56 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Could you not just use single quotes to stop the variable from expanding ... or have I missed something?
 
Old 12-11-2014, 09:29 PM   #9
SAbhi
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: Reputation: Disabled
Quote:
Originally Posted by wh33t View Post
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
 
Old 12-11-2014, 09:33 PM   #10
wh33t
Member
 
Registered: Oct 2003
Location: Canada
Posts: 922

Original Poster
Rep: Reputation: 61
Quote:
Originally Posted by astrogeek View Post
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.
 
Old 12-12-2014, 12:39 AM   #11
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,268
Blog Entries: 24

Rep: Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195
Quote:
Originally Posted by wh33t View Post
Ahh yes, I knew there would be a way to escape characters somehow. This is it. Thank you.
You are welcome, glad that worked!
 
  


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] "Insert" & "Delete" key returns "~" in a terminal. sharky Linux - General 15 04-26-2011 08:36 AM
bad: Want to insert a line into a text file using "sed" command eliote Linux - General 7 09-19-2010 02:55 AM
sed - use sed to replace multiple instances from "x" to "y" in a line mrodmac Linux - General 4 02-02-2010 11:37 AM
SED: how to insert a "&" character in a file carolflb Linux - Newbie 11 12-19-2009 10:48 AM

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

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