[英]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.