Linux 如何将多行输出连接到一行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15580144/
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
How to concatenate multiple lines of output to one line?
提问by T. Webster
If I run the command cat file | grep pattern
, I get many lines of output. How do you concatenate all lines into one line, effectively replacing each "\n"
with "\" "
(end with "
followed by space)?
如果我运行命令cat file | grep pattern
,我会得到多行输出。如何您连接所有线路,一条线路,有效地取代各自"\n"
用"\" "
(最终与"
后跟空格)?
cat file | grep pattern | xargs sed s/\n/ /g
isn't working for me.
cat file | grep pattern | xargs sed s/\n/ /g
不适合我。
采纳答案by Chris Seymour
Use tr '\n' ' '
to translate all newline characters to spaces:
使用tr '\n' ' '
翻译所有换行符为空格:
$ grep pattern file | tr '\n' ' '
Note: grep
reads files, cat
concatenates files. Don't cat file | grep
!
注意:grep
读取文件,cat
连接文件。不要cat file | grep
!
Edit:
编辑:
tr
can only handle single character translations. You could use awk
to change the output record separator like:
tr
只能处理单个字符的翻译。您可以使用awk
更改输出记录分隔符,如:
$ grep pattern file | awk '{print}' ORS='" '
This would transform:
这将转变:
one
two
three
to:
到:
one" two" three"
回答by sehe
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
)
回答by bluebadge
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
. The default limit of xargs
output is ~4096 characters, but can be increased with eg. xargs -s 8192
.
或任何命令,例如。ls | xargs
. xargs
输出的默认限制是 ~4096 个字符,但可以增加例如。xargs -s 8192
.
回答by user1699917
In bash echo
without quotes remove carriage returns, tabs and multiple spaces
在echo
没有引号的bash 中删除回车符、制表符和多个空格
echo $(cat file)
回答by kenorb
Here is the method using ex
editor(part of Vim):
Join all lines and print to the standard output:
$ ex +%j +%p -scq! file
Join all lines in-place (in the file):
$ ex +%j -scwq file
Note: This will concatenate all lines inside the file it-self!
ĴOIN所有行和pRINT到标准输出:
$ ex +%j +%p -scq! file
ĴOIN就地所有行(在文件中):
$ ex +%j -scwq file
注意:这将连接文件本身中的所有行!
回答by Vladimir Yahello
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
它将所有行合并为一个带有空格分隔符的行
回答by Richard Gomes
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
回答by simhumileco
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. Details can be found by running this command: xargs --show-limits
and of course in the manual: man xargs
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
)替换波浪线字符,并对每个波浪线 (flag g
) 执行此操作。
回答by user13498546
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.
这给了我一个文件的所有记录在一行上。