如何替换文件的包含新行字符的字符串的内容? [英] How to replace a file's content with a string containing new line characters?
本文介绍了如何替换文件的包含新行字符的字符串的内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我读到行1 \\ nline2 \\ nline3成一个字符串,像这样的:
When I read "line1\nline2\nline3" into a string, like this:
read string
line1\nline2\nline3
然后呼应字符串,并直接输出到一个文件:
Then echo the string and direct the output to a file:
echo $string > text.txt
TXT文件现在包含:
The txt file now contains:
line1nline2nline3
我怎能让这个文件包含:
How could I make it so that the file contains:
line1
line2
line2
感谢。
推荐答案
这里的问题是, \\ n
并不意味着换行。这只是逃避不必要的值 N
。
The problem here is that \n
does not mean line feed. It's just unnecessarily escaping the value of n
.
要做到你想要什么,你应该。
To do what you want, you should.
- 阅读在preserves反斜线方式的字符串
- 展开任何转义序列,写串出
您可以做1. 读-r
和2. 回声-e
:
You can do 1. with read -r
and 2. with echo -e
:
read -r string
echo -e "$string"
这篇关于如何替换文件的包含新行字符的字符串的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文