简体   繁体   English

如何将多行输出连接到一行?

[英]How to concatenate multiple lines of output to one line?

If I run the command cat file | grep pattern如果我运行命令cat file | grep pattern cat file | grep pattern , I get many lines of output. cat file | grep pattern ,我得到多行输出。 How do you concatenate all lines into one line, effectively replacing each "\\n" with "\\" " (end with " followed by space)?如何将所有行连接成一行,有效地将每个"\\n"替换为"\\" " (以"后跟空格结尾)?

cat file | grep pattern | xargs sed s/\\n/ /g cat file | grep pattern | xargs sed s/\\n/ /g isn't working for me. cat file | grep pattern | xargs sed s/\\n/ /g对我不起作用。

Use tr '\\n' ' ' to translate all newline characters to spaces:使用tr '\\n' ' '将所有换行符转换为空格:

$ grep pattern file | tr '\n' ' '

Note: grep reads files, cat concatenates files.注意: grep读取文件, cat连接文件。 Don't cat file | grep不要cat file | grep cat file | grep ! cat file | grep

Edit:编辑:

tr can only handle single character translations. tr只能处理单个字符的翻译。 You could use awk to change the output record separator like:您可以使用awk更改输出记录分隔符,例如:

$ grep pattern file | awk '{print}' ORS='" '

This would transform:这将转变:

one
two 
three

to:到:

one" two" three" 

Piping output to xargs will concatenate each line of output to a single line with spaces:管道输出到xargs会将每一行输出连接到一行带有空格的行:

grep pattern file | xargs

Or any command, eg.或任何命令,例如。 ls | xargs ls | xargs . ls | xargs The default limit of xargs output is ~4096 characters, but can be increased with eg. xargs输出的默认限制是 ~4096 个字符,但可以通过例如增加。 xargs -s 8192 . xargs -s 8192

在没有引号的 bash echo删除回车、制表符和多个空格

echo $(cat file)

This could be what you want这可能是你想要的

cat file | grep pattern | paste -sd' '

As to your edit, I'm not sure what it means, perhaps this?至于您的编辑,我不确定这意味着什么,也许是这个?

cat file | grep pattern | paste -sd'~' | sed -e 's/~/" "/g'

(this assumes that ~ does not occur in file ) (这假设~不会出现在file

This is an example which produces output separate by commas.这是一个生成由逗号分隔的输出的示例。 You can replace the comma by whatever separator you need.您可以用您需要的任何分隔符替换逗号。

cat <<EOD | xargs | sed 's/ /,/g'
> 1
> 2
> 3
> 4
> 5
> EOD

produces:产生:

1,2,3,4,5

Here is the method using ex editor (part of Vim ):这是使用ex编辑器Vim 的一部分)的方法:

  • J oin all lines and p rint to the standard output: ĴOIN所有行和P RINT到标准输出:

     $ ex +%j +%p -scq! file
  • J oin all lines in-place (in the file): ĴOIN就地所有行(在文件中):

     $ ex +%j -scwq file

    Note: This will concatenate all lines inside the file it-self!注意:这将连接文件本身中的所有行!

The fastest and easiest ways I know to solve this problem:我知道解决这个问题的最快和最简单的方法:

When we want to replace the new line character \\n with the space :当我们想用空格替换换行符\\n

xargs < file

xargs has own limits on the number of characters per line and the number of all characters combined, but we can increase them. xargs对每行的字符数和所有字符的组合数有自己的限制,但我们可以增加它们。 Details can be found by running this command: xargs --show-limits and of course in the manual: man xargs可以通过运行以下命令找到详细信息: xargs --show-limits ,当然在手册中: man xargs

When we want to replace one character with another exactly one character :当我们想用另一个恰好一个字符替换一个字符时

tr '\n' ' ' < file

When we want to replace one character with many characters :当我们想用多个字符替换一个字符时

tr '\n' '~' < file | sed s/~/many_characters/g

First, we replace the newline characters \\n for tildes ~ (or choose another unique character not present in the text), and then we replace the tilde characters with any other characters ( many_characters ) and we do it for each tilde (flag g ).首先,我们将换行符\\n替换为波浪号~ (或选择文本中不存在的另一个唯一字符),然后我们用任何其他字符( many_characters )替换波浪号字符,并对每个波浪号(标志g )进行替换.

I like the xargs solution, but if it's important to not collapse spaces, then one might instead do:我喜欢xargs解决方案,但如果不折叠空格很重要,那么人们可能会这样做:

sed ':b;$!{N;bb};s/\n/ /g'

That will replace newlines for spaces, without substituting the last line terminator like tr '\\n' ' ' would.这将替换空格的换行符,而不像tr '\\n' ' '那样替换最后一行终止符。

This also allows you to use other joining strings besides a space, like a comma, etc, something that xargs cannot do:这也允许您使用除空格之外的其他连接字符串,如逗号等,这是xargs无法做到的:

$ seq 1 5 | sed ':b;$!{N;bb};s/\n/,/g'
1,2,3,4,5

Here is another simple method using awk :这是使用awk另一种简单方法:

# cat > file.txt
a
b
c

# cat file.txt | awk '{ printf("%s ", $0) }'
a b c

Also, if your file has columns, this gives an easy way to concatenate only certain columns:此外,如果您的文件有列,这提供了一种仅连接某些列的简单方法:

# cat > cols.txt
a b c
d e f

# cat cols.txt | awk '{ printf("%s ", $2) }'
b e

Probably the best way to do it is using 'awk' tool which will generate output into one line可能最好的方法是使用“awk”工具,它将输出生成为一行

$ awk ' /pattern/ {print}' ORS=' ' /path/to/file

It will merge all lines into one with space delimiter它将所有行合并为一个带有空格分隔符的行

On red hat linux I just use echo :在红帽 linux 上,我只使用 echo :

echo $(cat /some/file/name) echo $(cat /some/file/name)

This gives me all records of a file on just one line.这给了我一个文件的所有记录在一行上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM