如何将sed的输出读入变量 [英] How to read output of sed into a variable
本文介绍了如何将sed的输出读入变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个值为 "abcd.txt"
的变量.
I have variable which has value "abcd.txt"
.
我想将 ".txt"
之前的所有内容存储在第二个变量中,用 ".log"
替换 ".txt"
>
I want to store everything before the ".txt"
in a second variable, replacing the ".txt"
with ".log"
回显所需的值没有问题:
I have no problem echoing the desired value:
a="abcd.txt"
echo $a | sed 's/.txt/.log/'
但是如何将 "abcd.log"
值放入第二个变量中?
But how do I get the value "abcd.log"
into the second variable?
推荐答案
您可以使用 命令替换 为:
new_filename=$(echo "$a" | sed 's/.txt/.log/')
或者不太推荐的反引号方式:
or the less recommended backtick way:
new_filename=`echo "$a" | sed 's/.txt/.log/'`
这篇关于如何将sed的输出读入变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文