Question

Here's a list of employees with information about each one stored in the file 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

I want to allign this information so that the starting of each field, where the field delimiter is '|' or the pipe, start at the same column numbers. Example: All names be stored starting at column 6, similarly the rest of the fields. One solution I found was to use printf, instead of print. printf can format the output. Here I found that a fixed amount of spaces could be reserved for each field while printing. For instance:

$ awk 'BEGIN{ FS="|"; OFS="|" }1 { printf "%4s %20s %10s %10s %8s %s\n", $1, $2, $3, $4, $5, $6}' emp.lst

Now, 4 spaces are reserved for the first field, 20 for the second, 10 for the third, etc. This works just fine. This is what I get as output on the terminal when I write the above command:

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

Now everything is fine, except that the fields are no longer separated by the delimiter '|' How do I retain the delimiter as '|' ? What I supposed from this is that the built in variable OFS is not working for some reason in the above command:

OFS="|"

Was it helpful?

Solution

When you use printf the setting of OFS becomes a no-op. You will have to explicitly put the delimiter in printf statements. For example:

$ 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

OTHER TIPS

You could use the column command:

$ 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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top