Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

formatted printing in awk [duplicate]

Tags:

bash

awk

I have a file that i put it into a 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.

casper@casper-PC ~ ->
 11:16 PM Wed Dec 02$ awk '{$1="|" $1 ; $2=$2 "| " ; $3=$3 "|       |        |" ; print $0}' /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 ?

|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$
like image 951
capser Avatar asked Dec 03 '15 04:12

capser


1 Answers

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 is pipe-delimited.

awk -F"|" '{printf("|%-25s|%10s|%8s|%8s|\n", $1, $2, $3, $4)}' file > newFile

Obviously, the %25s values indicate 25 chars wide, so change to meet your needs.

The occasional leading - as in %-25s indicates to left-justify the text.

revised data solution

 awk '{printf("|%-25s|%10s|%8s|%8s|\n", $1 " " $2, $3, $4, $5)}' file > newFile

Now, $1 " " $2 go in between the first 2 "|" chars, relying on awks concatenation feature to provide 1 value to the %-25s format specifier.

testing

echo "Peggy Ahwesh 37006646" | awk '{printf("|%-25s|%10s|%8s|%8s|\n", $1 $2, $3, $4, $5)}'

output

|PeggyAhwesh              |  37006646|        |        |
like image 151
shellter Avatar answered Oct 09 '22 16:10

shellter