LinuxQuestions.org
Help answer threads with 0 replies.
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, 02: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, 03:12 AM   #2
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
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, 03:23 AM   #3
GlennsPref
Senior Member
 
Registered: Apr 2004
Location: Brisbane, Australia
Posts: 3,366
Blog Entries: 33

Rep: Reputation: 216Reputation: 216Reputation: 216
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 03:25 AM. Reason: welcome to LQ
 
Old 03-16-2010, 03:29 AM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
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, 03:51 AM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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, 03:54 AM   #6
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
@ 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 03:55 AM. Reason: s/quotes/enclosures/
 
Old 03-16-2010, 03:55 AM   #7
GlennsPref
Senior Member
 
Registered: Apr 2004
Location: Brisbane, Australia
Posts: 3,366
Blog Entries: 33

Rep: Reputation: 216Reputation: 216Reputation: 216
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, 03:59 AM   #8
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
@ 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, 05:12 AM   #9
GlennsPref
Senior Member
 
Registered: Apr 2004
Location: Brisbane, Australia
Posts: 3,366
Blog Entries: 33

Rep: Reputation: 216Reputation: 216Reputation: 216
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, 05: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, 05: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, 07: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, 07:50 AM   #13
jamescondron
Member
 
Registered: Jul 2007
Location: Scunthorpe, UK
Distribution: Ubuntu 8.10; Gentoo; Debian Lenny
Posts: 961

Rep: Reputation: 69
What happens when you do it? What output are you expecting?
 
Old 03-16-2010, 08:00 AM   #14
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

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


All times are GMT -5. The time now is 09:49 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration