如何使用sed替换新行(\n) ?

时间:2021-02-03 15:28:58

How can I replace a newline (\n) using the sed command?

如何使用sed命令替换新行(\n) ?

I unsuccessfully tried:

我失败尝试:

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

How do I fix it?

我怎么修理它?

38 个解决方案

#1


1236  

Use this solution with GNU sed:

使用GNU sed的这个解决方案:

sed ':a;N;$!ba;s/\n/ /g' file

This will read the whole file in a loop, then replaces the newline(s) with a space.

这将在一个循环中读取整个文件,然后用空格替换换行符。

Explanation:

解释:

  1. Create a label via :a.
  2. 通过:a创建一个标签。
  3. Append the current and next line to the pattern space via N.
  4. 通过N将当前和下一行添加到模式空间。
  5. If we are before the last line, branch to the created label $!ba ($! means not to do it on the last line as there should be one final newline).
  6. 如果我们在最后一行之前,分支到创建的标签$!ba($ !意思是不要在最后一行做,因为应该有一个最后的换行。
  7. Finally the substitution replaces every newline with a space on the pattern space (which is the whole file).
  8. 最后,替换用模式空间上的空间替换了每个换行(这是整个文件)。

Here is cross-platform compatible syntax which works with BSD and OS X's sed (as per @Benjie comment):

下面是与BSD和OS X的sed兼容的跨平台兼容语法(如@Benjie注释):

sed -e ':a' -e 'N' -e '$!ba' -e 's/\n/ /g' file

#2


1392  

Use tr instead?

使用tr呢?

tr '\n' ' ' < input_filename

or remove the newline characters entirely:

或者完全删除换行符:

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

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

或者如果你有GNU版本(有很长的选项)

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

#3


406  

Fast answer:

快速回答:

sed ':a;N;$!ba;s/\n/ /g' file
  1. :a create a label 'a'
  2. :a创建标签'a'
  3. N append the next line to the pattern space
  4. 将下一行附加到模式空间。
  5. $! if not the last line, ba branch (go to) label 'a'
  6. 美元!如果不是最后一行,ba branch (go to)标签为a
  7. s substitute, /\n/ regex for new line, / / by a space, /g global match (as many times as it can)
  8. s替代,/\n/ regex为新行,/ /通过一个空格,/g全球匹配(尽可能多的时间)

sed will loop through step 1 to 3 until it reach the last line, getting all lines fit in the pattern space where sed will substitute all \n characters

sed将遍历步骤1到3,直到它到达最后一行,使所有行都适合于sed将替代所有\n字符的模式空间。


Alternatives:

选择:

All alternatives, unlike sed will not need to reach the last line to begin the process

与sed不同,所有备选方案都不需要到达最后一行才能开始流程。

with bash, slow

bash,慢

while read line; do printf "%s" "$line "; done < file

with perl, sed-like speed

用perl,sed-like速度

perl -p -e 's/\n/ /' file

with tr, faster than sed, can replace by one character only

tr比sed快,只能由一个字符代替。

tr '\n' ' ' < file

with paste, tr-like speed, can replace by one character only

用浆糊,像tr一样的速度,只能由一个字符代替。

paste -s -d ' ' file

with awk, tr-like speed

awk,tr-like速度

awk 1 ORS=' ' file

Other alternative like "echo $(< file)" is slow, works only on small files and needs to process the whole file to begin the process.

其他类似“echo $(< file)”的选项很慢,只在小文件上运行,需要处理整个文件以开始进程。


Long answer from the sed FAQ 5.10:

《sed FAQ》的长回答:

5.10. Why can't I match or delete a newline using the \n escape
sequence? Why can't I match 2 or more lines using \n?

5.10。为什么我不能使用\n转义序列匹配或删除一条新行?为什么我不能用\n匹配两个或更多的线?

The \n will never match the newline at the end-of-line because the
newline is always stripped off before the line is placed into the
pattern space. To get 2 or more lines into the pattern space, use
the 'N' command or something similar (such as 'H;...;g;').

\n将永远不会与行尾的换行符匹配,因为在将行放入模式空间之前,换行总是被删除。要在模式空间中获得2个或更多的行,可以使用“N”命令或类似的东西(例如“H;…;g;”)。

Sed works like this: sed reads one line at a time, chops off the
terminating newline, puts what is left into the pattern space where
the sed script can address or change it, and when the pattern space
is printed, appends a newline to stdout (or to a file). If the
pattern space is entirely or partially deleted with 'd' or 'D', the
newline is not added in such cases. Thus, scripts like

Sed的工作如下:Sed每次只读取一行代码,砍掉终止的新行,将剩下的内容放到Sed脚本可以处理或更改的模式空间中,并且在打印模式空间时,将一条新行添加到stdout(或文件)中。如果模式空间全部或部分被“d”或“d”删除,则在这种情况下不会添加新行。因此,脚本就像

  sed 's/\n//' file       # to delete newlines from each line             
  sed 's/\n/foo\n/' file  # to add a word to the end of each line         

will NEVER work, because the trailing newline is removed before
the line is put into the pattern space. To perform the above tasks,
use one of these scripts instead:

将永远不会起作用,因为在将行放入模式空间之前,拖放的新行被删除。要执行上述任务,请使用以下脚本之一:

  tr -d '\n' < file              # use tr to delete newlines              
  sed ':a;N;$!ba;s/\n//g' file   # GNU sed to delete newlines             
  sed 's/$/ foo/' file           # add "foo" to end of each line          

Since versions of sed other than GNU sed have limits to the size of
the pattern buffer, the Unix 'tr' utility is to be preferred here.
If the last line of the file contains a newline, GNU sed will add
that newline to the output but delete all others, whereas tr will
delete all newlines.

由于除了GNU sed之外的sed版本对模式缓冲区的大小有限制,Unix 'tr'实用程序在这里是首选。如果文件的最后一行包含一条新行,GNU sed将向输出添加新行,但删除所有其他行,而tr将删除所有新行。

To match a block of two or more lines, there are 3 basic choices:
(1) use the 'N' command to add the Next line to the pattern space;
(2) use the 'H' command at least twice to append the current line
to the Hold space, and then retrieve the lines from the hold space
with x, g, or G; or (3) use address ranges (see section 3.3, above)
to match lines between two specified addresses.

为了匹配两个或更多行的块,有三个基本的选择:(1)使用“N”命令将下一行添加到模式空间;(2)使用“H”命令至少两次将当前行追加到存储空间,然后用x、g、g等方式从存储空间中检索线路;或者(3)使用地址范围(见上文第3.3节),以匹配两个指定地址之间的行。

Choices (1) and (2) will put an \n into the pattern space, where it
can be addressed as desired ('s/ABC\nXYZ/alphabet/g'). One example
of using 'N' to delete a block of lines appears in section 4.13
("How do I delete a block of specific consecutive lines?"). This
example can be modified by changing the delete command to something
else, like 'p' (print), 'i' (insert), 'c' (change), 'a' (append),
or 's' (substitute).

选择(1)和(2)将把一个\n放入模式空间中,可以按需要处理(s/ABC\nXYZ/字母/g)。在第4.13节(“我如何删除一个特定的连续行?”)中,使用“N”删除一条行的一个例子。这个例子可以通过将delete命令更改为其他东西来修改,比如“p”(print)、“i”(插入)、“c”(更改)、“a”(append)或“s”(替换)。

Choice (3) will not put an \n into the pattern space, but it does
match a block of consecutive lines, so it may be that you don't
even need the \n to find what you're looking for. Since GNU sed
version 3.02.80 now supports this syntax:

选择(3)不会将一个\n放入模式空间,但是它确实匹配一个连续的行,所以可能您甚至不需要\n来找到您要找的东西。由于GNU sed版本3.02.80现在支持此语法:

  sed '/start/,+4d'  # to delete "start" plus the next 4 lines,           

in addition to the traditional '/from here/,/to there/{...}' range
addresses, it may be possible to avoid the use of \n entirely.

除了传统的/从这里/那里/到那里/{…range地址,可以完全避免使用\n。

#4


170  

A shorter awk alternative:

更短的awk选择:

awk 1 ORS=' '

Explanation

An awk program is built up of rules which consist of conditional code-blocks, i.e.:

一个awk程序是由包含有条件代码块的规则组成的,即:

condition { code-block }

If the code-block is omitted, the default is used: { print $0 }. Thus, the 1 is interpreted as a true condition and print $0 is executed for each line.

如果省略了代码块,则使用默认值:{print $0}。因此,将1解释为一个真实的条件,并为每一行执行打印$0。

When awk reads the input it splits it into records based on the value of RS (Record Separator), which by default is a newline, thus awk will by default parse the input line-wise. The splitting also involves stripping off RS from the input record.

当awk读取输入时,它根据RS(记录分隔符)的值将其分割成记录,默认情况下是一条新行,因此awk将默认解析输入行。拆分还包括从输入记录中剥离RS。

Now, when printing a record, ORS (Output Record Separator) is appended to it (default is again a newline). So by changing ORS to a space all newlines are changed to spaces.

现在,当打印一个记录时,ORS(输出记录分隔符)被附加到它(默认是一个换行符)。所以通过改变空间到一个空间,所有的换行都变成了空间。

#5


74  

The Perl version works the way you expected.

Perl版本按照您的预期运行。

perl -i -p -e 's/\n//' file

As pointed out in the comments, it's worth noting that this edits in place. -i.bak will give you a backup of the original file before the replacement in case your regular expression isn't as smart as you thought.

正如在评论中指出的那样,值得注意的是,这些编辑已经到位。-我。当你的正则表达式没有你想的那么聪明时,bak会在替换之前给你一个原始文件的备份。

#6


72  

gnu sed has an option -z for null separated records (lines). You can just call:

gnu sed有一个选项-z用于null分隔的记录(行)。你可以叫:

sed -z 's/\n/ /g'

#7


38  

Who needs sed? Here is the bash way:

谁需要对话?这里是bash方法:

cat test.txt |  while read line; do echo -n "$line "; done

#8


22  

In order to replace all newlines with spaces using awk, without reading the whole file into memory:

为了用awk替换所有的新行,而不将整个文件读入内存:

awk '{printf "%s ", $0}' inputfile

If you want a final newline:

如果你想要最后的换行:

awk '{printf "%s ", $0} END {printf "\n"}' inputfile

You can use a character other than space:

你可以使用除空间以外的字符:

awk '{printf "%s|", $0} END {printf "\n"}' inputfile

#9


20  

Three things.

三件事。

  1. tr (or cat, etc.) is absolutely not needed. (GNU) sed and (GNU) awk, when combined, can do 99.9% of any text processing you need.

    tr(或cat等)绝对不需要。(GNU) sed和(GNU) awk结合起来,可以完成您需要的任何文本处理的99.9%。

  2. stream != line based. ed is a line-based editor. sed is not. See sed lecture for more information on the difference. Most people confuse sed to be line-based because it is, by default, not very greedy in its pattern matching for SIMPLE matches - for instance, when doing pattern searching and replacing by one or two characters, it by default only replaces on the first match it finds (unless specified otherwise by the global command). There would not even be a global command if it were line-based rather than STREAM-based, because it would evaluate only lines at a time. Try running ed; you'll notice the difference. ed is pretty useful if you want to iterate over specific lines (such as in a for-loop), but most of the times you'll just want sed.

    基于流! =线。ed是一个基于行的编辑器。sed不是。有关差异的更多信息,请参阅sed讲座。大多数人混淆sed基于行的,因为它是,默认情况下,不是很贪婪模式匹配的简单匹配——例如,当进行模式搜索和替换一个或两个字符,默认情况下只替换第一个匹配它发现(除非指定否则全球命令)。如果它是基于行的,而不是基于流的,甚至不会有一个全局命令,因为它只会一次评估行。尝试运行ed;你会注意到差别。如果您希望迭代特定的行(比如for循环),那么ed非常有用,但是大多数情况下您只需要sed。

  3. That being said,

    话虽这么说,

    sed -e '{:q;N;s/\n/ /g;t q}' file
    

    works just fine in GNU sed version 4.2.1. The above command will replace all newlines with spaces. It's ugly and a bit cumbersome to type in, but it works just fine. The {}'s can be left out, as they're only included for sanity reasons.

    在GNU sed版本4.2.1中运行良好。上面的命令将替换所有带有空格的新行。打字很难看,而且有点麻烦,但效果很好。{}s可以省略,因为它们只包含在正常的原因中。

#10


18  

tr '\n' ' ' 

is the command.

是命令。

Simple and easy to use.

简单易用。

#11


12  

The answer with the :a label ...

答案是:标签……

How can I replace a newline (\n) using sed?

如何使用sed替换新行(\n) ?

... does not work in freebsd 7.2 on the command line:

…不工作在freebsd 7.2的命令行:

( echo foo ; echo bar ) | sed ':a;N;$!ba;s/\n/ /g'
sed: 1: ":a;N;$!ba;s/\n/ /g": unused label 'a;N;$!ba;s/\n/ /g'
foo
bar

But does if you put the sed script in a file or use -e to "build" the sed script...

但是,如果您将sed脚本放在一个文件中,或者使用-e来“构建”sed脚本……

> (echo foo; echo bar) | sed -e :a -e N -e '$!ba' -e 's/\n/ /g'
foo bar

or ...

还是……

> cat > x.sed << eof
:a
N
$!ba
s/\n/ /g
eof

> (echo foo; echo bar) | sed -f x.sed
foo bar

Maybe the sed in OS X is similar.

也许在OS X中的sed是相似的。

#12


10  

You can use xargs:

您可以使用xargs:

seq 10 | xargs

or

seq 10 | xargs echo -n

#13


9  

I'm not an expert, but I guess in sed you'd first need to append the next line into the pattern space, bij using "N". From the section "Multiline Pattern Space" in "Advanced sed Commands" of the book sed & awk (Dale Dougherty and Arnold Robbins; O'Reilly 1997; page 107 in the preview):

我不是专家,但我猜在sed中,您首先需要将下一行添加到模式空间中,bij使用“N”。从《sed & awk》(戴尔·多尔蒂和阿诺德·罗宾斯)的《高级sed命令》中的“多行模式空间”一节中;O ' reilly 1997;第107页的预览):

The multiline Next (N) command creates a multiline pattern space by reading a new line of input and appending it to the contents of the pattern space. The original contents of pattern space and the new input line are separated by a newline. The embedded newline character can be matched in patterns by the escape sequence "\n". In a multiline pattern space, the metacharacter "^" matches the very first character of the pattern space, and not the character(s) following any embedded newline(s). Similarly, "$" matches only the final newline in the pattern space, and not any embedded newline(s). After the Next command is executed, control is then passed to subsequent commands in the script.

multiline Next (N)命令通过读取新的输入行并将其附加到模式空间的内容,从而创建多行模式空间。模式空间的原始内容和新的输入行由一条换行分隔。嵌入的换行符可以根据转义序列“\n”匹配。多行模式空间,元字符“^”匹配的第一个字符模式空间,而不是字符(s)任何嵌入后换行。类似地,“$”只匹配模式空间中的最后一条换行,而不是任何嵌入的换行符。在执行下一个命令之后,将控制权传递给脚本中的后续命令。

From man sed:

从人对话:

[2addr]N

[2 addr]N

Append the next line of input to the pattern space, using an embedded newline character to separate the appended material from the original contents. Note that the current line number changes.

将下一行输入到模式空间中,使用嵌入式换行符将附加的内容与原始内容分离。注意,当前行号更改。

I've used this to search (multiple) badly formatted log files, in which the search string may be found on an "orphaned" next line.

我用这个来搜索(多个)格式糟糕的日志文件,其中的搜索字符串可能会在“孤立”的下一行中找到。

#14


7  

Easy-to-understand Solution

I had this problem. The kicker was that I needed the solution to work on BSD's (Mac OS X) and GNU's (Linux and Cygwin) sed and tr:

我有这个问题。更重要的是,我需要解决BSD (Mac OS X)和GNU (Linux和Cygwin) sed和tr的解决方案:

$ echo 'foo
bar
baz


foo2
bar2
baz2' \
| tr '\n' '\000' \
| sed 's:\x00\x00.*:\n:g' \
| tr '\000' '\n'

Output:

输出:

foo
bar
baz

(has trailing newline)

(已落后于换行符)

It works on Linux, OS X, and BSD - even without UTF-8 support or with a crappy terminal.

它在Linux、OS X和BSD上运行,即使没有UTF-8支持,也没有一个蹩脚的终端。

  1. Use tr to swap the newline with another character.

    使用tr将换行符替换为另一个字符。

    NULL (\000 or \x00) is nice because it doesn't need UTF-8 support and it's not likely to be used.

    NULL(\000或\x00)很好,因为它不需要UTF-8支持,也不太可能被使用。

  2. Use sed to match the NULL

    使用sed匹配NULL。

  3. Use tr to swap back extra newlines if you need them

    如果你需要的话,可以用tr来交换多余的换行。

#15


6  

In response to the "tr" solution above, on Windows (probably using the Gnuwin32 version of tr), the proposed solution:

针对以上的“tr”解决方案,在Windows上(可能使用的是Gnuwin32版本的tr),建议的解决方案是:

tr '\n' ' ' < input

was not working for me, it'd either error or actually replace the \n w/ '' for some reason.

它不是为我工作的,它要么是错误的,要么是由于某种原因而替代了\n w/ "。

Using another feature of tr, the "delete" option -d did work though:

使用tr的另一个特性,“delete”选项-d确实有效:

tr -d '\n' < input

or '\r\n' instead of '\n'

或者'\r\n'而不是'\n'

#16


6  

I used a hybrid approach to get around the newline thing by using tr to replace newlines with tabs, then replacing tabs with whatever I want. In this case, "
" since I'm trying to generate HTML breaks.

我使用了一种混合的方法来绕过换行,用tr替换带有标签的换行符,然后用我想要的任何东西替换制表符。在这种情况下,“因为我正在尝试生成HTML中断。”

echo -e "a\nb\nc\n" |tr '\n' '\t' | sed 's/\t/ <br> /g'`

#17


5  

In some situations maybe you can change RS to some other string or character. This way, \n is available for sub/gsub:

在某些情况下,也许你可以把RS换成其他字符串或字符。这种方式,\n可用于sub/gsub:

$ gawk 'BEGIN {RS="dn" } {gsub("\n"," ") ;print $0 }' file

The power of shell scripting is that if you do not know how to do it in one way you can do it in another way. And many times you have more things to take into account than make a complex solution on a simple problem.

shell脚本的强大之处在于,如果您不知道如何用一种方法来完成它,您可以用另一种方法来完成它。很多时候,你有更多的事情要考虑,而不是在一个简单的问题上做一个复杂的解决方案。

Regarding the thing that gawk is slow... and reads the file into memory, I do not know this, but to me gawk seems to work with one line at the time and is very very fast (not that fast as some of the others, but the time to write and test also counts).

关于gawk慢的事情……将文件读入内存,我不知道这一点,但对我来说,gawk似乎是在一段时间内工作,而且速度非常非常快(不像其他一些那样快,但是写和测试的时间也很重要)。

I process MB and even GB of data, and the only limit I found is line size.

我处理MB甚至是GB的数据,我发现的唯一限制是行大小。

#18


4  

You could use xargs — it will replace \n with a space by default.

您可以使用xargs -它将在默认情况下替换\n。

However, it would have problems if your input has any case of an unterminated quote, e.g. if the quote signs on a given line don't match.

但是,如果您的输入有任何未终止的引用,则会出现问题,例如,如果在给定的行上的引用符号不匹配。

#19


4  

Bullet-proof solution. Binary-data-safe and POSIX-compliant, but slow.

POSIX sed requires input according to the POSIX text file and POSIX line definitions, so NULL-bytes and too long lines are not allowed and each line must end with a newline (including the last line). This makes it hard to use sed for processing arbitrary input data.

POSIX sed需要根据POSIX文本文件和POSIX行定义进行输入,因此不允许空字节和太长的行,并且每一行必须以一条换行符(包括最后一行)结束。这使得它很难用于处理任意输入数据。

The following solution avoids sed and instead converts the input bytes to octal codes and then to bytes again, but intercepts octal code 012 (newline) and outputs the replacement string in place of it. As far as I can tell the solution is POSIX-compliant, so it should work on a wide variety of platforms.

下面的解决方案避免了sed,而是将输入字节转换为八进制码,然后再转换为字节,但是拦截octal代码012(换行)并输出替代字符串。就我所知,解决方案是posix兼容的,所以它应该在各种各样的平台上工作。

od -A n -t o1 -v | tr ' \t' '\n\n' | grep . |
  while read x; do [ "0$x" -eq 012 ] && printf '<br>\n' || printf "\\$x"; done

POSIX reference documentation: sh, shell command language, od, tr, grep, read, [, printf.

POSIX参考文档:sh, shell命令语言,od, tr, grep, read, [, printf。

Both read, [, and printf are built-ins in at least bash, but that is probably not guaranteed by POSIX, so on some platforms it could be that each input byte will start one or more new processes, which will slow things down. Even in bash this solution only reaches about 50 kB/s, so it's not suited for large files.

两者都读,[,和printf在至少bash中都是内置的,但这可能不是POSIX所保证的,所以在某些平台上,可能每个输入字节都将启动一个或多个新进程,这将使事情慢下来。即使在bash中,这个解决方案也只能达到大约50 kB/s,因此它不适合大型文件。

Tested on Ubuntu (bash, dash, and busybox), FreeBSD, and OpenBSD.

测试了Ubuntu (bash、dash、busybox)、FreeBSD和OpenBSD。

#20


3  

On Mac OS X (using FreeBSD sed):

在Mac OS X上(使用FreeBSD sed):

# replace each newline with a space
printf "a\nb\nc\nd\ne\nf" | sed -E -e :a -e '$!N; s/\n/ /g; ta'
printf "a\nb\nc\nd\ne\nf" | sed -E -e :a -e '$!N; s/\n/ /g' -e ta

#21


3  

Using Awk:

使用Awk:

awk "BEGIN { o=\"\" }  { o=o \" \" \$0 }  END { print o; }"

#22


3  

A solution I particularly like is to append all the file in the hold space and replace all newlines at the end of file:

我特别喜欢的一个解决方案是在保存空间中追加所有文件,并在文件末尾替换所有换行:

$ (echo foo; echo bar) | sed -n 'H;${x;s/\n//g;p;}'
foobar

However, someone said me the hold space can be finite in some sed implementations.

然而,有人说,在某些sed实现中,存储空间是有限的。

#23


3  

Replace newlines with any string, and replace the last newline too

The pure tr solutions can only replace with a single character, and the pure sed solutions don't replace the last newline of the input. The following solution fixes these problems, and seems to be safe for binary data (even with a UTF-8 locale):

纯tr解决方案只能用一个字符来替换,而纯sed解决方案不能替代输入的最后一行。下面的解决方案解决了这些问题,而且对于二进制数据(即使使用UTF-8语言环境)似乎是安全的:

printf '1\n2\n3\n' |
  sed 's/%/%p/g;s/@/%a/g' | tr '\n' @ | sed 's/@/<br>/g;s/%a/@/g;s/%p/%/g'

Result:

结果:

1<br>2<br>3<br>

#24


3  

It is sed that introduces the new-lines after "normal" substitution. First, it trims the new-line char, then it processes according to your instructions, then it introduces a new-line.

介绍了“正常”替代后的新线路。首先,它对新行char进行测试,然后根据你的指令进行处理,然后引入一条新行。

Using sed you can replace "the end" of a line (not the new-line char) after being trimmed, with a string of your choice, for each input line; but, sed will output different lines. For example, suppose you wanted to replace the "end of line" with "===" (more general than a replacing with a single space):

使用sed可以将行(而不是新行char)的“末尾”替换为每个输入行,并为每个输入行指定一个字符串;但是,sed将输出不同的行。例如,假设您想用“===”替换“行结束”(比用一个空格替换更通用):

PROMPT~$ cat <<EOF |sed 's/$/===/g'
first line
second line
3rd line
EOF

first line===
second line===
3rd line===
PROMPT~$

To replace the new-line char with the string, you can, inefficiently though, use tr , as pointed before, to replace the newline-chars with a "special char" and then use sed to replace that special char with the string you want.

要用字符串替换新行char,您可以使用tr(如前所述),用“特殊char”替换新行字符,然后使用sed替换您想要的字符串。

For example:

例如:

PROMPT~$ cat <<EOF | tr '\n' $'\x01'|sed -e 's/\x01/===/g'
first line
second line
3rd line
EOF

first line===second line===3rd line===PROMPT~$

#25


2  

@OP, if you want to replace newlines in a file, you can just use dos2unix (or unix2dox)

@OP,如果您想在文件中替换换行,您可以使用dos2unix(或unix2dox)

dos2unix yourfile yourfile

#26


2  

To remove empty lines:

删除空行:

sed -n "s/^$//;t;p;"

#27


2  

You can use this method also

你也可以用这个方法。

sed 'x;G;1!h;s/\n/ /g;$!d'

Explanation

解释

x   - which is used to exchange the data from both space (pattern and hold).
G   - which is used to append the data from hold space to pattern space.
h   - which is used to copy the pattern space to hold space.
1!h - During first line won't copy pattern space to hold space due to \n is
      available in pattern space.
$!d - Clear the pattern space every time before getting next line until the
      last line.

Flow:
When the first line get from the input, exchange is made, so 1 goes to hold space and \n comes to pattern space, then appending the hold space to pattern space, and then substitution is performed and deleted the pattern space.
During the second line exchange is made, 2 goes to hold space and 1 comes to pattern space, then G append the hold space into the pattern space, then h copy the pattern to it and substitution is made and deleted. This operation is continued until eof is reached then print exact result.

流:当第一行从输入中得到时,交换就生成了,所以1去容纳空间,\n进入模式空间,然后将容纳空间附加到模式空间,然后执行替换,并删除模式空间。在第二行交换中,有2个保持空间,1个进入模式空间,然后G在模式空间中添加容纳空间,然后h复制模式到它,并进行替换和删除。这个操作一直持续到eof到达,然后打印精确的结果。

#28


1  

sed '1h;1!H;$!d
     x;s/\n/ /g' YourFile

This does not work for huge files (buffer limit), but it is very efficient if there is enough memory to hold the file. (Correction H-> 1h;1!H after the good remark of @hilojack )

这并不适用于大文件(缓冲区限制),但是如果有足够的内存来保存文件,那么它是非常有效的。(校正H - > 1 H;1 !H在@hilojack的好评之后

Another version that change new line while reading (more cpu, less memory)

另一个在读取时更改新行的版本(更多的cpu,更少的内存)

 sed ':loop
 $! N
 s/\n/ /
 t loop' YourFile

#29


1  

Another sed method, almost the same as Zsolt Botykai's answer, but this uses sed's less-frequently used y (transliterate) command, which saves one byte of code (the trailing 'g'):

另一种sed方法,几乎与Zsolt Botykai的答案相同,但它使用sed较少使用的y (transliterate)命令,它节省了一个字节的代码(后面的“g”):

sed ':a;N;$!ba;y/\n/ /'

One would hope y would run faster than s, (perhaps at tr speeds, 20x faster), but in GNU sed v4.2.2 y is about 4% slower than s.

一个人希望y跑得比s快,(也许速度快20倍),但在GNU sed v4.2.2 y中比s慢4%。

#30


1  

I posted this answer because I have tried with most of the sed commend example provided above which does not work for me in my Unix box and giving me error message Label too long: {:q;N;s/\n/ /g;t q}. Finally I made my requirement and hence shared here which works in all Unix/Linux environment:-

我发布了这个答案,因为我已经尝试了上面提供的大多数sed推荐示例,这些示例在我的Unix框中不起作用,并且给我的错误消息标签太长:{:q;N;s/\ N /g;t q}。最后,我提出了我的要求,并在这里共享了在所有Unix/Linux环境中工作的内容:-。

line=$(while read line; do echo -n "$line "; done < yoursourcefile.txt)
echo $line |sed 's/ //g' > sortedoutput.txt

The first line will remove all the new line from file yoursourcefile.txt and will produce a single line. And second sed command will remove all the spaces from it.

第一行将删除文件yoursourcefile中的所有新行。txt将生成一行。第二个sed命令将删除它的所有空格。

#1


1236  

Use this solution with GNU sed:

使用GNU sed的这个解决方案:

sed ':a;N;$!ba;s/\n/ /g' file

This will read the whole file in a loop, then replaces the newline(s) with a space.

这将在一个循环中读取整个文件,然后用空格替换换行符。

Explanation:

解释:

  1. Create a label via :a.
  2. 通过:a创建一个标签。
  3. Append the current and next line to the pattern space via N.
  4. 通过N将当前和下一行添加到模式空间。
  5. If we are before the last line, branch to the created label $!ba ($! means not to do it on the last line as there should be one final newline).
  6. 如果我们在最后一行之前,分支到创建的标签$!ba($ !意思是不要在最后一行做,因为应该有一个最后的换行。
  7. Finally the substitution replaces every newline with a space on the pattern space (which is the whole file).
  8. 最后,替换用模式空间上的空间替换了每个换行(这是整个文件)。

Here is cross-platform compatible syntax which works with BSD and OS X's sed (as per @Benjie comment):

下面是与BSD和OS X的sed兼容的跨平台兼容语法(如@Benjie注释):

sed -e ':a' -e 'N' -e '$!ba' -e 's/\n/ /g' file

#2


1392  

Use tr instead?

使用tr呢?

tr '\n' ' ' < input_filename

or remove the newline characters entirely:

或者完全删除换行符:

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

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

或者如果你有GNU版本(有很长的选项)

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

#3


406  

Fast answer:

快速回答:

sed ':a;N;$!ba;s/\n/ /g' file
  1. :a create a label 'a'
  2. :a创建标签'a'
  3. N append the next line to the pattern space
  4. 将下一行附加到模式空间。
  5. $! if not the last line, ba branch (go to) label 'a'
  6. 美元!如果不是最后一行,ba branch (go to)标签为a
  7. s substitute, /\n/ regex for new line, / / by a space, /g global match (as many times as it can)
  8. s替代,/\n/ regex为新行,/ /通过一个空格,/g全球匹配(尽可能多的时间)

sed will loop through step 1 to 3 until it reach the last line, getting all lines fit in the pattern space where sed will substitute all \n characters

sed将遍历步骤1到3,直到它到达最后一行,使所有行都适合于sed将替代所有\n字符的模式空间。


Alternatives:

选择:

All alternatives, unlike sed will not need to reach the last line to begin the process

与sed不同,所有备选方案都不需要到达最后一行才能开始流程。

with bash, slow

bash,慢

while read line; do printf "%s" "$line "; done < file

with perl, sed-like speed

用perl,sed-like速度

perl -p -e 's/\n/ /' file

with tr, faster than sed, can replace by one character only

tr比sed快,只能由一个字符代替。

tr '\n' ' ' < file

with paste, tr-like speed, can replace by one character only

用浆糊,像tr一样的速度,只能由一个字符代替。

paste -s -d ' ' file

with awk, tr-like speed

awk,tr-like速度

awk 1 ORS=' ' file

Other alternative like "echo $(< file)" is slow, works only on small files and needs to process the whole file to begin the process.

其他类似“echo $(< file)”的选项很慢,只在小文件上运行,需要处理整个文件以开始进程。


Long answer from the sed FAQ 5.10:

《sed FAQ》的长回答:

5.10. Why can't I match or delete a newline using the \n escape
sequence? Why can't I match 2 or more lines using \n?

5.10。为什么我不能使用\n转义序列匹配或删除一条新行?为什么我不能用\n匹配两个或更多的线?

The \n will never match the newline at the end-of-line because the
newline is always stripped off before the line is placed into the
pattern space. To get 2 or more lines into the pattern space, use
the 'N' command or something similar (such as 'H;...;g;').

\n将永远不会与行尾的换行符匹配,因为在将行放入模式空间之前,换行总是被删除。要在模式空间中获得2个或更多的行,可以使用“N”命令或类似的东西(例如“H;…;g;”)。

Sed works like this: sed reads one line at a time, chops off the
terminating newline, puts what is left into the pattern space where
the sed script can address or change it, and when the pattern space
is printed, appends a newline to stdout (or to a file). If the
pattern space is entirely or partially deleted with 'd' or 'D', the
newline is not added in such cases. Thus, scripts like

Sed的工作如下:Sed每次只读取一行代码,砍掉终止的新行,将剩下的内容放到Sed脚本可以处理或更改的模式空间中,并且在打印模式空间时,将一条新行添加到stdout(或文件)中。如果模式空间全部或部分被“d”或“d”删除,则在这种情况下不会添加新行。因此,脚本就像

  sed 's/\n//' file       # to delete newlines from each line             
  sed 's/\n/foo\n/' file  # to add a word to the end of each line         

will NEVER work, because the trailing newline is removed before
the line is put into the pattern space. To perform the above tasks,
use one of these scripts instead:

将永远不会起作用,因为在将行放入模式空间之前,拖放的新行被删除。要执行上述任务,请使用以下脚本之一:

  tr -d '\n' < file              # use tr to delete newlines              
  sed ':a;N;$!ba;s/\n//g' file   # GNU sed to delete newlines             
  sed 's/$/ foo/' file           # add "foo" to end of each line          

Since versions of sed other than GNU sed have limits to the size of
the pattern buffer, the Unix 'tr' utility is to be preferred here.
If the last line of the file contains a newline, GNU sed will add
that newline to the output but delete all others, whereas tr will
delete all newlines.

由于除了GNU sed之外的sed版本对模式缓冲区的大小有限制,Unix 'tr'实用程序在这里是首选。如果文件的最后一行包含一条新行,GNU sed将向输出添加新行,但删除所有其他行,而tr将删除所有新行。

To match a block of two or more lines, there are 3 basic choices:
(1) use the 'N' command to add the Next line to the pattern space;
(2) use the 'H' command at least twice to append the current line
to the Hold space, and then retrieve the lines from the hold space
with x, g, or G; or (3) use address ranges (see section 3.3, above)
to match lines between two specified addresses.

为了匹配两个或更多行的块,有三个基本的选择:(1)使用“N”命令将下一行添加到模式空间;(2)使用“H”命令至少两次将当前行追加到存储空间,然后用x、g、g等方式从存储空间中检索线路;或者(3)使用地址范围(见上文第3.3节),以匹配两个指定地址之间的行。

Choices (1) and (2) will put an \n into the pattern space, where it
can be addressed as desired ('s/ABC\nXYZ/alphabet/g'). One example
of using 'N' to delete a block of lines appears in section 4.13
("How do I delete a block of specific consecutive lines?"). This
example can be modified by changing the delete command to something
else, like 'p' (print), 'i' (insert), 'c' (change), 'a' (append),
or 's' (substitute).

选择(1)和(2)将把一个\n放入模式空间中,可以按需要处理(s/ABC\nXYZ/字母/g)。在第4.13节(“我如何删除一个特定的连续行?”)中,使用“N”删除一条行的一个例子。这个例子可以通过将delete命令更改为其他东西来修改,比如“p”(print)、“i”(插入)、“c”(更改)、“a”(append)或“s”(替换)。

Choice (3) will not put an \n into the pattern space, but it does
match a block of consecutive lines, so it may be that you don't
even need the \n to find what you're looking for. Since GNU sed
version 3.02.80 now supports this syntax:

选择(3)不会将一个\n放入模式空间,但是它确实匹配一个连续的行,所以可能您甚至不需要\n来找到您要找的东西。由于GNU sed版本3.02.80现在支持此语法:

  sed '/start/,+4d'  # to delete "start" plus the next 4 lines,           

in addition to the traditional '/from here/,/to there/{...}' range
addresses, it may be possible to avoid the use of \n entirely.

除了传统的/从这里/那里/到那里/{…range地址,可以完全避免使用\n。

#4


170  

A shorter awk alternative:

更短的awk选择:

awk 1 ORS=' '

Explanation

An awk program is built up of rules which consist of conditional code-blocks, i.e.:

一个awk程序是由包含有条件代码块的规则组成的,即:

condition { code-block }

If the code-block is omitted, the default is used: { print $0 }. Thus, the 1 is interpreted as a true condition and print $0 is executed for each line.

如果省略了代码块,则使用默认值:{print $0}。因此,将1解释为一个真实的条件,并为每一行执行打印$0。

When awk reads the input it splits it into records based on the value of RS (Record Separator), which by default is a newline, thus awk will by default parse the input line-wise. The splitting also involves stripping off RS from the input record.

当awk读取输入时,它根据RS(记录分隔符)的值将其分割成记录,默认情况下是一条新行,因此awk将默认解析输入行。拆分还包括从输入记录中剥离RS。

Now, when printing a record, ORS (Output Record Separator) is appended to it (default is again a newline). So by changing ORS to a space all newlines are changed to spaces.

现在,当打印一个记录时,ORS(输出记录分隔符)被附加到它(默认是一个换行符)。所以通过改变空间到一个空间,所有的换行都变成了空间。

#5


74  

The Perl version works the way you expected.

Perl版本按照您的预期运行。

perl -i -p -e 's/\n//' file

As pointed out in the comments, it's worth noting that this edits in place. -i.bak will give you a backup of the original file before the replacement in case your regular expression isn't as smart as you thought.

正如在评论中指出的那样,值得注意的是,这些编辑已经到位。-我。当你的正则表达式没有你想的那么聪明时,bak会在替换之前给你一个原始文件的备份。

#6


72  

gnu sed has an option -z for null separated records (lines). You can just call:

gnu sed有一个选项-z用于null分隔的记录(行)。你可以叫:

sed -z 's/\n/ /g'

#7


38  

Who needs sed? Here is the bash way:

谁需要对话?这里是bash方法:

cat test.txt |  while read line; do echo -n "$line "; done

#8


22  

In order to replace all newlines with spaces using awk, without reading the whole file into memory:

为了用awk替换所有的新行,而不将整个文件读入内存:

awk '{printf "%s ", $0}' inputfile

If you want a final newline:

如果你想要最后的换行:

awk '{printf "%s ", $0} END {printf "\n"}' inputfile

You can use a character other than space:

你可以使用除空间以外的字符:

awk '{printf "%s|", $0} END {printf "\n"}' inputfile

#9


20  

Three things.

三件事。

  1. tr (or cat, etc.) is absolutely not needed. (GNU) sed and (GNU) awk, when combined, can do 99.9% of any text processing you need.

    tr(或cat等)绝对不需要。(GNU) sed和(GNU) awk结合起来,可以完成您需要的任何文本处理的99.9%。

  2. stream != line based. ed is a line-based editor. sed is not. See sed lecture for more information on the difference. Most people confuse sed to be line-based because it is, by default, not very greedy in its pattern matching for SIMPLE matches - for instance, when doing pattern searching and replacing by one or two characters, it by default only replaces on the first match it finds (unless specified otherwise by the global command). There would not even be a global command if it were line-based rather than STREAM-based, because it would evaluate only lines at a time. Try running ed; you'll notice the difference. ed is pretty useful if you want to iterate over specific lines (such as in a for-loop), but most of the times you'll just want sed.

    基于流! =线。ed是一个基于行的编辑器。sed不是。有关差异的更多信息,请参阅sed讲座。大多数人混淆sed基于行的,因为它是,默认情况下,不是很贪婪模式匹配的简单匹配——例如,当进行模式搜索和替换一个或两个字符,默认情况下只替换第一个匹配它发现(除非指定否则全球命令)。如果它是基于行的,而不是基于流的,甚至不会有一个全局命令,因为它只会一次评估行。尝试运行ed;你会注意到差别。如果您希望迭代特定的行(比如for循环),那么ed非常有用,但是大多数情况下您只需要sed。

  3. That being said,

    话虽这么说,

    sed -e '{:q;N;s/\n/ /g;t q}' file
    

    works just fine in GNU sed version 4.2.1. The above command will replace all newlines with spaces. It's ugly and a bit cumbersome to type in, but it works just fine. The {}'s can be left out, as they're only included for sanity reasons.

    在GNU sed版本4.2.1中运行良好。上面的命令将替换所有带有空格的新行。打字很难看,而且有点麻烦,但效果很好。{}s可以省略,因为它们只包含在正常的原因中。

#10


18  

tr '\n' ' ' 

is the command.

是命令。

Simple and easy to use.

简单易用。

#11


12  

The answer with the :a label ...

答案是:标签……

How can I replace a newline (\n) using sed?

如何使用sed替换新行(\n) ?

... does not work in freebsd 7.2 on the command line:

…不工作在freebsd 7.2的命令行:

( echo foo ; echo bar ) | sed ':a;N;$!ba;s/\n/ /g'
sed: 1: ":a;N;$!ba;s/\n/ /g": unused label 'a;N;$!ba;s/\n/ /g'
foo
bar

But does if you put the sed script in a file or use -e to "build" the sed script...

但是,如果您将sed脚本放在一个文件中,或者使用-e来“构建”sed脚本……

> (echo foo; echo bar) | sed -e :a -e N -e '$!ba' -e 's/\n/ /g'
foo bar

or ...

还是……

> cat > x.sed << eof
:a
N
$!ba
s/\n/ /g
eof

> (echo foo; echo bar) | sed -f x.sed
foo bar

Maybe the sed in OS X is similar.

也许在OS X中的sed是相似的。

#12


10  

You can use xargs:

您可以使用xargs:

seq 10 | xargs

or

seq 10 | xargs echo -n

#13


9  

I'm not an expert, but I guess in sed you'd first need to append the next line into the pattern space, bij using "N". From the section "Multiline Pattern Space" in "Advanced sed Commands" of the book sed & awk (Dale Dougherty and Arnold Robbins; O'Reilly 1997; page 107 in the preview):

我不是专家,但我猜在sed中,您首先需要将下一行添加到模式空间中,bij使用“N”。从《sed & awk》(戴尔·多尔蒂和阿诺德·罗宾斯)的《高级sed命令》中的“多行模式空间”一节中;O ' reilly 1997;第107页的预览):

The multiline Next (N) command creates a multiline pattern space by reading a new line of input and appending it to the contents of the pattern space. The original contents of pattern space and the new input line are separated by a newline. The embedded newline character can be matched in patterns by the escape sequence "\n". In a multiline pattern space, the metacharacter "^" matches the very first character of the pattern space, and not the character(s) following any embedded newline(s). Similarly, "$" matches only the final newline in the pattern space, and not any embedded newline(s). After the Next command is executed, control is then passed to subsequent commands in the script.

multiline Next (N)命令通过读取新的输入行并将其附加到模式空间的内容,从而创建多行模式空间。模式空间的原始内容和新的输入行由一条换行分隔。嵌入的换行符可以根据转义序列“\n”匹配。多行模式空间,元字符“^”匹配的第一个字符模式空间,而不是字符(s)任何嵌入后换行。类似地,“$”只匹配模式空间中的最后一条换行,而不是任何嵌入的换行符。在执行下一个命令之后,将控制权传递给脚本中的后续命令。

From man sed:

从人对话:

[2addr]N

[2 addr]N

Append the next line of input to the pattern space, using an embedded newline character to separate the appended material from the original contents. Note that the current line number changes.

将下一行输入到模式空间中,使用嵌入式换行符将附加的内容与原始内容分离。注意,当前行号更改。

I've used this to search (multiple) badly formatted log files, in which the search string may be found on an "orphaned" next line.

我用这个来搜索(多个)格式糟糕的日志文件,其中的搜索字符串可能会在“孤立”的下一行中找到。

#14


7  

Easy-to-understand Solution

I had this problem. The kicker was that I needed the solution to work on BSD's (Mac OS X) and GNU's (Linux and Cygwin) sed and tr:

我有这个问题。更重要的是,我需要解决BSD (Mac OS X)和GNU (Linux和Cygwin) sed和tr的解决方案:

$ echo 'foo
bar
baz


foo2
bar2
baz2' \
| tr '\n' '\000' \
| sed 's:\x00\x00.*:\n:g' \
| tr '\000' '\n'

Output:

输出:

foo
bar
baz

(has trailing newline)

(已落后于换行符)

It works on Linux, OS X, and BSD - even without UTF-8 support or with a crappy terminal.

它在Linux、OS X和BSD上运行,即使没有UTF-8支持,也没有一个蹩脚的终端。

  1. Use tr to swap the newline with another character.

    使用tr将换行符替换为另一个字符。

    NULL (\000 or \x00) is nice because it doesn't need UTF-8 support and it's not likely to be used.

    NULL(\000或\x00)很好,因为它不需要UTF-8支持,也不太可能被使用。

  2. Use sed to match the NULL

    使用sed匹配NULL。

  3. Use tr to swap back extra newlines if you need them

    如果你需要的话,可以用tr来交换多余的换行。

#15


6  

In response to the "tr" solution above, on Windows (probably using the Gnuwin32 version of tr), the proposed solution:

针对以上的“tr”解决方案,在Windows上(可能使用的是Gnuwin32版本的tr),建议的解决方案是:

tr '\n' ' ' < input

was not working for me, it'd either error or actually replace the \n w/ '' for some reason.

它不是为我工作的,它要么是错误的,要么是由于某种原因而替代了\n w/ "。

Using another feature of tr, the "delete" option -d did work though:

使用tr的另一个特性,“delete”选项-d确实有效:

tr -d '\n' < input

or '\r\n' instead of '\n'

或者'\r\n'而不是'\n'

#16


6  

I used a hybrid approach to get around the newline thing by using tr to replace newlines with tabs, then replacing tabs with whatever I want. In this case, "
" since I'm trying to generate HTML breaks.

我使用了一种混合的方法来绕过换行,用tr替换带有标签的换行符,然后用我想要的任何东西替换制表符。在这种情况下,“因为我正在尝试生成HTML中断。”

echo -e "a\nb\nc\n" |tr '\n' '\t' | sed 's/\t/ <br> /g'`

#17


5  

In some situations maybe you can change RS to some other string or character. This way, \n is available for sub/gsub:

在某些情况下,也许你可以把RS换成其他字符串或字符。这种方式,\n可用于sub/gsub:

$ gawk 'BEGIN {RS="dn" } {gsub("\n"," ") ;print $0 }' file

The power of shell scripting is that if you do not know how to do it in one way you can do it in another way. And many times you have more things to take into account than make a complex solution on a simple problem.

shell脚本的强大之处在于,如果您不知道如何用一种方法来完成它,您可以用另一种方法来完成它。很多时候,你有更多的事情要考虑,而不是在一个简单的问题上做一个复杂的解决方案。

Regarding the thing that gawk is slow... and reads the file into memory, I do not know this, but to me gawk seems to work with one line at the time and is very very fast (not that fast as some of the others, but the time to write and test also counts).

关于gawk慢的事情……将文件读入内存,我不知道这一点,但对我来说,gawk似乎是在一段时间内工作,而且速度非常非常快(不像其他一些那样快,但是写和测试的时间也很重要)。

I process MB and even GB of data, and the only limit I found is line size.

我处理MB甚至是GB的数据,我发现的唯一限制是行大小。

#18


4  

You could use xargs — it will replace \n with a space by default.

您可以使用xargs -它将在默认情况下替换\n。

However, it would have problems if your input has any case of an unterminated quote, e.g. if the quote signs on a given line don't match.

但是,如果您的输入有任何未终止的引用,则会出现问题,例如,如果在给定的行上的引用符号不匹配。

#19


4  

Bullet-proof solution. Binary-data-safe and POSIX-compliant, but slow.

POSIX sed requires input according to the POSIX text file and POSIX line definitions, so NULL-bytes and too long lines are not allowed and each line must end with a newline (including the last line). This makes it hard to use sed for processing arbitrary input data.

POSIX sed需要根据POSIX文本文件和POSIX行定义进行输入,因此不允许空字节和太长的行,并且每一行必须以一条换行符(包括最后一行)结束。这使得它很难用于处理任意输入数据。

The following solution avoids sed and instead converts the input bytes to octal codes and then to bytes again, but intercepts octal code 012 (newline) and outputs the replacement string in place of it. As far as I can tell the solution is POSIX-compliant, so it should work on a wide variety of platforms.

下面的解决方案避免了sed,而是将输入字节转换为八进制码,然后再转换为字节,但是拦截octal代码012(换行)并输出替代字符串。就我所知,解决方案是posix兼容的,所以它应该在各种各样的平台上工作。

od -A n -t o1 -v | tr ' \t' '\n\n' | grep . |
  while read x; do [ "0$x" -eq 012 ] && printf '<br>\n' || printf "\\$x"; done

POSIX reference documentation: sh, shell command language, od, tr, grep, read, [, printf.

POSIX参考文档:sh, shell命令语言,od, tr, grep, read, [, printf。

Both read, [, and printf are built-ins in at least bash, but that is probably not guaranteed by POSIX, so on some platforms it could be that each input byte will start one or more new processes, which will slow things down. Even in bash this solution only reaches about 50 kB/s, so it's not suited for large files.

两者都读,[,和printf在至少bash中都是内置的,但这可能不是POSIX所保证的,所以在某些平台上,可能每个输入字节都将启动一个或多个新进程,这将使事情慢下来。即使在bash中,这个解决方案也只能达到大约50 kB/s,因此它不适合大型文件。

Tested on Ubuntu (bash, dash, and busybox), FreeBSD, and OpenBSD.

测试了Ubuntu (bash、dash、busybox)、FreeBSD和OpenBSD。

#20


3  

On Mac OS X (using FreeBSD sed):

在Mac OS X上(使用FreeBSD sed):

# replace each newline with a space
printf "a\nb\nc\nd\ne\nf" | sed -E -e :a -e '$!N; s/\n/ /g; ta'
printf "a\nb\nc\nd\ne\nf" | sed -E -e :a -e '$!N; s/\n/ /g' -e ta

#21


3  

Using Awk:

使用Awk:

awk "BEGIN { o=\"\" }  { o=o \" \" \$0 }  END { print o; }"

#22


3  

A solution I particularly like is to append all the file in the hold space and replace all newlines at the end of file:

我特别喜欢的一个解决方案是在保存空间中追加所有文件,并在文件末尾替换所有换行:

$ (echo foo; echo bar) | sed -n 'H;${x;s/\n//g;p;}'
foobar

However, someone said me the hold space can be finite in some sed implementations.

然而,有人说,在某些sed实现中,存储空间是有限的。

#23


3  

Replace newlines with any string, and replace the last newline too

The pure tr solutions can only replace with a single character, and the pure sed solutions don't replace the last newline of the input. The following solution fixes these problems, and seems to be safe for binary data (even with a UTF-8 locale):

纯tr解决方案只能用一个字符来替换,而纯sed解决方案不能替代输入的最后一行。下面的解决方案解决了这些问题,而且对于二进制数据(即使使用UTF-8语言环境)似乎是安全的:

printf '1\n2\n3\n' |
  sed 's/%/%p/g;s/@/%a/g' | tr '\n' @ | sed 's/@/<br>/g;s/%a/@/g;s/%p/%/g'

Result:

结果:

1<br>2<br>3<br>

#24


3  

It is sed that introduces the new-lines after "normal" substitution. First, it trims the new-line char, then it processes according to your instructions, then it introduces a new-line.

介绍了“正常”替代后的新线路。首先,它对新行char进行测试,然后根据你的指令进行处理,然后引入一条新行。

Using sed you can replace "the end" of a line (not the new-line char) after being trimmed, with a string of your choice, for each input line; but, sed will output different lines. For example, suppose you wanted to replace the "end of line" with "===" (more general than a replacing with a single space):

使用sed可以将行(而不是新行char)的“末尾”替换为每个输入行,并为每个输入行指定一个字符串;但是,sed将输出不同的行。例如,假设您想用“===”替换“行结束”(比用一个空格替换更通用):

PROMPT~$ cat <<EOF |sed 's/$/===/g'
first line
second line
3rd line
EOF

first line===
second line===
3rd line===
PROMPT~$

To replace the new-line char with the string, you can, inefficiently though, use tr , as pointed before, to replace the newline-chars with a "special char" and then use sed to replace that special char with the string you want.

要用字符串替换新行char,您可以使用tr(如前所述),用“特殊char”替换新行字符,然后使用sed替换您想要的字符串。

For example:

例如:

PROMPT~$ cat <<EOF | tr '\n' $'\x01'|sed -e 's/\x01/===/g'
first line
second line
3rd line
EOF

first line===second line===3rd line===PROMPT~$

#25


2  

@OP, if you want to replace newlines in a file, you can just use dos2unix (or unix2dox)

@OP,如果您想在文件中替换换行,您可以使用dos2unix(或unix2dox)

dos2unix yourfile yourfile

#26


2  

To remove empty lines:

删除空行:

sed -n "s/^$//;t;p;"

#27


2  

You can use this method also

你也可以用这个方法。

sed 'x;G;1!h;s/\n/ /g;$!d'

Explanation

解释

x   - which is used to exchange the data from both space (pattern and hold).
G   - which is used to append the data from hold space to pattern space.
h   - which is used to copy the pattern space to hold space.
1!h - During first line won't copy pattern space to hold space due to \n is
      available in pattern space.
$!d - Clear the pattern space every time before getting next line until the
      last line.

Flow:
When the first line get from the input, exchange is made, so 1 goes to hold space and \n comes to pattern space, then appending the hold space to pattern space, and then substitution is performed and deleted the pattern space.
During the second line exchange is made, 2 goes to hold space and 1 comes to pattern space, then G append the hold space into the pattern space, then h copy the pattern to it and substitution is made and deleted. This operation is continued until eof is reached then print exact result.

流:当第一行从输入中得到时,交换就生成了,所以1去容纳空间,\n进入模式空间,然后将容纳空间附加到模式空间,然后执行替换,并删除模式空间。在第二行交换中,有2个保持空间,1个进入模式空间,然后G在模式空间中添加容纳空间,然后h复制模式到它,并进行替换和删除。这个操作一直持续到eof到达,然后打印精确的结果。

#28


1  

sed '1h;1!H;$!d
     x;s/\n/ /g' YourFile

This does not work for huge files (buffer limit), but it is very efficient if there is enough memory to hold the file. (Correction H-> 1h;1!H after the good remark of @hilojack )

这并不适用于大文件(缓冲区限制),但是如果有足够的内存来保存文件,那么它是非常有效的。(校正H - > 1 H;1 !H在@hilojack的好评之后

Another version that change new line while reading (more cpu, less memory)

另一个在读取时更改新行的版本(更多的cpu,更少的内存)

 sed ':loop
 $! N
 s/\n/ /
 t loop' YourFile

#29


1  

Another sed method, almost the same as Zsolt Botykai's answer, but this uses sed's less-frequently used y (transliterate) command, which saves one byte of code (the trailing 'g'):

另一种sed方法,几乎与Zsolt Botykai的答案相同,但它使用sed较少使用的y (transliterate)命令,它节省了一个字节的代码(后面的“g”):

sed ':a;N;$!ba;y/\n/ /'

One would hope y would run faster than s, (perhaps at tr speeds, 20x faster), but in GNU sed v4.2.2 y is about 4% slower than s.

一个人希望y跑得比s快,(也许速度快20倍),但在GNU sed v4.2.2 y中比s慢4%。

#30


1  

I posted this answer because I have tried with most of the sed commend example provided above which does not work for me in my Unix box and giving me error message Label too long: {:q;N;s/\n/ /g;t q}. Finally I made my requirement and hence shared here which works in all Unix/Linux environment:-

我发布了这个答案,因为我已经尝试了上面提供的大多数sed推荐示例,这些示例在我的Unix框中不起作用,并且给我的错误消息标签太长:{:q;N;s/\ N /g;t q}。最后,我提出了我的要求,并在这里共享了在所有Unix/Linux环境中工作的内容:-。

line=$(while read line; do echo -n "$line "; done < yoursourcefile.txt)
echo $line |sed 's/ //g' > sortedoutput.txt

The first line will remove all the new line from file yoursourcefile.txt and will produce a single line. And second sed command will remove all the spaces from it.

第一行将删除文件yoursourcefile中的所有新行。txt将生成一行。第二个sed命令将删除它的所有空格。