Bash paste 命令输出格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26466260/
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 paste command output formatting
提问by Ramesh
File1:
文件1:
1
2
File2:
文件2:
1 2 3
4 5
File3:
文件3:
x x x
yy yy
zz
paste file1 file2 file2
gives me a tab separated output:
paste file1 file2 file2
给我一个制表符分隔的输出:
1 1 2 3 x x x
2 4 5 yy yy
zz
paste -d" " file1 file2 file3
gives me the output:
paste -d" " file1 file2 file3
给我输出:
1 1 2 3 x x x
2 4 5 yy yy
zz
I want it like below:
我想要它如下:
1 1 2 3 x x x
2 4 5 yy yy
zz
Any idea if this is possible or should I try any other command?
知道这是否可行还是我应该尝试任何其他命令?
采纳答案by Ramesh
Could use sed after to remove tabs
可以使用 sed after 删除标签
paste file file2 file3 | sed 's/\t/ /'
1 1 2 3 x x x
2 4 5 yy yy
zz
Here is a general purpose awk script that will work on any number of file with any formatting.
这是一个通用的 awk 脚本,可以处理任意数量的任何格式的文件。
awk '
{x=ARGIND;a[x]=a[x]>(b=length($ paste <(paste -d" " f1 f2) f3
1 1 2 3 x x x
2 4 5 yy yy
zz
))?a[x]:b}
{F[FNR,x]=paste -d" " file1 file2 | paste -d'|' - file3 | sed 's,|, ,g'
}
END{
for(q=1;q<=FNR;q++)
{
for(i=1;i<=ARGC;i++)
{
printf( "%-"a[i]"s ",F[q,i])
}print ""
}
}' file{1,2,3,4)
回答by fedorqui 'SO stop harming'
paste
twice makes it to me:
paste
两次对我来说:
paste -d'¤' file1 file2 | sed 's,¤, ,g'
回答by lynxlynxlynx
Just from you examples alone, it seems you could try first joining files 1 and 2, then joining that with file 3, but with a special delimiter, which you'd change later to a space.
仅从您的示例来看,您似乎可以先尝试将文件 1 和 2 连接起来,然后将其与文件 3 连接起来,但使用特殊的分隔符,稍后将其更改为空格。
Untested example:
未经测试的示例:
$ more file{1,2,3,4} | cat
::::::::::::::
file1
::::::::::::::
1
2
::::::::::::::
file2
::::::::::::::
1 2 3
4 5 6 7 8
::::::::::::::
file3
::::::::::::::
x x x
yy yy
zz
::::::::::::::
file4
::::::::::::::
a a a
bb bb bb
c c cc
d d d
$ paste file{1,2,3,4} | sed -e 's/\t/ \t/g' | column -t -s$'\t'
1 1 2 3 x x x a a a
2 4 5 6 7 8 yy yy bb bb bb
zz c c cc
d d d
Here I used |
, but you should use something you know for sure won't appear in the data i.e. something even more obscure like ?
. A bit of a hack, but should work.
我在这里使用了|
,但是您应该使用您确定不会出现在数据中的内容,即更晦涩的内容,例如?
。有点黑客,但应该工作。
For just two files:
仅用于两个文件:
##代码##回答by Etan Reisner
Is this the sort of thing you are looking for?
这是您正在寻找的那种东西吗?
##代码##