繁体   English   中英

sed on Bash字符串变量

[英]sed on a Bash string variable

我有一个变量:

temp='Some text \n Some words \n'

我想用sed删除其中一些行:

sed -e '1d' $temp 

我想知道这是否可行。

当您将字符串作为参数传递时, sed将解释为文件名或文件名列表:

sed -e '1d' "$temp"

当然,那不是你想要的。

你需要在这里使用字符串<<<而不是:

temp=$'Some text\nSome words\nLast word'
sed '1d' <<< "$temp"

输出:

Some words
Last word

与posix兼容的方法是:

temp="$(printf '%s\n' "$temp" | sed '1d')"

如果你只需要一个与bash兼容的解决方案,请参阅codeforester的答案 ,因为这里的字符串语法更易读。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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