简体   繁体   中英

Replacing words in shell command

I'm a bit new at this so bear with me. I would like to replace certain text that is attached to a variable? or is it string? i can't remember what the dickens it's called.

delete me /from/this/here/string/

var="delete me /from/this\ here\ string/"

i tried:

echo $var | sed 's/^\(.* \)\(delete\sthis \)$/\wtf?\1/g'
echo $var | sed 's/.*\//delete this;/g'
awk ${var/123/abc};

I would like to git riddens of the 'delete me' printing. the second part always changes, but 'delete me' is always present. i'm too old for this, so thank you

Try this

> var="delete me /from/this\ here\ string/"
> echo "${var/delete me/}"
 /from/this\ here\ string/

这个怎么样?

echo $var|sed 's/\(^[^/]*\)/abc/'
var="delete me /from/this\ here\ string/"

var="$(printf '%s' "$var" | sed -e 's/^[[:blank:]]*delete[[:blank:]][[:blank:]]*[^[:blank:]]*[[:blank:]]*//' -e 's/\\//g')"

echo "$var"

My output:

/from/this here string/

It simply substitutes the word "delete" and a word following it, removing all space before and after the phrase of course. This allows it to work on "delete me", "delete this", and even "\\t \\t delete \\t \\t \\t me_too \\t \\t \\t" because a "blank" is a space or a tab character (\\t). It will only work for two words starting with "delete" at the beginning.

I hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM