我有一个转换文件数据的脚本,为了更有效地工作,我想更改内存中的数据,然后将其转储到文件中。 假设我想修改包含以下内容的文件:
> This is a line
> this is other line
我使用 sed 命令将“>”符号替换为“#”符号:
transform_output=$(eval "sed ${sed_args[@]} $file" 2>&1)
echo -e $transform_output
我得到输出:
# This is a line # this is other line
而不是我想要的输出是:
# This is a line
# this is other line
- 如何将输出保存在保留换行符的字符串变量中?
- 如何在包含“#ADDED LINE#”的变量开头添加一行并将其保存在文件中?
(我要获取的文件是):
#ADDED LINE#
# This is a line
# this is other line
提前致谢
最佳答案
引号用于防止在空格处分词:
echo -e "$transform_output"
将命令与另一个 echo 组合在一起:
{ echo "#ADDED LINE#"; echo -e "$transform_output"} > 文件
关于linux - 将命令输出保存在保留换行符的字符串变量中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14484466/