1

from this answer: Using a command inside a sed substitution there is used this to make command substitution:

echo aaabbdd | sed -e 's/a*/echo `echo -n & | wc -m`/;e'

When I shell use double quotes to expand bash commands (as suggested in comments: sed '$a'"$(some commands)" somefile and when to use embedded commands in sed (as from the example above - from the link) - because I can obviously use & (sed match) in outer bash command. So which use when?

10
  • & is not placing the echo command in the background - it's substituted by the matched portion (in this case, aaa) before the pattern space is passed to the shell for execution Commented May 1, 2020 at 15:32
  • try sed '$a'"$(alias so)" however, any escape sequences like \t in the content of the command output will get expanded, and you cannot have literal newlines in that output Commented May 1, 2020 at 15:34
  • @steeldriver I do not understand, can you please elaborate the substituted by the matched portion, from when does & substitute any portion and which portion? From preceding command? I do no understand. give some exmaple used in bash Commented May 1, 2020 at 15:37
  • also, if you wanted to use -i option with that alias example, you do not need sed at all, alias so >> filename will append the output of the command to the file Commented May 1, 2020 at 15:39
  • @Sundeep that is not the point, I want to use sed with bash commands. Can you still explain the '&' ? Is it just sed substitution or is it bash feature? Commented May 1, 2020 at 15:40

1 Answer 1

-2

See the following script:

echo "DATE_REPLACE_ME_hh" | sed -e "s/DATE_REPLACE_ME/$(date '+%Y-%m-%d')/g"
1
  • This does not appear to address the issue in question: when to use command substitution in the replacement part of the substitution and when to use the special GNU sed flag /e. Commented Sep 7, 2022 at 4:19

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.