LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Embed command output in sed expression. (https://www.linuxquestions.org/questions/linux-newbie-8/embed-command-output-in-sed-expression-790421/)

AwesomeMachine 02-20-2010 12:17 PM

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?

GrapefruiTgirl 02-20-2010 12:26 PM

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


jschiwal 02-20-2010 12:35 PM

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


AwesomeMachine 02-20-2010 01:18 PM

Here's eventually what I did:

Code:

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

EXTRAVERSION = -022010

XavierP 02-20-2010 01:48 PM

@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.

AwesomeMachine 02-20-2010 02:33 PM

Truth
 
Quote:

Originally Posted by XavierP (Post 3870757)
@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.

GrapefruiTgirl 02-20-2010 02:42 PM

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

Tinkster 02-20-2010 02:48 PM

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


All times are GMT -5. The time now is 03:40 AM.