column manipulation using awk

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
3
down vote

favorite












I have a file with more than 200 columns. As for example purpose, I am here using a file with less number of columns(9). Below is the input file(few lines)



chr10 181243 225933 1 1 1 10 0 36
chr10 181500 225933 1 1 1 106 0 35
chr10 226069 255828 1 1 1 57 0 37
chr10 243946 255828 1 1 1 4 0 27
chr10 255989 267134 1 1 1 87 0 32
chr10 255989 282777 1 1 1 61 0 34
chr10 267297 282777 1 1 1 61 0 37
chr10 282856 283524 1 1 1 92 0 35
chr10 282856 285377 1 1 1 1 0 15
chr10 283618 285377 1 1 1 72 0 33


I want to rearrange the file such that my last column(here 9th column) is the 4th column in the output file and then print everything else. So the output I am looking for is



chr10 181243 225933 36 1 1 1 10 0
chr10 181500 225933 35 1 1 1 106 0
chr10 226069 255828 37 1 1 1 57 0
chr10 243946 255828 27 1 1 1 4 0
chr10 255989 267134 32 1 1 1 87 0
chr10 255989 282777 34 1 1 1 61 0
chr10 267297 282777 37 1 1 1 61 0
chr10 282856 283524 35 1 1 1 92 0
chr10 282856 285377 15 1 1 1 1 0
chr10 283618 285377 33 1 1 1 72 0


On a file with less number of columns, I can use something like this to achieve the above output:



awk -v OFS="t" 'print $1,$2,$3,$9,$4,$5,$6,$7,$8'


If now I have a file with large number of columns, how can I place the last column of the file as the 4th column and rest I print as it is?










share|improve this question























  • @JeffSchaller, pretty clear from the input/output sample that column 9 should be moved and "shunted" in between columns 3 and 4, forcing the rest over one slot.
    – Wildcard
    1 hour ago










  • In the output file, column9 are all zeroes because column 8 in the input was all zeroes
    – user3138373
    1 hour ago










  • Apologies for the confusion; I saw "rearrange" and assumed "swap"
    – Jeff Schaller
    1 hour ago














up vote
3
down vote

favorite












I have a file with more than 200 columns. As for example purpose, I am here using a file with less number of columns(9). Below is the input file(few lines)



chr10 181243 225933 1 1 1 10 0 36
chr10 181500 225933 1 1 1 106 0 35
chr10 226069 255828 1 1 1 57 0 37
chr10 243946 255828 1 1 1 4 0 27
chr10 255989 267134 1 1 1 87 0 32
chr10 255989 282777 1 1 1 61 0 34
chr10 267297 282777 1 1 1 61 0 37
chr10 282856 283524 1 1 1 92 0 35
chr10 282856 285377 1 1 1 1 0 15
chr10 283618 285377 1 1 1 72 0 33


I want to rearrange the file such that my last column(here 9th column) is the 4th column in the output file and then print everything else. So the output I am looking for is



chr10 181243 225933 36 1 1 1 10 0
chr10 181500 225933 35 1 1 1 106 0
chr10 226069 255828 37 1 1 1 57 0
chr10 243946 255828 27 1 1 1 4 0
chr10 255989 267134 32 1 1 1 87 0
chr10 255989 282777 34 1 1 1 61 0
chr10 267297 282777 37 1 1 1 61 0
chr10 282856 283524 35 1 1 1 92 0
chr10 282856 285377 15 1 1 1 1 0
chr10 283618 285377 33 1 1 1 72 0


On a file with less number of columns, I can use something like this to achieve the above output:



awk -v OFS="t" 'print $1,$2,$3,$9,$4,$5,$6,$7,$8'


If now I have a file with large number of columns, how can I place the last column of the file as the 4th column and rest I print as it is?










share|improve this question























  • @JeffSchaller, pretty clear from the input/output sample that column 9 should be moved and "shunted" in between columns 3 and 4, forcing the rest over one slot.
    – Wildcard
    1 hour ago










  • In the output file, column9 are all zeroes because column 8 in the input was all zeroes
    – user3138373
    1 hour ago










  • Apologies for the confusion; I saw "rearrange" and assumed "swap"
    – Jeff Schaller
    1 hour ago












up vote
3
down vote

favorite









up vote
3
down vote

favorite











I have a file with more than 200 columns. As for example purpose, I am here using a file with less number of columns(9). Below is the input file(few lines)



chr10 181243 225933 1 1 1 10 0 36
chr10 181500 225933 1 1 1 106 0 35
chr10 226069 255828 1 1 1 57 0 37
chr10 243946 255828 1 1 1 4 0 27
chr10 255989 267134 1 1 1 87 0 32
chr10 255989 282777 1 1 1 61 0 34
chr10 267297 282777 1 1 1 61 0 37
chr10 282856 283524 1 1 1 92 0 35
chr10 282856 285377 1 1 1 1 0 15
chr10 283618 285377 1 1 1 72 0 33


I want to rearrange the file such that my last column(here 9th column) is the 4th column in the output file and then print everything else. So the output I am looking for is



chr10 181243 225933 36 1 1 1 10 0
chr10 181500 225933 35 1 1 1 106 0
chr10 226069 255828 37 1 1 1 57 0
chr10 243946 255828 27 1 1 1 4 0
chr10 255989 267134 32 1 1 1 87 0
chr10 255989 282777 34 1 1 1 61 0
chr10 267297 282777 37 1 1 1 61 0
chr10 282856 283524 35 1 1 1 92 0
chr10 282856 285377 15 1 1 1 1 0
chr10 283618 285377 33 1 1 1 72 0


On a file with less number of columns, I can use something like this to achieve the above output:



awk -v OFS="t" 'print $1,$2,$3,$9,$4,$5,$6,$7,$8'


If now I have a file with large number of columns, how can I place the last column of the file as the 4th column and rest I print as it is?










share|improve this question















I have a file with more than 200 columns. As for example purpose, I am here using a file with less number of columns(9). Below is the input file(few lines)



chr10 181243 225933 1 1 1 10 0 36
chr10 181500 225933 1 1 1 106 0 35
chr10 226069 255828 1 1 1 57 0 37
chr10 243946 255828 1 1 1 4 0 27
chr10 255989 267134 1 1 1 87 0 32
chr10 255989 282777 1 1 1 61 0 34
chr10 267297 282777 1 1 1 61 0 37
chr10 282856 283524 1 1 1 92 0 35
chr10 282856 285377 1 1 1 1 0 15
chr10 283618 285377 1 1 1 72 0 33


I want to rearrange the file such that my last column(here 9th column) is the 4th column in the output file and then print everything else. So the output I am looking for is



chr10 181243 225933 36 1 1 1 10 0
chr10 181500 225933 35 1 1 1 106 0
chr10 226069 255828 37 1 1 1 57 0
chr10 243946 255828 27 1 1 1 4 0
chr10 255989 267134 32 1 1 1 87 0
chr10 255989 282777 34 1 1 1 61 0
chr10 267297 282777 37 1 1 1 61 0
chr10 282856 283524 35 1 1 1 92 0
chr10 282856 285377 15 1 1 1 1 0
chr10 283618 285377 33 1 1 1 72 0


On a file with less number of columns, I can use something like this to achieve the above output:



awk -v OFS="t" 'print $1,$2,$3,$9,$4,$5,$6,$7,$8'


If now I have a file with large number of columns, how can I place the last column of the file as the 4th column and rest I print as it is?







awk






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 1 hour ago









Jeff Schaller

32.3k849109




32.3k849109










asked 1 hour ago









user3138373

82541430




82541430











  • @JeffSchaller, pretty clear from the input/output sample that column 9 should be moved and "shunted" in between columns 3 and 4, forcing the rest over one slot.
    – Wildcard
    1 hour ago










  • In the output file, column9 are all zeroes because column 8 in the input was all zeroes
    – user3138373
    1 hour ago










  • Apologies for the confusion; I saw "rearrange" and assumed "swap"
    – Jeff Schaller
    1 hour ago
















  • @JeffSchaller, pretty clear from the input/output sample that column 9 should be moved and "shunted" in between columns 3 and 4, forcing the rest over one slot.
    – Wildcard
    1 hour ago










  • In the output file, column9 are all zeroes because column 8 in the input was all zeroes
    – user3138373
    1 hour ago










  • Apologies for the confusion; I saw "rearrange" and assumed "swap"
    – Jeff Schaller
    1 hour ago















@JeffSchaller, pretty clear from the input/output sample that column 9 should be moved and "shunted" in between columns 3 and 4, forcing the rest over one slot.
– Wildcard
1 hour ago




@JeffSchaller, pretty clear from the input/output sample that column 9 should be moved and "shunted" in between columns 3 and 4, forcing the rest over one slot.
– Wildcard
1 hour ago












In the output file, column9 are all zeroes because column 8 in the input was all zeroes
– user3138373
1 hour ago




In the output file, column9 are all zeroes because column 8 in the input was all zeroes
– user3138373
1 hour ago












Apologies for the confusion; I saw "rearrange" and assumed "swap"
– Jeff Schaller
1 hour ago




Apologies for the confusion; I saw "rearrange" and assumed "swap"
– Jeff Schaller
1 hour ago










2 Answers
2






active

oldest

votes

















up vote
2
down vote



accepted










To replace the fourth field with the last field:



awk -v OFS="t" ' $4 = $NF; $NF=""; print 


To insert the last field before the fourth field, we have to get a little creative:



awk -v OFS="t" 'temp=$NF; for( i=NF;i>4;i-- ) $i=$(i-1); $4=temp ; print'


This will preserve the final field, zip through all the fields and move each one backward to the fourth forward one, and then put the desired fourth field into place:



$ echo 1..10 | awk -v OFS="t" 'temp=$NF; for( i=NF;i>4;i-- ) $i=$(i-1); $4=temp ; print'
1 2 3 10 4 5 6 7 8 9





share|improve this answer






















  • The request was to use the last column, and it was pointed out in the question that the number of fields in the actual data is not nine.
    – DopeGhoti
    1 hour ago






  • 1




    No, this overwrites the value in column 4. Look at the example input/output and you'll see the intention is to leave column 4 in existence, but move it over to column 5.
    – Wildcard
    1 hour ago










  • Alternative which inserts rather than overwrites is now provided.
    – DopeGhoti
    1 hour ago

















up vote
2
down vote













Perl is very concise for this: split each line into words, pop off the last word and insert it at
index 3 (0-based)



$ perl -lane 'splice @F, 3, 0, pop(@F); print "@F"' file | column -t
chr10 181243 225933 36 1 1 1 10 0
chr10 181500 225933 35 1 1 1 106 0
...





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',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f468879%2fcolumn-manipulation-using-awk%23new-answer', 'question_page');

    );

    Post as a guest






























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote



    accepted










    To replace the fourth field with the last field:



    awk -v OFS="t" ' $4 = $NF; $NF=""; print 


    To insert the last field before the fourth field, we have to get a little creative:



    awk -v OFS="t" 'temp=$NF; for( i=NF;i>4;i-- ) $i=$(i-1); $4=temp ; print'


    This will preserve the final field, zip through all the fields and move each one backward to the fourth forward one, and then put the desired fourth field into place:



    $ echo 1..10 | awk -v OFS="t" 'temp=$NF; for( i=NF;i>4;i-- ) $i=$(i-1); $4=temp ; print'
    1 2 3 10 4 5 6 7 8 9





    share|improve this answer






















    • The request was to use the last column, and it was pointed out in the question that the number of fields in the actual data is not nine.
      – DopeGhoti
      1 hour ago






    • 1




      No, this overwrites the value in column 4. Look at the example input/output and you'll see the intention is to leave column 4 in existence, but move it over to column 5.
      – Wildcard
      1 hour ago










    • Alternative which inserts rather than overwrites is now provided.
      – DopeGhoti
      1 hour ago














    up vote
    2
    down vote



    accepted










    To replace the fourth field with the last field:



    awk -v OFS="t" ' $4 = $NF; $NF=""; print 


    To insert the last field before the fourth field, we have to get a little creative:



    awk -v OFS="t" 'temp=$NF; for( i=NF;i>4;i-- ) $i=$(i-1); $4=temp ; print'


    This will preserve the final field, zip through all the fields and move each one backward to the fourth forward one, and then put the desired fourth field into place:



    $ echo 1..10 | awk -v OFS="t" 'temp=$NF; for( i=NF;i>4;i-- ) $i=$(i-1); $4=temp ; print'
    1 2 3 10 4 5 6 7 8 9





    share|improve this answer






















    • The request was to use the last column, and it was pointed out in the question that the number of fields in the actual data is not nine.
      – DopeGhoti
      1 hour ago






    • 1




      No, this overwrites the value in column 4. Look at the example input/output and you'll see the intention is to leave column 4 in existence, but move it over to column 5.
      – Wildcard
      1 hour ago










    • Alternative which inserts rather than overwrites is now provided.
      – DopeGhoti
      1 hour ago












    up vote
    2
    down vote



    accepted







    up vote
    2
    down vote



    accepted






    To replace the fourth field with the last field:



    awk -v OFS="t" ' $4 = $NF; $NF=""; print 


    To insert the last field before the fourth field, we have to get a little creative:



    awk -v OFS="t" 'temp=$NF; for( i=NF;i>4;i-- ) $i=$(i-1); $4=temp ; print'


    This will preserve the final field, zip through all the fields and move each one backward to the fourth forward one, and then put the desired fourth field into place:



    $ echo 1..10 | awk -v OFS="t" 'temp=$NF; for( i=NF;i>4;i-- ) $i=$(i-1); $4=temp ; print'
    1 2 3 10 4 5 6 7 8 9





    share|improve this answer














    To replace the fourth field with the last field:



    awk -v OFS="t" ' $4 = $NF; $NF=""; print 


    To insert the last field before the fourth field, we have to get a little creative:



    awk -v OFS="t" 'temp=$NF; for( i=NF;i>4;i-- ) $i=$(i-1); $4=temp ; print'


    This will preserve the final field, zip through all the fields and move each one backward to the fourth forward one, and then put the desired fourth field into place:



    $ echo 1..10 | awk -v OFS="t" 'temp=$NF; for( i=NF;i>4;i-- ) $i=$(i-1); $4=temp ; print'
    1 2 3 10 4 5 6 7 8 9






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited 1 hour ago

























    answered 1 hour ago









    DopeGhoti

    40.9k55080




    40.9k55080











    • The request was to use the last column, and it was pointed out in the question that the number of fields in the actual data is not nine.
      – DopeGhoti
      1 hour ago






    • 1




      No, this overwrites the value in column 4. Look at the example input/output and you'll see the intention is to leave column 4 in existence, but move it over to column 5.
      – Wildcard
      1 hour ago










    • Alternative which inserts rather than overwrites is now provided.
      – DopeGhoti
      1 hour ago
















    • The request was to use the last column, and it was pointed out in the question that the number of fields in the actual data is not nine.
      – DopeGhoti
      1 hour ago






    • 1




      No, this overwrites the value in column 4. Look at the example input/output and you'll see the intention is to leave column 4 in existence, but move it over to column 5.
      – Wildcard
      1 hour ago










    • Alternative which inserts rather than overwrites is now provided.
      – DopeGhoti
      1 hour ago















    The request was to use the last column, and it was pointed out in the question that the number of fields in the actual data is not nine.
    – DopeGhoti
    1 hour ago




    The request was to use the last column, and it was pointed out in the question that the number of fields in the actual data is not nine.
    – DopeGhoti
    1 hour ago




    1




    1




    No, this overwrites the value in column 4. Look at the example input/output and you'll see the intention is to leave column 4 in existence, but move it over to column 5.
    – Wildcard
    1 hour ago




    No, this overwrites the value in column 4. Look at the example input/output and you'll see the intention is to leave column 4 in existence, but move it over to column 5.
    – Wildcard
    1 hour ago












    Alternative which inserts rather than overwrites is now provided.
    – DopeGhoti
    1 hour ago




    Alternative which inserts rather than overwrites is now provided.
    – DopeGhoti
    1 hour ago












    up vote
    2
    down vote













    Perl is very concise for this: split each line into words, pop off the last word and insert it at
    index 3 (0-based)



    $ perl -lane 'splice @F, 3, 0, pop(@F); print "@F"' file | column -t
    chr10 181243 225933 36 1 1 1 10 0
    chr10 181500 225933 35 1 1 1 106 0
    ...





    share|improve this answer
























      up vote
      2
      down vote













      Perl is very concise for this: split each line into words, pop off the last word and insert it at
      index 3 (0-based)



      $ perl -lane 'splice @F, 3, 0, pop(@F); print "@F"' file | column -t
      chr10 181243 225933 36 1 1 1 10 0
      chr10 181500 225933 35 1 1 1 106 0
      ...





      share|improve this answer






















        up vote
        2
        down vote










        up vote
        2
        down vote









        Perl is very concise for this: split each line into words, pop off the last word and insert it at
        index 3 (0-based)



        $ perl -lane 'splice @F, 3, 0, pop(@F); print "@F"' file | column -t
        chr10 181243 225933 36 1 1 1 10 0
        chr10 181500 225933 35 1 1 1 106 0
        ...





        share|improve this answer












        Perl is very concise for this: split each line into words, pop off the last word and insert it at
        index 3 (0-based)



        $ perl -lane 'splice @F, 3, 0, pop(@F); print "@F"' file | column -t
        chr10 181243 225933 36 1 1 1 10 0
        chr10 181500 225933 35 1 1 1 106 0
        ...






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 36 mins ago









        glenn jackman

        47.3k265103




        47.3k265103



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f468879%2fcolumn-manipulation-using-awk%23new-answer', 'question_page');

            );

            Post as a guest













































































            Comments

            Popular posts from this blog

            Long meetings (6-7 hours a day): Being “babysat” by supervisor

            What does second last employer means? [closed]

            One-line joke