linux - shell 中的制表符给出不同的宽度

标签 linux shell awk formatting whitespace

我想问一下为什么这个简单的脚本会给出不同的缩进
如果我的方法有误,请提出正确的方法。 :)

数据文件:

Mr. James Adams General Manager
Mr. James Adams General Manager
Mr. James Carleo Branch Manager
Mr. James Cobb Branch Manager
Mr. James Collier Branch Manager
Mr. James K. Cross Sr.Vice
Mr. James Cummings Branch Manager
Mr. "James ""Jamie""" Dyer Branch
Mr. James (Danny) Farris Manager
Mr. James N. Martin General
Mr. James N. Martin General
Mr. James Ogden Branch Manager
Mr. James Wilson Branch Manager

脚本:

awk '{print $1,$2,$3,"\t",$4,$5}'

预期输出:

Mr. James Adams      General Manager
Mr. James Adams      General Manager
Mr. James Carleo     Branch Manager
Mr. James Cobb       Branch Manager
Mr. James Collier    Branch Manager
Mr. James K.         Cross Sr.Vice
Mr. James Cummings   Branch Manager
Mr. "James ""Jamie"""    Dyer Branch
Mr. James (Danny)    Farris Manager
Mr. James N.         Martin General
Mr. James N.         Martin General
Mr. James Ogden      Branch Manager
Mr. James Wilson     Branch Manager

实际输出:

Mr. James Adams      General Manager
Mr. James Adams      General Manager
Mr. James Carleo     Branch Manager
Mr. James Cobb   Branch Manager
Mr. James Collier    Branch Manager
Mr. James K.     Cross Sr.Vice
Mr. James Cummings   Branch Manager
Mr. "James ""Jamie"""    Dyer Branch
Mr. James (Danny)    Farris Manager
Mr. James N.     Martin General
Mr. James N.     Martin General
Mr. James Ogden      Branch Manager
Mr. James Wilson     Branch Manager

最佳答案

您可以使用 awk 中的 printf 来调整列宽等:

awk '{printf "%s %s %-20s %s %s\n",$1,$2,$3,$4,$5}' file
Mr. James Adams                General Manager
Mr. James Adams                General Manager
Mr. James Carleo               Branch Manager
Mr. James Cobb                 Branch Manager
Mr. James Collier              Branch Manager
Mr. James K.                   Cross Sr.Vice
Mr. James Cummings             Branch Manager
Mr. "James ""Jamie"""           Dyer Branch
Mr. James (Danny)              Farris Manager
Mr. James N.                   Martin General
Mr. James N.                   Martin General
Mr. James Ogden                Branch Manager
Mr. James Wilson               Branch Manager

关于linux - shell 中的制表符给出不同的宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22681113/

相关文章:

macos - 查找具有(损坏的) future 日期的所有文件并修改为今天

bash - 列出包含 `n` 或更少行的文件

linux - awk 脚本在特定行打印信息

linux - 在 awk 中访问 shell 变量但不解释

linux - 用于查找第 n 次出现的字符串并打印行号的 shell 脚本

linux - grep for process id 以获得正确的值

regex - 使用正则表达式搜索并用目录创建替换 linux 上的重命名

php - bash awk - 需要来自 php 脚本的关联数组输出

java - 在 Linux 下检索 SD 卡序列号和制造商

linux - 修改/etc/profile linux