bash awk 格式化打印
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34057902/
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
formatted printing in awk
提问by capser
I have a file that i put it into a wiki
我有一个文件,我把它放到了 wiki
Marina Abramovi? 43199842
Vito Acconci 43160016
Bas Jan 80902233
Eija-Liisa Ahtila 43406552
Peggy Ahwesh 37006646
Rita Ackermann 43208993
Chantal Akerman 43272249
Vikky Alexander 80703016
Edward Allington 43387956
Francis Al?s 43215850
Laurie Anderson 43170307
Carl Andre 43308046
Janine Antoni 43386750
Ida Applebroog 43392256
Nobuyoshi Araki 43387929
Diane Arbus 43394941
Siah Armajani 43312101
Arman smith 80008834
John Armleder 43437177
Richard Artschwager 43371334
Frank Auerbach 43386120
If I put Pipes between the words and names, when i put it in the wiki it looks like a nice neatly formatted table. I used awk which works great, however sometime i spend more time evening out the columns that i would putting in the pipes manually.
如果我将 Pipes 放在单词和名称之间,当我将它放入 wiki 时,它看起来就像一个漂亮的整齐格式化的表格。我使用了很好用的 awk,但是有时我会花更多的时间来平衡我将手动放入管道中的列。
casper@casper-PC ~ ->
11:16 PM Wed Dec 02$ awk '{="|" ; = "| " ; = "| | |" ; print |Marina Abramovi? | 43199842| | |
|Vito Acconci | 43160016| | |
|Bas Jan | 80902233| | |
|Eija-Liisa Ahtila | 43406552| | |
|Peggy Ahwesh | 37006646| | |
|Rita Ackermann | 43208993| | |
|Chantal Akerma | 43272249| | |
|Vikky Alexander | 80703016| | |
|Edward Allington | 43387956| | |
|Francis Al?s | 43215850| | |
|Laurie Anderson | 43170307| | |
|Carl Andre | 43308046| | |
|Janine Antoni | 43386750| | |
|Ida Applebroog | 43392256| | |
|Nobuyoshi Araki | 43387929| | |
|Diane Arbus | 43394941| | |
|Siah Armajani | 43312101| | |
|Arman Smith | 80008834| | |
|John Armleder | 43437177| | |
|Richard Artschwager| 43371334| | |
|Frank Auerbach | 43386120| | |
casper@casper-PC ~ ->
11:16 PM Wed Dec 02$
}' /tmp/joinNameNumber
|Marina Abramovi?| 43199842| | |
|Vito Acconci| 43160016| | |
|Bas Jan| 80902233| | |
|Eija-Liisa Ahtila| 43406552| | |
|Peggy Ahwesh| 37006646| | |
|Rita Ackermann| 43208993| | |
|Chantal Akerman| 43272249| | |
|Vikky Alexander| 80703016| | |
|Edward Allington| 43387956| | |
|Francis Al?s| 43215850| | |
|Laurie Anderson| 43170307| | |
|Carl Andre| 43308046| | |
|Janine Antoni| 43386750| | |
|Ida Applebroog| 43392256| | |
|Nobuyoshi Araki| 43387929| | |
|Diane Arbus| 43394941| | |
|Siah Armajani| 43312101| | |
|Arman Smith| 80008834| | |
|John Armleder| 43437177| | |
|Richard Artschwager| 43371334| | |
|Frank Auerbach| 43386120| | |
casper@casper-PC ~ ->
11:16 PM Wed Dec 02$
Is there a way that i could print out the lines in fixed width ?
有没有办法可以打印出固定宽度的线条?
awk -F"|" '{printf("|%-25s|%10s|%8s|%8s|\n", , , , )}' file > newFile
回答by shellter
I think you said your input file is pipe-delimited (but I don't see that in your sample data).
我想你说你的输入文件是用管道分隔的(但我在你的示例数据中没有看到)。
Here's a solution if your data ispipe-delimited.
如果您的数据以竖线分隔,这里有一个解决方案。
awk '{printf("|%-25s|%10s|%8s|%8s|\n", " " , , , )}' file > newFile
Obviously, the %25s
values indicate 25 chars wide, so change to meet your needs.
显然,这些%25s
值表示 25 个字符宽,因此请根据您的需要进行更改。
The occasional leading -
as in %-25s
indicates to left-justify the text.
偶尔的前导-
如 in%-25s
表示将文本左对齐。
revised data solution
修正数据解决方案
echo "Peggy Ahwesh 37006646" | awk '{printf("|%-25s|%10s|%8s|%8s|\n", , , , )}'
Now, $1 " " $2
go in between the first 2 "|" chars, relying on awk
s concatenation feature to provide 1 value to the %-25s
format specifier.
现在,$1 " " $2
进入前两个“|” 字符,依靠awk
s 连接功能为%-25s
格式说明符提供 1 个值。
testing
测试
|PeggyAhwesh | 37006646| | |
output
输出
##代码##