formatted printing in awk

10,086

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|        |        |
Share:
10,086
capser
Author by

capser

Updated on September 16, 2022

Comments

  • capser
    capser over 1 year

    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$
    
  • capser
    capser over 8 years
    the source file is not pipe delimited - but this is a good answer !!!!
  • Jonathan Leffler
    Jonathan Leffler over 8 years
    Do you need $1 " " $2, to put a space between the parts of the name?
  • shellter
    shellter over 8 years
    @JonathanLeffler : ah, yep, thanks, I'll fix that. You da man! ;-)
  • shellter
    shellter over 8 years
    @casper : Done with edits I think. Going to bed. Good luck to all.