LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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


Closed Thread
  Search this Thread
Old 02-20-2010, 12:17 PM   #1
AwesomeMachine
LQ Guru
 
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 5,524

Rep: Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015
Post Embed command output in sed expression.


One would suspect:

sed -i 's/EXTRAVERSION =/EXTRAVERSION = -$(date +%D)/g' file

would search in the file, file for the string: 'EXTRAVERSION =', and change every instance it finds to 'EXTRAVERSION = -02/20/2010'. BUT IT DOESN'T.

It changes it to 'EXTRAVERSION = $(date +%D)', literally. I've tried single and double quotes in almost every possible configuration. Is there any way to tell sed that one desires the command output, not the literal text?
 
Old 02-20-2010, 12:26 PM   #2
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 555Reputation: 555Reputation: 555Reputation: 555Reputation: 555Reputation: 555
Code:
sasha@reactor: echo "EXTRA=5768" | sed "s/5768/-$(date)/g"
EXTRA=-Sat Feb 20 14:34:17 AST 2010
sasha@reactor:
@ Awesome -- not sure why it won't work for you -- it works for me; and, as you suggest you have tried, using double quotes has always worked for me when the replacement contains a variable I want to insert, rather than a literal string.

NOTE that in my example, I just used `echo` but I have done the same during sed-inplace on files, and that too has worked. Try again with double quotes?

Sasha

OH -- wait! Your date string will contain SLASHES, which may be borking the RHS -- use something else as a separator, like a % or a pipe. EDIT: Yes, this seems to be the problem.
Like so:
Code:
sasha@reactor: echo "EXTRA=5768" | sed "s%5768%-$(date +%D)%g"
EXTRA=-02/20/10
sasha@reactor

Last edited by GrapefruiTgirl; 02-20-2010 at 12:29 PM.
 
1 members found this post helpful.
Old 02-20-2010, 12:35 PM   #3
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
You need to use double quotes instead of single quotes. Then the variables and command substitution is performed before sed gets the command.

You can use `set' to see how arguments were expanded before the command runs:
Code:
set sed -i 's/EXTRAVERSION =/EXTRAVERSION = -$(date +%D)/g' file
jschiwal@qosmio:/tmp> echo $*
sed -i s/EXTRAVERSION =/EXTRAVERSION = -$(date +%D)/g file
jschiwal@qosmio:/tmp> set sed -i "s/EXTRAVERSION =/EXTRAVERSION = -$(date +%D)/g" file
jschiwal@qosmio:/tmp> echo $*
sed -i s/EXTRAVERSION =/EXTRAVERSION = -02/20/10/g file
 
Old 02-20-2010, 01:18 PM   #4
AwesomeMachine
LQ Guru
 
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 5,524

Original Poster
Rep: Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015
Here's eventually what I did:

Code:
sed -i "s%EXTRAVERSION =%EXTRAVERSION = \-$(date +%D | sed 's%\/%%g')%g" ./Makefile
Which yielded:
Code:
EXTRAVERSION = -022010

Last edited by Tinkster; 02-20-2010 at 02:49 PM. Reason: Actually you only must abide by LQs rules while at LQ.
 
Old 02-20-2010, 01:48 PM   #5
XavierP
Moderator
 
Registered: Nov 2002
Location: Kent, England
Distribution: Debian Testing
Posts: 19,192
Blog Entries: 4

Rep: Reputation: 475Reputation: 475Reputation: 475Reputation: 475Reputation: 475
@AwesomeMachine - what on earth are you talking about? You "must voice an opinion"? Not sure if you have noticed, but we're no longer in the Bronze Age. Your weird spouting would appear to be something a caricature of a Middle Eastern person would say. While it's cool that your time machine took you back 6000 years and you're in the perfect world, you still have to post in ours. Edit your thread to take out the offensive statements and I'll get this removed.

And if it was supposed to be a joke, well, no one is laughing.
 
Old 02-20-2010, 02:33 PM   #6
AwesomeMachine
LQ Guru
 
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 5,524

Original Poster
Rep: Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015
Arrow Truth

Quote:
Originally Posted by XavierP View Post
@AwesomeMachine - what on earth are you talking about? You "must voice an opinion"? Not sure if you have noticed, but we're no longer in the Bronze Age. Your weird spouting would appear to be something a caricature of a Middle Eastern person would say. While it's cool that your time machine took you back 6000 years and you're in the perfect world, you still have to post in ours. Edit your thread to take out the offensive statements and I'll get this removed.

And if it was supposed to be a joke, well, no one is laughing.
What was Truth IS Truth NOW, and it always will be Truth. There is One Truth. Everything else is wrong. I don't make Truth. I don't even enforce it. I only report it. If no one is willing to speak Truth, everybody dies. I don't expect appreciation, only tolerance. Am I not entitled to my opinion? Is my opinion wrong? Do certain people, like me, have 'wrong thinking'? I'm a masculine male, which is more than I can say for most men nowadays.
 
Old 02-20-2010, 02:42 PM   #7
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 555Reputation: 555Reputation: 555Reputation: 555Reputation: 555Reputation: 555
Nobody asked you to report "your version of truth", nor did anyone ask for your opinion about a woman's "role" in society. Whether or not your opinion is wrong, is not at issue, though opinions, by their very nature, are neither right nor wrong-- they are simply opinions.
Around here, you will not be appreciated, nor shown tolerance, for expressing your opinion on this subject matter, especially in a thread that is both unrelated to said opinion, and not in the General forum.

You have two more lines of text to remove from your post. In case you are unsure which ones they are, they are the ones following the second code block.

Sasha
 
Old 02-20-2010, 02:48 PM   #8
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
And with Sasha's words I'm taking this thread to the grave.



With no respect or tolerance for Awesome's outburst of primeval
tribal law I removed the remaining offending lines.



Cheers,
Tink

Last edited by Tinkster; 02-20-2010 at 02:50 PM.
 
  


Closed Thread


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 or awk help - need to remove text on each line before a regular expression kmkocot Linux - Newbie 15 10-30-2009 03:20 AM
sed regular expression match everything up to a certain character bhepdogg Programming 3 05-28-2009 02:59 PM
sed - regular expression Vilmerok Programming 5 02-26-2009 08:44 AM
sed regular expression Ammad Linux - General 7 10-29-2008 05:52 PM
sed regular expression help needed Dew Linux - Newbie 1 03-30-2005 02:59 PM

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

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