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

Does a gnoll speak both Gnoll and Abyssal, or is Gnoll a dialect of Abyssal?

Why did they ever make smaller than full-frame sensors?

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

Why would "an mule" be used instead of "a mule"?

When was the earliest opportunity the Voyager crew had to return to the Alpha quadrant?

Why is the Digital 0 not 0V in computer systems?

Can I conceal an antihero's insanity - and should I?

Is low emotional intelligence associated with right-wing and prejudiced attitudes?

How can I discourage sharing internal API keys within a company?

Why is Kirchoff's loop rule true in a DC circuit?

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

Writing a love interest for my hero

How to stabilise the bicycle seatpost and saddle when it is all the way up?

Gravity on an Orbital Ring

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

Telling my mother that I have anorexia without panicking her

Are Democrats more likely to believe Astrology is a science?

Sample Inverse Color in Photoshop

Glue or not to glue boots

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

A medieval fantasy adventurer lights a torch in a 100% pure oxygen room. What happens?

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

How to say "quirky" in German without sounding derogatory?

Double it your way



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



...'printf "%-3s' --> 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
    8 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



...'printf "%-3s' --> 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
    8 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



...'printf "%-3s' --> 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



...'printf "%-3s' --> 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
    8 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
    8 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
8 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
8 hours ago











4 Answers
4






active

oldest

votes


















7
















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



Ex.



$ awk 'BEGINOFS=FS=" $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 'BEGINOFS=FS="$1=sprintf("%-*s",f1,$1)1' test.txt

    – A.Danischewski
    7 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 OFS = FS = "
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 OFS = FS = "
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
    6 hours ago


















0
















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



$ awk -F'|' '",$1; for(i=2;i<NF;i++)printf "%s printf "%sn", $i' 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
    5 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 'BEGIN"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 'BEGINOFS=FS=" $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 'BEGINOFS=FS="$1=sprintf("%-*s",f1,$1)1' test.txt

      – A.Danischewski
      7 hours ago















    7
















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



    Ex.



    $ awk 'BEGINOFS=FS=" $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 'BEGINOFS=FS="$1=sprintf("%-*s",f1,$1)1' test.txt

      – A.Danischewski
      7 hours ago













    7














    7










    7









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



    Ex.



    $ awk 'BEGINOFS=FS=" $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 'BEGINOFS=FS=" $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 'BEGINOFS=FS="$1=sprintf("%-*s",f1,$1)1' test.txt

      – A.Danischewski
      7 hours ago

















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

      – A.Danischewski
      7 hours ago
















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

    – A.Danischewski
    7 hours ago





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

    – A.Danischewski
    7 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 OFS = FS = "
    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 OFS = FS = "
    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
      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 OFS = FS = "
    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 OFS = FS = "
    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
      6 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 OFS = FS = "
    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 OFS = FS = "
    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 OFS = FS = "
    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 OFS = FS = "
    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 6 hours ago

























    answered 8 hours ago









    KusalanandaKusalananda

    163k19 gold badges321 silver badges508 bronze badges




    163k19 gold badges321 silver badges508 bronze badges










    • 1





      @EdMorton Yes, neat, thanks.

      – Kusalananda
      6 hours ago












    • 1





      @EdMorton Yes, neat, thanks.

      – Kusalananda
      6 hours ago







    1




    1





    @EdMorton Yes, neat, thanks.

    – Kusalananda
    6 hours ago





    @EdMorton Yes, neat, thanks.

    – Kusalananda
    6 hours ago











    0
















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



    $ awk -F'|' '",$1; for(i=2;i<NF;i++)printf "%s printf "%sn", $i' 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
      5 hours ago















    0
















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



    $ awk -F'|' '",$1; for(i=2;i<NF;i++)printf "%s printf "%sn", $i' 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
      5 hours ago













    0














    0










    0









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



    $ awk -F'|' '",$1; for(i=2;i<NF;i++)printf "%s printf "%sn", $i' 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'|' '",$1; for(i=2;i<NF;i++)printf "%s printf "%sn", $i' 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 8 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
      5 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
      5 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
    5 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
    5 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 'BEGIN"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 'BEGIN"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 'BEGIN"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 'BEGIN"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

            Sahara Skak | Bilen | Luke uk diar | NawigatsjuunCommonskategorii: SaharaWikivoyage raisfeerer: Sahara26° N, 13° O

            The fall designs the understood secretary. Looking glass Science Shock Discovery Hot Everybody Loves Raymond Smile 곳 서비스 성실하다 Defas Kaloolon Definition: To combine or impregnate with sulphur or any of its compounds as to sulphurize caoutchouc in vulcanizing Flame colored Reason Useful Thin Help 갖다 유명하다 낙엽 장례식 Country Iron Definition: A fencer a gladiator one who exhibits his skill in the use of the sword Definition: The American black throated bunting Spiza Americana Nostalgic Needy Method to my madness 시키다 평가되다 전부 소설가 우아하다 Argument Tin Feeling Representative Gym Music Gaur Chicken 일쑤 코치 편 학생증 The harbor values the sugar. Vasagle Yammoe Enstatite Definition: Capable of being limited Road Neighborly Five Refer Built Kangaroo 비비다 Degree Release Bargain Horse 하루 형님 유교 석 동부 괴롭히다 경제력

            19. јануар Садржај Догађаји Рођења Смрти Празници и дани сећања Види још Референце Мени за навигацијуу