以下是员工列表,其中包含有关存储在emp.lst文件中的每个人的信息:
2233|a.k. shukla|g.m.|sales|12/12/52|6000
9876|jai sharma|director|production|12/03/50|7000
5678|sumith chakrobarty|d.g.m.|marketing|19/04/43|6000
2365|barun sengupta|director|personnel|11/05/47|7800
5423|n.k. gupta|chairman|admin|30/08/56|5400
1006|chanchal singhvi|director|sales|03/09/38|6700
6213|karuna ganguly|g.m|accounts|05/06/62|6300
1265|s.n. dasgupta|manager|sales|12/09/63|5600
4290|jayant Choudhary|executive|production|07/09/50|6000
2476|anil aggarwal|manager|sales|01/05/59|5000
6521|lalit chowdury|director|marketing|26/09/45|8200
3212|shyam saksena|d.g.m.|accounts|12/12/55|6000
3564|sudhir Agarwal|executive|personnel|06/07/47|7500
2345|j.b. saxena|g.m|marketing|12/03/45|8000
0110|v.k. agarwal|g.m|marketing|31/12/40|9000
我想将这些信息对齐,以便每个字段的开头,字段分隔符为'|'或管道,从相同的列号开始。示例:从第6列开始存储所有名称,类似于其余字段。 我找到的一个解决方案是使用printf而不是print。 printf可以格式化输出。在这里,我发现在打印时可以为每个字段保留固定数量的空间。 例如:
$ awk 'BEGIN{ FS="|"; OFS="|" }1 { printf "%4s %20s %10s %10s %8s %s\n", $1, $2, $3, $4, $5, $6}' emp.lst
现在,第一个字段保留4个空格,第二个字段保留20个空格,第三个字段保留10个空格等。 这很好用。这是我在编写上述命令时在终端上输出的内容:
2233 a.k. shukla g.m. sales 12/12/52 6000
9876 jai sharma director production 12/03/50 7000
5678 sumith chakrobarty d.g.m. marketing 19/04/43 6000
2365 barun sengupta director personnel 11/05/47 7800
5423 n.k. gupta chairman admin 30/08/56 5400
1006 chanchal singhvi director sales 03/09/38 6700
6213 karuna ganguly g.m accounts 05/06/62 6300
1265 s.n. dasgupta manager sales 12/09/63 5600
4290 jayant Choudhary executive production 07/09/50 6000
2476 anil aggarwal manager sales 01/05/59 5000
6521 lalit chowdury director marketing 26/09/45 8200
3212 shyam saksena d.g.m. accounts 12/12/55 6000
3564 sudhir Agarwal executive personnel 06/07/47 7500
2345 j.b. saxena g.m marketing 12/03/45 8000
0110 v.k. agarwal g.m marketing 31/12/40 9000
现在一切都很好,除了字段不再用分隔符'|'分隔如何将分隔符保留为“|” ? 我所认为的是内置变量OFS在上述命令中由于某种原因不起作用:
OFS = “|”
答案 0 :(得分:1)
当您使用printf
时,OFS
的设置将成为无操作。您必须在printf
语句中明确地放置分隔符。例如:
$ awk -F'|' '{printf "%4s|%20s|%10s|%10s|%8s|%s\n", $1, $2, $3, $4, $5, $6}' file
2233| a.k. shukla| g.m.| sales|12/12/52|6000
9876| jai sharma| director|production|12/03/50|7000
5678| sumith chakrobarty| d.g.m.| marketing|19/04/43|6000
2365| barun sengupta| director| personnel|11/05/47|7800
5423| n.k. gupta| chairman| admin|30/08/56|5400
1006| chanchal singhvi| director| sales|03/09/38|6700
6213| karuna ganguly| g.m| accounts|05/06/62|6300
1265| s.n. dasgupta| manager| sales|12/09/63|5600
4290| jayant Choudhary| executive|production|07/09/50|6000
2476| anil aggarwal| manager| sales|01/05/59|5000
6521| lalit chowdury| director| marketing|26/09/45|8200
3212| shyam saksena| d.g.m.| accounts|12/12/55|6000
3564| sudhir Agarwal| executive| personnel|06/07/47|7500
2345| j.b. saxena| g.m| marketing|12/03/45|8000
0110| v.k. agarwal| g.m| marketing|31/12/40|9000
答案 1 :(得分:1)
您可以使用列命令:
$ column -s '|' -t file
2233 a.k. shukla g.m. sales 12/12/52 6000
9876 jai sharma director production 12/03/50 7000
5678 sumith chakrobarty d.g.m. marketing 19/04/43 6000
2365 barun sengupta director personnel 11/05/47 7800
5423 n.k. gupta chairman admin 30/08/56 5400
1006 chanchal singhvi director sales 03/09/38 6700
6213 karuna ganguly g.m accounts 05/06/62 6300
1265 s.n. dasgupta manager sales 12/09/63 5600
4290 jayant Choudhary executive production 07/09/50 6000
2476 anil aggarwal manager sales 01/05/59 5000
6521 lalit chowdury director marketing 26/09/45 8200
3212 shyam saksena d.g.m. accounts 12/12/55 6000
3564 sudhir Agarwal executive personnel 06/07/47 7500
2345 j.b. saxena g.m marketing 12/03/45 8000
0110 v.k. agarwal g.m marketing 31/12/40 9000