Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

sed - 如何使用sed替换换行符(\ n)?(How can I replace a newline ( ) using sed?)

How can I replace a newline (" \n ") with a space ("

(如何将换行符(“ \n ”)替换为空格(“) ") using the sed command?

(“)使用sed命令?)

I unsuccessfully tried:

(我尝试失败:)

sed 's#
# #g' file
sed 's#^$# #g' file

How do I fix it?

(我如何解决它?)

  ask by hhh translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

sed is intended to be used on line-based input.

(sed旨在用于基于行的输入。)

Although it can do what you need.

(虽然它可以满足您的需求。)


A better option here is to use the tr command as follows:

(更好的选择是使用tr命令,如下所示:)

tr '
' ' ' < input_filename

or remove the newline characters entirely:

(或完全删除换行符:)

tr -d '
' < input.txt > output.txt

or if you have the GNU version (with its long options)

(或您是否拥有GNU版本(及其长选项))

tr --delete '
' < input.txt > output.txt

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...