Modify width of first column in file with a variable number of fields, using awkAwk: expanding first field along the columnHow to add column in the beginning of file using perl?unix: replace one entire column in one file with a single value from another fileJoining two files matching two columns with mismatches and in each matching line, substitute second column from file 1 into 6th column in file 2How to print all fields containing one of two strings in a table with awkHow to compare 2 files with common columns and then get the output file with columns from each fileHow to use awk to correct and unify a corrupted file with multiple columns and lines?awk - , fixed width columnsAwk for merging multiple files with common columnClean formatting of output within bash scripts

Uncovering the Accelerated Dragon opening

Is a suit against a Univeristy Dorm for changing policies on a whim likely to succeed (USA)?

Why is the T-1000 humanoid?

In Germany, how can I maximize the impact of my charitable donations?

Where can I get an anonymous Rav Kav card issued?

Do all humans have an identical nucleotide sequence for certain proteins, e.g haemoglobin?

How is Team Scooby Doo (Mystery Inc.) funded?

Mean π: Archimedes vs. Gauss - π computation through generalized means

How are aircraft depainted?

Is there a reliable way to hide/convey a message in vocal expressions (speech, song,...)

How can I fix a framing mistake so I can drywall?

Does my opponent need to prove his creature has morph?

How do email clients "send later" without storing a password?

What exactly is a marshrutka (маршрутка)?

I was promised a work PC but still awaiting approval 3 months later so using my own laptop - Is it fair to ask employer for laptop insurance?

Can I disable a battery powered device by reversing half of its batteries?

How to work with a technician hired with a grant who argues everything

Why does Coq include let-expressions in its core language

What is my breathable atmosphere composed of?

Do ibuprofen or paracetamol cause hearing loss?

Random point on a sphere

Integer Decision Variables Always Forced to Zero in Minimization Problem (MINLP)

How do I determine what is "magic" and "bearing magic" for Detect Magic?

A shy person in a queue



Modify width of first column in file with a variable number of fields, using awk


Awk: expanding first field along the columnHow to add column in the beginning of file using perl?unix: replace one entire column in one file with a single value from another fileJoining two files matching two columns with mismatches and in each matching line, substitute second column from file 1 into 6th column in file 2How to print all fields containing one of two strings in a table with awkHow to compare 2 files with common columns and then get the output file with columns from each fileHow to use awk to correct and unify a corrupted file with multiple columns and lines?awk - , fixed width columnsAwk for merging multiple files with common columnClean formatting of output within bash scripts






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








4















I've seen a lot of questions regarding formatting in awk. I understand how to use printf, but I dont want to specify every field.



For example, assume this is my file:



c1|c2|c3|c4|c5
c6|c7|c8|c9|c10
c11|c12|c13|c14|c15


I want to format it so every record's first field is the width of c11 of longest cell contained in the first field



c1 |c2|c3|c4|c5
c6 |c7|c8|c9|c10
c11|c12|c13|c14|c15


I understand that I could specify



awk -F"|" 'printf "%-3s%s%s%s%sn", $1, $2, $3, $4, $5' file > newfile


Lets assume I know what I want the width of the first column to be, but I do NOT know how many fields are in the file. Basically I want to do something like



...'", $1' --> and then print the rest of the fields in their original format









share|improve this question









New contributor



Kayli O'Keefe is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





















  • Another way to address it: sed 's/|/'' '' '' |/;s/(...) */1/' (here adding extra quotes to insert those 3 spaces as the SE comments squeeze contiguous spaces into one)

    – Stéphane Chazelas
    7 hours ago


















4















I've seen a lot of questions regarding formatting in awk. I understand how to use printf, but I dont want to specify every field.



For example, assume this is my file:



c1|c2|c3|c4|c5
c6|c7|c8|c9|c10
c11|c12|c13|c14|c15


I want to format it so every record's first field is the width of c11 of longest cell contained in the first field



c1 |c2|c3|c4|c5
c6 |c7|c8|c9|c10
c11|c12|c13|c14|c15


I understand that I could specify



awk -F"|" 'printf "%-3s%s%s%s%sn", $1, $2, $3, $4, $5' file > newfile


Lets assume I know what I want the width of the first column to be, but I do NOT know how many fields are in the file. Basically I want to do something like



...'", $1' --> and then print the rest of the fields in their original format









share|improve this question









New contributor



Kayli O'Keefe is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





















  • Another way to address it: sed 's/|/'' '' '' |/;s/(...) */1/' (here adding extra quotes to insert those 3 spaces as the SE comments squeeze contiguous spaces into one)

    – Stéphane Chazelas
    7 hours ago














4












4








4








I've seen a lot of questions regarding formatting in awk. I understand how to use printf, but I dont want to specify every field.



For example, assume this is my file:



c1|c2|c3|c4|c5
c6|c7|c8|c9|c10
c11|c12|c13|c14|c15


I want to format it so every record's first field is the width of c11 of longest cell contained in the first field



c1 |c2|c3|c4|c5
c6 |c7|c8|c9|c10
c11|c12|c13|c14|c15


I understand that I could specify



awk -F"|" 'printf "%-3s%s%s%s%sn", $1, $2, $3, $4, $5' file > newfile


Lets assume I know what I want the width of the first column to be, but I do NOT know how many fields are in the file. Basically I want to do something like



...'", $1' --> and then print the rest of the fields in their original format









share|improve this question









New contributor



Kayli O'Keefe is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I've seen a lot of questions regarding formatting in awk. I understand how to use printf, but I dont want to specify every field.



For example, assume this is my file:



c1|c2|c3|c4|c5
c6|c7|c8|c9|c10
c11|c12|c13|c14|c15


I want to format it so every record's first field is the width of c11 of longest cell contained in the first field



c1 |c2|c3|c4|c5
c6 |c7|c8|c9|c10
c11|c12|c13|c14|c15


I understand that I could specify



awk -F"|" 'printf "%-3s%s%s%s%sn", $1, $2, $3, $4, $5' file > newfile


Lets assume I know what I want the width of the first column to be, but I do NOT know how many fields are in the file. Basically I want to do something like



...'", $1' --> and then print the rest of the fields in their original format






awk files text-formatting printf






share|improve this question









New contributor



Kayli O'Keefe is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.










share|improve this question









New contributor



Kayli O'Keefe is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








share|improve this question




share|improve this question








edited 5 hours ago









Kusalananda

163k19 gold badges321 silver badges508 bronze badges




163k19 gold badges321 silver badges508 bronze badges






New contributor



Kayli O'Keefe is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








asked 8 hours ago









Kayli O'KeefeKayli O'Keefe

233 bronze badges




233 bronze badges




New contributor



Kayli O'Keefe is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




New contributor




Kayli O'Keefe is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

















  • Another way to address it: sed 's/|/'' '' '' |/;s/(...) */1/' (here adding extra quotes to insert those 3 spaces as the SE comments squeeze contiguous spaces into one)

    – Stéphane Chazelas
    7 hours ago


















  • Another way to address it: sed 's/|/'' '' '' |/;s/(...) */1/' (here adding extra quotes to insert those 3 spaces as the SE comments squeeze contiguous spaces into one)

    – Stéphane Chazelas
    7 hours ago

















Another way to address it: sed 's/|/'' '' '' |/;s/(...) */1/' (here adding extra quotes to insert those 3 spaces as the SE comments squeeze contiguous spaces into one)

– Stéphane Chazelas
7 hours ago






Another way to address it: sed 's/|/'' '' '' |/;s/(...) */1/' (here adding extra quotes to insert those 3 spaces as the SE comments squeeze contiguous spaces into one)

– Stéphane Chazelas
7 hours ago











4 Answers
4






active

oldest

votes


















7
















You can use sprintf to re-format $1 only.



Ex.



$ awk 'BEGIN" $1 = sprintf("%-3s",$1) 1' file
c1 |c2|c3|c4|c5
c6 |c7|c8|c9|c10
c11|c12|c13|c14|c15





share|improve this answer

























  • Concise, you can use dynamic formatting with sprintf too: E.g. awk -vf1=3 'BEGIN"$1=sprintf("%-*s",f1,$1)1' test.txt

    – A.Danischewski
    6 hours ago


















1
















To figure out the largest/longest length of the first field, and then to reformat the values in the field according that length, you will have to do two separate passes over the file.



awk 'BEGIN " 
FNR == NR if (m < (n=length($1))) m = n; next
$1 = sprintf("%-*s", m, $1); print ' file file


(note that the input file is specified twice on the command line)



For the data that you present, this would produce



c1 |c2|c3|c4|c5
c6 |c7|c8|c9|c10
c11|c12|c13|c14|c15


The first pass is handled by the FNR == NR block, which simply keeps track of the longest field seen so far (m contains the maximum length seen), and skips to the next line.



The second pass is handled by the last block, which reformats the first field using sprintf(). The format string %-*s means "a left-justified string whose width is given by the integer argument before the argument that holds the actual string".



This could obviously be expanded to do all columns by turning the scalar m into an array that holds the maximum width of each column:



$ awk 'BEGIN " 
FNR == NR for (i=1; i<=NF; ++i) if (m[i] < (n=length($i))) m[i] = n; next
for (i=1; i<=NF; ++i) $i = sprintf("%-*s", m[i], $i); print ' file file
c1 |c2 |c3 |c4 |c5
c6 |c7 |c8 |c9 |c10
c11|c12|c13|c14|c15





share|improve this answer






















  • 1





    @EdMorton Yes, neat, thanks.

    – Kusalananda
    5 hours ago


















0
















The intelligent way is what steeldriver suggested. The needlessly convoluted way is to iterate over every field:



$ awk -F'|' 'printf "%-3s' file
c1 |c2|c3|c4|c5
c6 |c7|c8|c9|c10
c11|c12|c13|c14|c15


But just sprintf $1 and be done with it.






share|improve this answer

























  • You've got it a bit backwards, small concise statements generally are more convoluted. Iterating over the fields is less convoluted.

    – A.Danischewski
    4 hours ago


















0
















In Awk you can use a "*" to generate a dynamic printf format string.



If you know the length already you can pass the field length for the first column with -v.



awk -vcol1=3 'BEGINFS="for(i=1;i<=NF;i++)if(i==1)printf "%*-s%s",col1,$i,FS;else if(i!=NF)printf "%s%s",$i,FS;else printf "%sn",$i;;' test.txt


Note: if you didn't know what the first column length is you could store the values in an array then finding the max col length along the way and print it all out in the END block.






share|improve this answer



























    Your Answer








    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "106"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );







    Kayli O'Keefe is a new contributor. Be nice, and check out our Code of Conduct.









    draft saved

    draft discarded
















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f541215%2fmodify-width-of-first-column-in-file-with-a-variable-number-of-fields-using-awk%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    4 Answers
    4






    active

    oldest

    votes








    4 Answers
    4






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    7
















    You can use sprintf to re-format $1 only.



    Ex.



    $ awk 'BEGIN" $1 = sprintf("%-3s",$1) 1' file
    c1 |c2|c3|c4|c5
    c6 |c7|c8|c9|c10
    c11|c12|c13|c14|c15





    share|improve this answer

























    • Concise, you can use dynamic formatting with sprintf too: E.g. awk -vf1=3 'BEGIN"$1=sprintf("%-*s",f1,$1)1' test.txt

      – A.Danischewski
      6 hours ago















    7
















    You can use sprintf to re-format $1 only.



    Ex.



    $ awk 'BEGIN" $1 = sprintf("%-3s",$1) 1' file
    c1 |c2|c3|c4|c5
    c6 |c7|c8|c9|c10
    c11|c12|c13|c14|c15





    share|improve this answer

























    • Concise, you can use dynamic formatting with sprintf too: E.g. awk -vf1=3 'BEGIN"$1=sprintf("%-*s",f1,$1)1' test.txt

      – A.Danischewski
      6 hours ago













    7














    7










    7









    You can use sprintf to re-format $1 only.



    Ex.



    $ awk 'BEGIN" $1 = sprintf("%-3s",$1) 1' file
    c1 |c2|c3|c4|c5
    c6 |c7|c8|c9|c10
    c11|c12|c13|c14|c15





    share|improve this answer













    You can use sprintf to re-format $1 only.



    Ex.



    $ awk 'BEGIN" $1 = sprintf("%-3s",$1) 1' file
    c1 |c2|c3|c4|c5
    c6 |c7|c8|c9|c10
    c11|c12|c13|c14|c15






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered 8 hours ago









    steeldriversteeldriver

    43.2k5 gold badges56 silver badges95 bronze badges




    43.2k5 gold badges56 silver badges95 bronze badges















    • Concise, you can use dynamic formatting with sprintf too: E.g. awk -vf1=3 'BEGIN"$1=sprintf("%-*s",f1,$1)1' test.txt

      – A.Danischewski
      6 hours ago

















    • Concise, you can use dynamic formatting with sprintf too: E.g. awk -vf1=3 'BEGIN"$1=sprintf("%-*s",f1,$1)1' test.txt

      – A.Danischewski
      6 hours ago
















    Concise, you can use dynamic formatting with sprintf too: E.g. awk -vf1=3 'BEGIN"$1=sprintf("%-*s",f1,$1)1' test.txt

    – A.Danischewski
    6 hours ago





    Concise, you can use dynamic formatting with sprintf too: E.g. awk -vf1=3 'BEGIN"$1=sprintf("%-*s",f1,$1)1' test.txt

    – A.Danischewski
    6 hours ago













    1
















    To figure out the largest/longest length of the first field, and then to reformat the values in the field according that length, you will have to do two separate passes over the file.



    awk 'BEGIN " 
    FNR == NR if (m < (n=length($1))) m = n; next
    $1 = sprintf("%-*s", m, $1); print ' file file


    (note that the input file is specified twice on the command line)



    For the data that you present, this would produce



    c1 |c2|c3|c4|c5
    c6 |c7|c8|c9|c10
    c11|c12|c13|c14|c15


    The first pass is handled by the FNR == NR block, which simply keeps track of the longest field seen so far (m contains the maximum length seen), and skips to the next line.



    The second pass is handled by the last block, which reformats the first field using sprintf(). The format string %-*s means "a left-justified string whose width is given by the integer argument before the argument that holds the actual string".



    This could obviously be expanded to do all columns by turning the scalar m into an array that holds the maximum width of each column:



    $ awk 'BEGIN " 
    FNR == NR for (i=1; i<=NF; ++i) if (m[i] < (n=length($i))) m[i] = n; next
    for (i=1; i<=NF; ++i) $i = sprintf("%-*s", m[i], $i); print ' file file
    c1 |c2 |c3 |c4 |c5
    c6 |c7 |c8 |c9 |c10
    c11|c12|c13|c14|c15





    share|improve this answer






















    • 1





      @EdMorton Yes, neat, thanks.

      – Kusalananda
      5 hours ago















    1
















    To figure out the largest/longest length of the first field, and then to reformat the values in the field according that length, you will have to do two separate passes over the file.



    awk 'BEGIN " 
    FNR == NR if (m < (n=length($1))) m = n; next
    $1 = sprintf("%-*s", m, $1); print ' file file


    (note that the input file is specified twice on the command line)



    For the data that you present, this would produce



    c1 |c2|c3|c4|c5
    c6 |c7|c8|c9|c10
    c11|c12|c13|c14|c15


    The first pass is handled by the FNR == NR block, which simply keeps track of the longest field seen so far (m contains the maximum length seen), and skips to the next line.



    The second pass is handled by the last block, which reformats the first field using sprintf(). The format string %-*s means "a left-justified string whose width is given by the integer argument before the argument that holds the actual string".



    This could obviously be expanded to do all columns by turning the scalar m into an array that holds the maximum width of each column:



    $ awk 'BEGIN " 
    FNR == NR for (i=1; i<=NF; ++i) if (m[i] < (n=length($i))) m[i] = n; next
    for (i=1; i<=NF; ++i) $i = sprintf("%-*s", m[i], $i); print ' file file
    c1 |c2 |c3 |c4 |c5
    c6 |c7 |c8 |c9 |c10
    c11|c12|c13|c14|c15





    share|improve this answer






















    • 1





      @EdMorton Yes, neat, thanks.

      – Kusalananda
      5 hours ago













    1














    1










    1









    To figure out the largest/longest length of the first field, and then to reformat the values in the field according that length, you will have to do two separate passes over the file.



    awk 'BEGIN " 
    FNR == NR if (m < (n=length($1))) m = n; next
    $1 = sprintf("%-*s", m, $1); print ' file file


    (note that the input file is specified twice on the command line)



    For the data that you present, this would produce



    c1 |c2|c3|c4|c5
    c6 |c7|c8|c9|c10
    c11|c12|c13|c14|c15


    The first pass is handled by the FNR == NR block, which simply keeps track of the longest field seen so far (m contains the maximum length seen), and skips to the next line.



    The second pass is handled by the last block, which reformats the first field using sprintf(). The format string %-*s means "a left-justified string whose width is given by the integer argument before the argument that holds the actual string".



    This could obviously be expanded to do all columns by turning the scalar m into an array that holds the maximum width of each column:



    $ awk 'BEGIN " 
    FNR == NR for (i=1; i<=NF; ++i) if (m[i] < (n=length($i))) m[i] = n; next
    for (i=1; i<=NF; ++i) $i = sprintf("%-*s", m[i], $i); print ' file file
    c1 |c2 |c3 |c4 |c5
    c6 |c7 |c8 |c9 |c10
    c11|c12|c13|c14|c15





    share|improve this answer















    To figure out the largest/longest length of the first field, and then to reformat the values in the field according that length, you will have to do two separate passes over the file.



    awk 'BEGIN " 
    FNR == NR if (m < (n=length($1))) m = n; next
    $1 = sprintf("%-*s", m, $1); print ' file file


    (note that the input file is specified twice on the command line)



    For the data that you present, this would produce



    c1 |c2|c3|c4|c5
    c6 |c7|c8|c9|c10
    c11|c12|c13|c14|c15


    The first pass is handled by the FNR == NR block, which simply keeps track of the longest field seen so far (m contains the maximum length seen), and skips to the next line.



    The second pass is handled by the last block, which reformats the first field using sprintf(). The format string %-*s means "a left-justified string whose width is given by the integer argument before the argument that holds the actual string".



    This could obviously be expanded to do all columns by turning the scalar m into an array that holds the maximum width of each column:



    $ awk 'BEGIN " 
    FNR == NR for (i=1; i<=NF; ++i) if (m[i] < (n=length($i))) m[i] = n; next
    for (i=1; i<=NF; ++i) $i = sprintf("%-*s", m[i], $i); print ' file file
    c1 |c2 |c3 |c4 |c5
    c6 |c7 |c8 |c9 |c10
    c11|c12|c13|c14|c15






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited 5 hours ago

























    answered 7 hours ago









    KusalanandaKusalananda

    163k19 gold badges321 silver badges508 bronze badges




    163k19 gold badges321 silver badges508 bronze badges










    • 1





      @EdMorton Yes, neat, thanks.

      – Kusalananda
      5 hours ago












    • 1





      @EdMorton Yes, neat, thanks.

      – Kusalananda
      5 hours ago







    1




    1





    @EdMorton Yes, neat, thanks.

    – Kusalananda
    5 hours ago





    @EdMorton Yes, neat, thanks.

    – Kusalananda
    5 hours ago











    0
















    The intelligent way is what steeldriver suggested. The needlessly convoluted way is to iterate over every field:



    $ awk -F'|' 'printf "%-3s' file
    c1 |c2|c3|c4|c5
    c6 |c7|c8|c9|c10
    c11|c12|c13|c14|c15


    But just sprintf $1 and be done with it.






    share|improve this answer

























    • You've got it a bit backwards, small concise statements generally are more convoluted. Iterating over the fields is less convoluted.

      – A.Danischewski
      4 hours ago















    0
















    The intelligent way is what steeldriver suggested. The needlessly convoluted way is to iterate over every field:



    $ awk -F'|' 'printf "%-3s' file
    c1 |c2|c3|c4|c5
    c6 |c7|c8|c9|c10
    c11|c12|c13|c14|c15


    But just sprintf $1 and be done with it.






    share|improve this answer

























    • You've got it a bit backwards, small concise statements generally are more convoluted. Iterating over the fields is less convoluted.

      – A.Danischewski
      4 hours ago













    0














    0










    0









    The intelligent way is what steeldriver suggested. The needlessly convoluted way is to iterate over every field:



    $ awk -F'|' 'printf "%-3s' file
    c1 |c2|c3|c4|c5
    c6 |c7|c8|c9|c10
    c11|c12|c13|c14|c15


    But just sprintf $1 and be done with it.






    share|improve this answer













    The intelligent way is what steeldriver suggested. The needlessly convoluted way is to iterate over every field:



    $ awk -F'|' 'printf "%-3s' file
    c1 |c2|c3|c4|c5
    c6 |c7|c8|c9|c10
    c11|c12|c13|c14|c15


    But just sprintf $1 and be done with it.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered 7 hours ago









    terdonterdon

    142k35 gold badges294 silver badges471 bronze badges




    142k35 gold badges294 silver badges471 bronze badges















    • You've got it a bit backwards, small concise statements generally are more convoluted. Iterating over the fields is less convoluted.

      – A.Danischewski
      4 hours ago

















    • You've got it a bit backwards, small concise statements generally are more convoluted. Iterating over the fields is less convoluted.

      – A.Danischewski
      4 hours ago
















    You've got it a bit backwards, small concise statements generally are more convoluted. Iterating over the fields is less convoluted.

    – A.Danischewski
    4 hours ago





    You've got it a bit backwards, small concise statements generally are more convoluted. Iterating over the fields is less convoluted.

    – A.Danischewski
    4 hours ago











    0
















    In Awk you can use a "*" to generate a dynamic printf format string.



    If you know the length already you can pass the field length for the first column with -v.



    awk -vcol1=3 'BEGINFS="for(i=1;i<=NF;i++)if(i==1)printf "%*-s%s",col1,$i,FS;else if(i!=NF)printf "%s%s",$i,FS;else printf "%sn",$i;;' test.txt


    Note: if you didn't know what the first column length is you could store the values in an array then finding the max col length along the way and print it all out in the END block.






    share|improve this answer





























      0
















      In Awk you can use a "*" to generate a dynamic printf format string.



      If you know the length already you can pass the field length for the first column with -v.



      awk -vcol1=3 'BEGINFS="for(i=1;i<=NF;i++)if(i==1)printf "%*-s%s",col1,$i,FS;else if(i!=NF)printf "%s%s",$i,FS;else printf "%sn",$i;;' test.txt


      Note: if you didn't know what the first column length is you could store the values in an array then finding the max col length along the way and print it all out in the END block.






      share|improve this answer



























        0














        0










        0









        In Awk you can use a "*" to generate a dynamic printf format string.



        If you know the length already you can pass the field length for the first column with -v.



        awk -vcol1=3 'BEGINFS="for(i=1;i<=NF;i++)if(i==1)printf "%*-s%s",col1,$i,FS;else if(i!=NF)printf "%s%s",$i,FS;else printf "%sn",$i;;' test.txt


        Note: if you didn't know what the first column length is you could store the values in an array then finding the max col length along the way and print it all out in the END block.






        share|improve this answer













        In Awk you can use a "*" to generate a dynamic printf format string.



        If you know the length already you can pass the field length for the first column with -v.



        awk -vcol1=3 'BEGINFS="for(i=1;i<=NF;i++)if(i==1)printf "%*-s%s",col1,$i,FS;else if(i!=NF)printf "%s%s",$i,FS;else printf "%sn",$i;;' test.txt


        Note: if you didn't know what the first column length is you could store the values in an array then finding the max col length along the way and print it all out in the END block.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 7 hours ago









        A.DanischewskiA.Danischewski

        2642 silver badges7 bronze badges




        2642 silver badges7 bronze badges
























            Kayli O'Keefe is a new contributor. Be nice, and check out our Code of Conduct.









            draft saved

            draft discarded

















            Kayli O'Keefe is a new contributor. Be nice, and check out our Code of Conduct.












            Kayli O'Keefe is a new contributor. Be nice, and check out our Code of Conduct.











            Kayli O'Keefe is a new contributor. Be nice, and check out our Code of Conduct.














            Thanks for contributing an answer to Unix & Linux Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f541215%2fmodify-width-of-first-column-in-file-with-a-variable-number-of-fields-using-awk%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Canceling a color specificationRandomly assigning color to Graphics3D objects?Default color for Filling in Mathematica 9Coloring specific elements of sets with a prime modified order in an array plotHow to pick a color differing significantly from the colors already in a given color list?Detection of the text colorColor numbers based on their valueCan color schemes for use with ColorData include opacity specification?My dynamic color schemes

            Invision Community Contents History See also References External links Navigation menuProprietaryinvisioncommunity.comIPS Community ForumsIPS Community Forumsthis blog entry"License Changes, IP.Board 3.4, and the Future""Interview -- Matt Mecham of Ibforums""CEO Invision Power Board, Matt Mecham Is a Liar, Thief!"IPB License Explanation 1.3, 1.3.1, 2.0, and 2.1ArchivedSecurity Fixes, Updates And Enhancements For IPB 1.3.1Archived"New Demo Accounts - Invision Power Services"the original"New Default Skin"the original"Invision Power Board 3.0.0 and Applications Released"the original"Archived copy"the original"Perpetual licenses being done away with""Release Notes - Invision Power Services""Introducing: IPS Community Suite 4!"Invision Community Release Notes

            François Viète Contents Biography Work and thought Bibliography See also Notes Further reading External links Navigation menup. 21Google Bookspp. 75–77Google BooksDe thou (from University of Saint Andrews)ArchivedGoogle BooksGoogle BooksGoogle BooksGoogle booksGoogle Bookscc-parthenay.frL'histoire universelle (fr)Universal History (en)ArchivedAdsabs.harvard.eduPagesperso-orange.frArchive.orgChikara Sasaki. Descartes' mathematical thought p.259Google BooksGoogle BooksGoogle Bookspp. 152 and onwardGoogle BooksGoogle BooksScribd.comGoogle Books1257-7979Google BooksGoogle BooksGoogle BooksGoogle BooksGoogle BooksGoogle BooksGallica.bnf.frGoogle BooksGoogle Books"François Viète"Francois Viète: Father of Modern Algebraic NotationThe Lawyer and the GamblerAbout TarporleySite de Jean-Paul GuichardL'algèbre nouvelle"About the Harmonicon"cb120511976(data)1188044800000 0001 0913 5903n82164680ola2013766880073431702w6vt1sb70287374827140948071409480