简体   繁体   English

AWK-在列之间添加特定数量的空格

[英]Awk - Adding specific number of spaces between columns

My input file has columns which are space evenly like so: 我的输入文件具有均匀分布的列,如下所示:

X a b C D

How do I use awk to specify the number of spaces between the columns such that I get something like this: 我如何使用awk指定列之间的空格数,以便得到如下内容:

X     a b    C         D

I know how to count the spaces between the columns using awk, I just don't know how to add those spaces in order to get the layout I want. 我知道如何使用awk计算列之间的间隔,我只是不知道如何添加这些间隔以获取所需的布局。 Any suggestions? 有什么建议么?

with awk awk

$ echo "X a b C D" | awk '{printf "%-6s%-2s%-5s%-9s%s\n", $1,$2,$3,$4,$5}'

X     a b    C        D

or with printf directly 或直接与printf

$ echo "X a b C D" | xargs printf "%-6s%-2s%-5s%-9s%s\n"
X     a b    C        D

This is where printf comes in handy: 这是printf派上用场的地方:

$ echo "X a b C D" | perl -lane 'printf "%-6s%-2s%-5s%-9s%s\n", @F'
X     a b    C        D

Adjust the field widths as required. 根据需要调整字段宽度。

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

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