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 12-10-2014, 09:31 PM   #1
wh33t
Member
 
Registered: Oct 2003
Location: Canada
Posts: 672

Rep: Reputation: 46
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, 09:34 PM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,424

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

Original Poster
Rep: Reputation: 46
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-11-2014, 12:35 AM   #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: 664

Rep: Reputation: 80
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-11-2014 at 12:36 AM. Reason: typo correction
 
Old 12-11-2014, 03:13 AM   #5
wh33t
Member
 
Registered: Oct 2003
Location: Canada
Posts: 672

Original Poster
Rep: Reputation: 46
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, 03:19 AM   #6
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_10{.0|.1|.2}
Posts: 4,024
Blog Entries: 1

Rep: Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112
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 03:35 AM.
 
Old 12-11-2014, 03:29 AM   #7
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_10{.0|.1|.2}
Posts: 4,024
Blog Entries: 1

Rep: Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112
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 03:53 AM.
 
1 members found this post helpful.
Old 12-11-2014, 08:56 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,424

Rep: Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823
Could you not just use single quotes to stop the variable from expanding ... or have I missed something?
 
Old 12-11-2014, 10: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: 664

Rep: Reputation: 80
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 10:30 PM. Reason: my head is bumping aroung the dollar sign, correcting typo :P
 
Old 12-11-2014, 10:33 PM   #10
wh33t
Member
 
Registered: Oct 2003
Location: Canada
Posts: 672

Original Poster
Rep: Reputation: 46
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, 01:39 AM   #11
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_10{.0|.1|.2}
Posts: 4,024
Blog Entries: 1

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


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


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