I have tried this command in bash linux
echo "example=value" | sed "s/^example=\(.*\)$/\1/"
The output is value. But if I put it in a variable, it doesn't work.
For example:
var="example=value" | sed "s/^example=\(.*\)$/\1/"
echo $var
The output is nothing. What wrong?
Do it like this:
var=$(echo example=value | sed "s/^example=\(.*\)$/\1/")
echo $var
assigning a variable doesn't pass the value to sed via pipe.
You can pass while assigning like this:
var="example=value" && echo "$var" | sed "s/^example=\(.*\)$/\1/"
or use a sub shell like this:
var=$(echo "example=value" | sed "s/^example=\(.*\)$/\1/")
You can do this without sed, using shell parameter expansion:
$ var="example=value"
$ var="${var#*=}"
$ echo "$var"
value
Related
I have a parameter file (param.env) having the following content.
MY_PARAM=com:27}WMV\)pviZN
also, a bash file where I am fetching the value of MY_PARAM and writing into a random file.
#!/bin/bash
value=$(grep "^MY_PARAM=" param.env | cut -d '=' -f2-)
value1=$(cat param.env | grep "^MY_PARAM" | sed 's/=/ /' | awk '{print $2}')
echo $value
echo $value1
printf '%s\n' "$value"
Output:
com:27}WMV\)pviZN
com:27}WMV\)pviZN
com:27}WMV\)pviZN
However, I am expecting \ to be escaped and should not be part of the output.
I am also not allowed to edit the param.env.
Expected output:
com:27}WMV)pviZN
You could source the file, then the string will behave as if you'd assigned it like that in an interactive shell:
$ (. param.env; echo "$MY_PARAM")
com:27}WMV)pviZN
I've put the commands in a subshell so they don't pollute the environment.
I'm trying to use sed to add a variable prefix to my command...
datestamp="$(date +'%D %r %Z')"
prefix=$("$datestamp site=$i space=$number")
prefix=$("site=$i space=$number")
echo test this thing | sed 's/^/$prefix /'
I'm expecting
site=abc space=12 test this thing
Simply replace the single quotes for the sed line with double quotes, and that should do the trick.
datestamp="$(date +'%D %r %Z')"
prefix="$datestamp site=$i space=$number"
echo test this thing | sed "s#^#$prefix #"
I have a string like this:
string="aaa-bbb"
But I want to add space before char '-', so I want this:
aaa -bbb
I tried a lot of things, but I can't add space there. I tried with echo $string | tr '-' ' -', and some other stuff, but it didn't work...
I have Linux Mint: GNU bash, version 4.3.8(1)
No need to call sed, use string substitution native in BASH:
$ foo="abc-def-ghi"
$ echo "${foo//-/ -}"
abc -def -ghi
Note the two slashes after the variable name: the first slash replaces the first occurrence, where two slashes replace every occurrence.
Bash has builtin string substitution.
$ string="aaa-bbb"
$ result="${string/-/ -}"
$ echo "$result"
aaa -bbb
Alternatively, you can use sed or perl:
$ string="aaa-bbb"
$ result=$(sed 's/-/ -/' <<< $string)
$ echo "$result"
aaa -bbb
$ result=$(perl -pe 's/-/ -/' <<< $string)
$ echo "$result"
aaa -bbb
Give a try to this:
printf "%s\n" "${string}" | sed 's/-/ -/g'
It looks for - and replace it with - (space hyphen)
You are asking the shell to echo an un-quoted variable $string.
When that happens, spaces inside variables are used to split the string:
$ string="a -b -c"
$ printf '<%s>\n' $string
<a>
<-b>
<-c>
The variable does contain the spaces, just that you are not seeing it correctly.
Quote your expansions
$ printf '<%s>\n' "$string"
<a -b -c>
To get your variable changed from - to - there are many solutions:
sed: string="$(echo "$string" | sed 's/-/ -/g')"; echo "$string"
bash: string="${string//-/ -}; echo "$string"
tr can only substitute one character at a time. what you're looking for is sed:
echo "$string" | sed 's/-/ -/'
Is there simple equivalent of
sed -i '/str/d' /file
but for multiline variable?
Or I can use only following
var=`echo "$var" | sed "s/str//"`
Use
var=$(echo "$var" | sed '/str/d')
The quotes around $var in the subcommand are important for interpolating the newline characters. Otherwise $var would all be on one line.
You don't need sed, echo, and a pipe just to manipulate a string in bash:
$ echo "$var"
foo
str
bar
$ var="${var//str
}"
$ echo "$var"
foo
bar
man bash.
There is a string $STRING, in which syllables are written with the spaces. If the variable $WORD have at least one syllable in this string, report of this in any way.
Your solution checks to see if $WORD exists in $STRING when it should be the other way around. Try this:
string="run walk stand"
word=walking
if echo "$string" | sed -e 's/ /\n/g' | grep -Fqif - <(echo "$word")
then
echo "Match!"
fi
As you can see, you can test the result of the grep without having to save the output in a variable.
By the way -n is the same as ! -z.