LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 03-16-2010, 01:54 AM   #1
mamun2015
LQ Newbie
 
Registered: Mar 2010
Posts: 5

Rep: Reputation: 0
How to replace a character with the output of some commands using sed?


Hi guys,

I was facing problems with the following scenario for the last several hours, can anyone help me out?


rm -f /www/emailout/template.html

TODAY=`date '+%d-%m-%y'`

DBRUN=`ps ax | grep dtd `

sed -e 's/ncTODAY/'"$TODAY"'/g' -e "s/ncdbrun/'"$DBRUN"'/g" /www/emailout/test1.html > /www/emailout/template.html


But, I can't get the output of $DBRUN ?

What would be the solution, can anyone please fix the problem for me?
 
Old 03-16-2010, 02:12 AM   #2
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 555Reputation: 555Reputation: 555Reputation: 555Reputation: 555Reputation: 555
Your second sed -e expression is double-quoted (good), but the $DBRUN variable itself is also double-quoted (problem), so put the second -e expression in single quotes. Making that correction fixed it for me when I tested it.

Even simpler, would be to double-quote the -e expressions, and use NO quotes at all around the $VARIABLES inside the sed expressions. That way is simpler, and easier to read.

Examples:
Code:
sasha@reactor: TODAY=$(date '+%d-%m-%y')
sasha@reactor: DBRUN='BLARG!! RAWK!!!'

Your way:
sasha@reactor: echo 'ncTODAY...ncdbrun' | sed -e 's/ncTODAY/'"$TODAY"'/g' -e "s/ncdbrun/'"$DBRUN"'/g"
sed: -e expression #2, char 18: unterminated `s' command

My way:
sasha@reactor: echo 'ncTODAY...ncdbrun' | sed -e 's/ncTODAY/'"$TODAY"'/g' -e 's/ncdbrun/'"$DBRUN"'/g'
16-03-10...BLARG!! RAWK!!!

The simpler way (notice the quotes):
sasha@reactor: echo 'ncTODAY...ncdbrun' | sed -e "s/ncTODAY/$TODAY/g" -e "s/ncdbrun/$DBRUN/g"
16-03-10...BLARG!! RAWK!!!
sasha@reactor:
Sasha
 
1 members found this post helpful.
Old 03-16-2010, 02:23 AM   #3
GlennsPref
Senior Member
 
Registered: Apr 2004
Location: Brisbane, Australia
Distribution: Devuan
Posts: 3,654
Blog Entries: 33

Rep: Reputation: 283Reputation: 283Reputation: 283
Brilliant Sasha!

off topic, I'm learning sed.

Glenn

Quote:
Hi, Welcome to LQ!

LQ has a fantastic search function that may save you time waiting for an answer to a popular question.

With over 3 million posts to search it's possible the answer has been given.

Last edited by GlennsPref; 03-16-2010 at 02:25 AM. Reason: welcome to LQ
 
Old 03-16-2010, 02:29 AM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Quote:
Originally Posted by GlennsPref View Post
Brilliant Sasha!

off topic, I'm learning sed.

Glenn
Actually ... that's the SHELL doing the variable expansion,
not sed. sed doesn't care about what quotes you use.


Cheers,
Tink
 
1 members found this post helpful.
Old 03-16-2010, 02:51 AM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Yes, the quoting on the second expression is all messed up. Remember, quotes act more like toggles than enclosures. Also, single-quotes are escaped (meaning they lose their special meaning) when under the effect of double quotes, and vice-versa.

Let's simplify your two expressions.
expression 1:
Code:
-e 'string'   "$variable"  'string'
   ^      ^   ^         ^  ^      ^
  on     off on        off on    off
This one is good. single quote no. 2 toggles off escaping, then the double-quotes turn it back on again, while allowing the variable to expand. Then single quote no. 3 turns it back on again to protect the string.

expression 2:
Code:
-e " string '   " $variable "   ' string "
   ^        ^   ^           ^   ^        ^
  on        x  off         on   x       off
Not so good here. Both of the single quotes are "inside" double quotes, and so are being treated literally as part of the string expressions (meaning sed will probably not match anything. The variable is outside of the quotes and so it's allowed to expand, but since it's not protected in any way, word-splitting will take place on the contents, probably breaking the expression.

As Sasha said though, the best thing to do here is to simply enclose the whole thing in a single set of double-quotes. The variables will expand, then the whole thing will be sent to sed as a single expression.
Code:
-e "string $variable string"
 
1 members found this post helpful.
Old 03-16-2010, 02:54 AM   #6
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 555Reputation: 555Reputation: 555Reputation: 555Reputation: 555Reputation: 555
@ David -- that's a really good way of putting it:

The quotes act like toggles rather than enclosures.

I say: The less quotes the better!

Last edited by GrapefruiTgirl; 03-16-2010 at 02:55 AM. Reason: s/quotes/enclosures/
 
Old 03-16-2010, 02:55 AM   #7
GlennsPref
Senior Member
 
Registered: Apr 2004
Location: Brisbane, Australia
Distribution: Devuan
Posts: 3,654
Blog Entries: 33

Rep: Reputation: 283Reputation: 283Reputation: 283
Just goes to show how far I have to go....lol!

I'm using the Z shell(zsh) confuses me, but works nicely for what I need (aliases, etc).
Thank you, regards Glenn

regards Glenn
 
Old 03-16-2010, 02:59 AM   #8
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 555Reputation: 555Reputation: 555Reputation: 555Reputation: 555Reputation: 555
@ Glenn,

I've never used zsh, but you surely know that you can have aliases in other shells too, yes? Like Bash, Dash, etc..

Is there a particular reason you use zsh?

(Sorry OP, not trying to get off topic here but at least the question is hopefully answered!)
 
Old 03-16-2010, 04:12 AM   #9
GlennsPref
Senior Member
 
Registered: Apr 2004
Location: Brisbane, Australia
Distribution: Devuan
Posts: 3,654
Blog Entries: 33

Rep: Reputation: 283Reputation: 283Reputation: 283
I came across it reading a Jerry Peek article on LJ(Linux Journal) (if my memory serves me correctly)

It has a lot of globing effects I know nothing about, but I like the colours of the text (lol, candy bait) and the ease of set up. been using it for years, don't know why. I guess someone suggested it to me.

But it works, and it works well.

Sorry for the hijack OP, Glenn
 
Old 03-16-2010, 04:41 AM   #10
mamun2015
LQ Newbie
 
Registered: Mar 2010
Posts: 5

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by GrapefruiTgirl View Post
Your second sed -e expression is double-quoted (good), but the $DBRUN variable itself is also double-quoted (problem), so put the second -e expression in single quotes. Making that correction fixed it for me when I tested it.

Even simpler, would be to double-quote the -e expressions, and use NO quotes at all around the $VARIABLES inside the sed expressions. That way is simpler, and easier to read.

Examples:
Code:
sasha@reactor: TODAY=$(date '+%d-%m-%y')
sasha@reactor: DBRUN='BLARG!! RAWK!!!'

Your way:
sasha@reactor: echo 'ncTODAY...ncdbrun' | sed -e 's/ncTODAY/'"$TODAY"'/g' -e "s/ncdbrun/'"$DBRUN"'/g"
sed: -e expression #2, char 18: unterminated `s' command

My way:
sasha@reactor: echo 'ncTODAY...ncdbrun' | sed -e 's/ncTODAY/'"$TODAY"'/g' -e 's/ncdbrun/'"$DBRUN"'/g'
16-03-10...BLARG!! RAWK!!!

The simpler way (notice the quotes):
sasha@reactor: echo 'ncTODAY...ncdbrun' | sed -e "s/ncTODAY/$TODAY/g" -e "s/ncdbrun/$DBRUN/g"
16-03-10...BLARG!! RAWK!!!
sasha@reactor:
Sasha


Can you plase, change your DBRUN variable like this:

DBRUN=`ps ax`

Still I am getting the error? I don't know at least it should show as a string, there may be formatting problem.....please help,man!!
 
Old 03-16-2010, 04:58 AM   #11
mamun2015
LQ Newbie
 
Registered: Mar 2010
Posts: 5

Original Poster
Rep: Reputation: 0
Oh Shasha, I forgot to thank you......thanks a lot,man. Though I already worked similar things what you did for me, [ Copied from the previous one:
Can you plase, change your DBRUN variable like this:

DBRUN=`ps ax`

Still I am getting the error? I don't know at least it should show as a string, there may be formatting problem.....please help,man!! ]
 
Old 03-16-2010, 06:45 AM   #12
mamun2015
LQ Newbie
 
Registered: Mar 2010
Posts: 5

Original Poster
Rep: Reputation: 0
How to replace a character with the output of commands using sed?

Hi guys,

Is it possible to do the following:

TODAY=$(date '+%d-%m-%y')
DBRUN=`ps ax`

echo 'ncTODAY...ncdbrun' | sed -e "s/ncTODAY/$TODAY/g" -e "s/ncdbrun/$DBRUN/g"

I couldn't find anyway out,can anyone please help me?
 
Old 03-16-2010, 06:50 AM   #13
jamescondron
Member
 
Registered: Jul 2007
Location: Scunthorpe, UK
Distribution: Ubuntu 8.10; Gentoo; Debian Lenny
Posts: 961

Rep: Reputation: 70
What happens when you do it? What output are you expecting?
 
Old 03-16-2010, 07:00 AM   #14
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
first, "ps ax" on my machine produces 2 screenfuls of details on processes. Was it your intent to insert that all on one line in place of "ncbrun"?

Your syntax seems OK, but it is not at all obvious what the ultimate objective is.
 
Old 03-16-2010, 07:36 AM   #15
mamun2015
LQ Newbie
 
Registered: Mar 2010
Posts: 5

Original Poster
Rep: Reputation: 0
Thumbs up

Quote:
Originally Posted by pixellany View Post
first, "ps ax" on my machine produces 2 screenfuls of details on processes. Was it your intent to insert that all on one line in place of "ncbrun"?

Your syntax seems OK, but it is not at all obvious what the ultimate objective is.
Thanks for your question!

Basically, I need to search for a word then I need to replace it with the output of "ps ax", each process in new line.

please,reply.
 
  


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] sed help - replace line feed with different character bradvan Programming 7 04-22-2012 11:31 PM
find and replace (probably with sed) or some other linux commands dbgeek Linux - Newbie 2 12-14-2009 05:57 AM
Replace 2nd to last Character with SED elproducto Programming 5 03-31-2009 12:41 PM
find certain character and replace with a number yongitz Programming 1 01-18-2007 07:40 AM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 06:12 AM

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

All times are GMT -5. The time now is 02:47 AM.

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