Linux tr 命令 - 如何用实际的换行符 (\n) 替换字符串 "\n"

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13610639/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 17:54:34  来源:igfitidea点击:

tr command - how to replace the string "\n" with an actual newline (\n)

linuxgnutr

提问by Alastair

I would like to use the trcommand to replace all occurrences of the string "\n" with a new line (\n).

我想使用该tr命令用新行 (\n) 替换所有出现的字符串“\n”。

I tried tr '\\n' '\n'but this just seems to match any '\' and any 'n'

我试过了,tr '\\n' '\n'但这似乎与任何 '\' 和任何 'n' 匹配

采纳答案by sampson-chen

Here's how to do it with sed:

以下是如何做到这一点sed

sed 's/\n/\n/g'

Example usage:

用法示例:

To replace all occurrences of \nin a file in-place:

\n就地替换文件中所有出现的:

sed -i 's/\n/\n/g' input_filename

To replace all occurrences of \nthrough a pipe, and save into another file

替换所有出现的\nthrough 管道,并保存到另一个文件中

cat file1 file2 file3 file4 | sed 's/\n/\n/g' > output_file