Remove text from String

Hello All,

I am new to Bash Programming, I just started a few days ago. OK now to my Question. I want to remove a text from a string. Example:

var="Hello Bad World!"
echo $var
Output: Hello Bad World!

But i want:
Hello World!

How can i remove the word "Bad" ? I heard about sed but how does it work with words?

Thanks!

sed -i 's/Bad//g' file

Hi Coolman24,

$ var='Hello Bad World!'
$ echo $var
Hello Bad World!
$ echo $var | sed 's/[ ][^ ]*//'
Hello World!

Regards,
Birei

@birei,
do you mind explaining the expression :frowning:
thanks.

Thanks.

@anishkumarv

That works. But I cant use a String at "file"

@birei

That dont work if the string is "HelloBadWorld!"
Thanks anyway.

Hi,

the problem is, you can't use sed to replace in a file, you have to use a temporary file for this.

sed -i 's/Bad//g'  file > new file1

cat file1

Now I got it:

var="Hello Bad World!"
newvar=$var | sed 's/Bad//'
echo "$newvar"

But it just displays a blank line :frowning: