bash cat将多个文件内容转换为没有换行符的单个字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3936738/
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
bash cat multiple files content in to single string without newlines
提问by user271785
i got some files with name start as eg_. and only each contains one single line
我得到了一些以 eg_ 开头的文件。并且只有每个包含一行
eg_01.txt: @china:129.00
eg_01.txt:@china:129.00
eg_02.txt @uk:219.98
eg_02.txt @uk:219.98
eg_03.txt @USA:341.90
eg_03.txt @USA:341.90
......
......
i am expecting to cat them in to a single line to send by URL like: @china:129.00@uk:219.98@USA:341.90
我希望将它们归为一行,然后通过 URL 发送,例如:@china:129.00@uk:219.98@USA:341.90
i use
echo cat eg_*
我用回声 cat eg_*
it give me the output look like a string, but it actually contains new line: "@china:129.00
它给我的输出看起来像一个字符串,但它实际上包含新行:“@china:129.00
@uk:219.98 @USA:341.90"
@英国:219.98 @美国:341.90"
is there any other way i can construct that string which expected and get rid of new line and even the space? is only cat enough to do this?
有没有其他方法可以构建预期的字符串并摆脱新行甚至空格?只有猫能做到这一点吗?
thanks in advance
提前致谢
回答by Daniel DiPaolo
You could always pipe it to tr
你总是可以通过管道将它发送到 tr
tr "\n" " "
That removes all newlines on stdin
and replaces them with spaces
这将删除所有换行符stdin
并用空格替换它们
EDIT: as suggested by Bart Sas, you could also remove newlines with tr -d
编辑:根据 Bart Sas 的建议,您还可以删除换行符tr -d
tr -d "\n"
(note: just specifying an empty string to tr
for the second argument won't do)
(注意:只tr
为第二个参数指定一个空字符串是不行的)
回答by ghostdog74
Using only one command
只使用一个命令
url=$(awk '{printf "%s",perl -pe'chomp' eg*.txt
}' eg*)
回答by Andy Lester
In Perl, you'd do it like this:
在 Perl 中,你会这样做:
##代码##The -p
says "loop through the input file and do whatever code is specified by the -e
switch. The chomp
in Perl says "Remove any trailing newlines."
该-p
说“遍历输入文件,但被指定的任何代码-e
开关,chomp
Perl中说:‘中删除任何尾随的换行符’。