Is it possible to substitute a thing in env var with SED?
$ a='aoeua'
$ sed 's@a@o@g' <$a
bash: aoeua: No such file or directory
$ env|grep "SHELL"
SHELL=/bin/bash
The output I want is
ooeuo
replacing each a
in 'aoeua'
with o
.
The sed command is a common Linux command-line text processing utility. It's pretty convenient to process text files using this command. However, sometimes, the text we want the sed command to process is not in a file. Instead, it can be a literal string or saved in a shell variable.
Find and replace text within a file using sed command The procedure to change the text in files under Linux/Unix using sed: Use Stream EDitor (sed) as follows: sed -i 's/old-text/new-text/g' input.txt. The s is the substitute command of sed for find and replace.
sed operates on a stream of text that it reads from either a text file or from standard input (STDIN). This means that you can send the output of another command directly into sed for editing, or you can work on a file that you've already created.
This might work for you:
a='aoeua'
sed 's@a@o@g' <<<$a
ooeuo
<<<$a
is a here-string
Use echo:
$ echo "$a" | sed 's@a@o@g'
In bash you can also do simple substitutions with the ${parameter/pattern/string}
syntax. For example:
$ v='aoeua'
$ echo ${v/a/o}
ooeua
Note that this only replaces the first occurrence of the pattern.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With