Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use SED without a file with an env var?

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.

like image 868
hhh Avatar asked Jan 17 '12 23:01

hhh


People also ask

Can you use sed on a variable?

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.

How do you substitute sed?

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.

Can sed read from stdin?

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.


2 Answers

This might work for you:

a='aoeua'
sed 's@a@o@g' <<<$a
ooeuo

<<<$a is a here-string

like image 150
potong Avatar answered Oct 25 '22 05:10

potong


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.

like image 22
Laurence Gonsalves Avatar answered Oct 25 '22 05:10

Laurence Gonsalves