awk how to sort within a row from left to right within row?












0















I have a project I am using awk to try to resolve. I'm new to awk and still learning. I know you can write functions in awk but I'm not exaxtly sure how to do it. I have a large datafile with xml type tags. Each row refers to a unique item and contains a different number of attribute fields. I need to sort the rows based on numbered tags in that row from lowest to highest and remove duplicates. Is it possible to do this within each row using awk??



<ITEM ID='81'>,< 1>KWIKSET</1>,< 2>PASS</2>,< 7>KNOB</7>,< 5a>RIGHT</5a>,< 8c>BRASS</8c>,< 2>TYLO</2>,< 2>PASS</2>,< 5a>RIGHT</5a>,< 8c>BRASS</8c>
<ITEM ID='82'>,< 1>KWIKSET</1>,< 4a>PRIVACY</4a>,< 7>KNOB</7>,< 8b>SATIN</8b>,< 8c>CHROME</8c>,< 2>TYLO</2>,< 4a>PRIVACY</4a>,< 8b>SATIN</8b>,< 8c>CHROME</8c>
<ITEM ID='83'>,< 1>KWIKSET</1>,< 8b>POLISHED</8b>,< 8c>BRASS</8c>









share|improve this question

























  • Ummm ... why do feel you have to make that ugly again (undo my edit)?

    – tink
    Jan 10 at 4:06











  • Oh sorry... was trying to figure out how to make it show the code... it's cutting some of what I'm posting... :-|

    – Mike
    Jan 10 at 4:08











  • stackoverflow.com/questions/25896237/sort-fields-within-a-line

    – tink
    Jan 10 at 4:22






  • 1





    thanks tink... this looks like exactly what I was looking for. :)

    – Mike
    Jan 10 at 4:28
















0















I have a project I am using awk to try to resolve. I'm new to awk and still learning. I know you can write functions in awk but I'm not exaxtly sure how to do it. I have a large datafile with xml type tags. Each row refers to a unique item and contains a different number of attribute fields. I need to sort the rows based on numbered tags in that row from lowest to highest and remove duplicates. Is it possible to do this within each row using awk??



<ITEM ID='81'>,< 1>KWIKSET</1>,< 2>PASS</2>,< 7>KNOB</7>,< 5a>RIGHT</5a>,< 8c>BRASS</8c>,< 2>TYLO</2>,< 2>PASS</2>,< 5a>RIGHT</5a>,< 8c>BRASS</8c>
<ITEM ID='82'>,< 1>KWIKSET</1>,< 4a>PRIVACY</4a>,< 7>KNOB</7>,< 8b>SATIN</8b>,< 8c>CHROME</8c>,< 2>TYLO</2>,< 4a>PRIVACY</4a>,< 8b>SATIN</8b>,< 8c>CHROME</8c>
<ITEM ID='83'>,< 1>KWIKSET</1>,< 8b>POLISHED</8b>,< 8c>BRASS</8c>









share|improve this question

























  • Ummm ... why do feel you have to make that ugly again (undo my edit)?

    – tink
    Jan 10 at 4:06











  • Oh sorry... was trying to figure out how to make it show the code... it's cutting some of what I'm posting... :-|

    – Mike
    Jan 10 at 4:08











  • stackoverflow.com/questions/25896237/sort-fields-within-a-line

    – tink
    Jan 10 at 4:22






  • 1





    thanks tink... this looks like exactly what I was looking for. :)

    – Mike
    Jan 10 at 4:28














0












0








0








I have a project I am using awk to try to resolve. I'm new to awk and still learning. I know you can write functions in awk but I'm not exaxtly sure how to do it. I have a large datafile with xml type tags. Each row refers to a unique item and contains a different number of attribute fields. I need to sort the rows based on numbered tags in that row from lowest to highest and remove duplicates. Is it possible to do this within each row using awk??



<ITEM ID='81'>,< 1>KWIKSET</1>,< 2>PASS</2>,< 7>KNOB</7>,< 5a>RIGHT</5a>,< 8c>BRASS</8c>,< 2>TYLO</2>,< 2>PASS</2>,< 5a>RIGHT</5a>,< 8c>BRASS</8c>
<ITEM ID='82'>,< 1>KWIKSET</1>,< 4a>PRIVACY</4a>,< 7>KNOB</7>,< 8b>SATIN</8b>,< 8c>CHROME</8c>,< 2>TYLO</2>,< 4a>PRIVACY</4a>,< 8b>SATIN</8b>,< 8c>CHROME</8c>
<ITEM ID='83'>,< 1>KWIKSET</1>,< 8b>POLISHED</8b>,< 8c>BRASS</8c>









share|improve this question
















I have a project I am using awk to try to resolve. I'm new to awk and still learning. I know you can write functions in awk but I'm not exaxtly sure how to do it. I have a large datafile with xml type tags. Each row refers to a unique item and contains a different number of attribute fields. I need to sort the rows based on numbered tags in that row from lowest to highest and remove duplicates. Is it possible to do this within each row using awk??



<ITEM ID='81'>,< 1>KWIKSET</1>,< 2>PASS</2>,< 7>KNOB</7>,< 5a>RIGHT</5a>,< 8c>BRASS</8c>,< 2>TYLO</2>,< 2>PASS</2>,< 5a>RIGHT</5a>,< 8c>BRASS</8c>
<ITEM ID='82'>,< 1>KWIKSET</1>,< 4a>PRIVACY</4a>,< 7>KNOB</7>,< 8b>SATIN</8b>,< 8c>CHROME</8c>,< 2>TYLO</2>,< 4a>PRIVACY</4a>,< 8b>SATIN</8b>,< 8c>CHROME</8c>
<ITEM ID='83'>,< 1>KWIKSET</1>,< 8b>POLISHED</8b>,< 8c>BRASS</8c>






awk sort






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 10 at 4:17







Mike

















asked Jan 10 at 3:30









MikeMike

12




12













  • Ummm ... why do feel you have to make that ugly again (undo my edit)?

    – tink
    Jan 10 at 4:06











  • Oh sorry... was trying to figure out how to make it show the code... it's cutting some of what I'm posting... :-|

    – Mike
    Jan 10 at 4:08











  • stackoverflow.com/questions/25896237/sort-fields-within-a-line

    – tink
    Jan 10 at 4:22






  • 1





    thanks tink... this looks like exactly what I was looking for. :)

    – Mike
    Jan 10 at 4:28



















  • Ummm ... why do feel you have to make that ugly again (undo my edit)?

    – tink
    Jan 10 at 4:06











  • Oh sorry... was trying to figure out how to make it show the code... it's cutting some of what I'm posting... :-|

    – Mike
    Jan 10 at 4:08











  • stackoverflow.com/questions/25896237/sort-fields-within-a-line

    – tink
    Jan 10 at 4:22






  • 1





    thanks tink... this looks like exactly what I was looking for. :)

    – Mike
    Jan 10 at 4:28

















Ummm ... why do feel you have to make that ugly again (undo my edit)?

– tink
Jan 10 at 4:06





Ummm ... why do feel you have to make that ugly again (undo my edit)?

– tink
Jan 10 at 4:06













Oh sorry... was trying to figure out how to make it show the code... it's cutting some of what I'm posting... :-|

– Mike
Jan 10 at 4:08





Oh sorry... was trying to figure out how to make it show the code... it's cutting some of what I'm posting... :-|

– Mike
Jan 10 at 4:08













stackoverflow.com/questions/25896237/sort-fields-within-a-line

– tink
Jan 10 at 4:22





stackoverflow.com/questions/25896237/sort-fields-within-a-line

– tink
Jan 10 at 4:22




1




1





thanks tink... this looks like exactly what I was looking for. :)

– Mike
Jan 10 at 4:28





thanks tink... this looks like exactly what I was looking for. :)

– Mike
Jan 10 at 4:28










2 Answers
2






active

oldest

votes


















0














Is there any reason you are solely using awk? To solve your problem, you need to first split the data into each unit, sort, remove duplicates, and then rejoin. While you could do this with most any capable programming or scripting language (even C), is it really worth reinventing the wheel when there's already tools captable of doing what you need?



If the data you posted is a real representation of the data you are working with, you can quickly process it with the following commands:



$ cat RAW_DATA
<ITEM ID='81'>,< 1>KWIKSET</1>,< 2>PASS</2>,< 7>KNOB</7>,< 5a>RIGHT</5a>,< 8c>BRASS</8c>,< 2>TYLO</2>,< 2>PASS</2>,< 5a>RIGHT</5a>,< 8c>BRASS</8c>
<ITEM ID='82'>,< 1>KWIKSET</1>,< 4a>PRIVACY</4a>,< 7>KNOB</7>,< 8b>SATIN</8b>,< 8c>CHROME</8c>,< 2>TYLO</2>,< 4a>PRIVACY</4a>,< 8b>SATIN</8b>,< 8c>CHROME</8c>
<ITEM ID='83'>,< 1>KWIKSET</1>,< 8b>POLISHED</8b>,< 8c>BRASS</8c>
$ while read line; do echo "$(cut -d, -f1 <<< "$line"),$(cut -d, -f2- <<< "$line" | tr ',' 'n' | sort -n | uniq | paste -sd,)"; done < RAW_DATA
<ITEM ID='81'>,< 1>KWIKSET</1>,< 2>PASS</2>,< 2>TYLO</2>,< 5a>RIGHT</5a>,< 7>KNOB</7>,< 8c>BRASS</8c>
<ITEM ID='82'>,< 1>KWIKSET</1>,< 2>TYLO</2>,< 4a>PRIVACY</4a>,< 7>KNOB</7>,< 8b>SATIN</8b>,< 8c>CHROME</8c>
<ITEM ID='83'>,< 1>KWIKSET</1>,< 8b>POLISHED</8b>,< 8c>BRASS</8c>


The while loop reads through each line of the file and processes it separately. Then we want to echo back the new line where cut -d, -f1 <<< "$line" extracts just the first field as it's static and cut -d, -f2- <<< "$line" | tr ',' 'n' | sort -n | uniq | paste -sd, extracts the remaining fields, sorts them numerically, filters for unique values, and uses paste -sd, to rejoin as a comma-delimited list.






share|improve this answer


























  • He initially had more than one row of data (a table). With that your tr approach would be .... odd? :)

    – tink
    Jan 10 at 4:10











  • I'm not especially attached to awk, if there's a better way to do this... it's just what I know best... but I'm up to learn if there's a better way... :-)

    – Mike
    Jan 10 at 4:11











  • I have 20,000+ rows to go through daily... it needs to sort within the row from left to right, but not top to bottom.

    – Mike
    Jan 10 at 4:12











  • @Mike Please update your question with a correct example of your data and I can update my answer accordingly

    – Jonathan Rouleau
    Jan 10 at 4:13











  • Thanks Jonathan. I'm having problems trying to get it to show up correctly. There are xml tags. The first tag seems deleted, and the rest of the closing tags as well. Is there a better way to format this so it shows up??? I've tried code, html, etc.... it still looks choppy... this is the actual data

    – Mike
    Jan 10 at 4:15





















0














I'd use perl for this:



perl -MList::Util=uniq -F, -lane '
$item = shift @F;
@fields = uniq sort @F;
print join ",", $item, @fields;
' file


outputs:



<ITEM ID='81'>,< 1>KWIKSET</1>,< 2>PASS</2>,< 2>TYLO</2>,< 5a>RIGHT</5a>,< 7>KNOB</7>,< 8c>BRASS</8c>
<ITEM ID='82'>,< 1>KWIKSET</1>,< 2>TYLO</2>,< 4a>PRIVACY</4a>,< 7>KNOB</7>,< 8b>SATIN</8b>,< 8c>CHROME</8c>
<ITEM ID='83'>,< 1>KWIKSET</1>,< 8b>POLISHED</8b>,< 8c>BRASS</8c>


It can be made into an even more incomprehensible one-liner:



perl -MList::Util=uniq -F, -lape '$"=","; $_="@{[$F[0], uniq sort @F[1..$#F]]}"' file


I hope the tag contents do not contain commas.






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/3.0/"u003ecc by-sa 3.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
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f493599%2fawk-how-to-sort-within-a-row-from-left-to-right-within-row%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Is there any reason you are solely using awk? To solve your problem, you need to first split the data into each unit, sort, remove duplicates, and then rejoin. While you could do this with most any capable programming or scripting language (even C), is it really worth reinventing the wheel when there's already tools captable of doing what you need?



    If the data you posted is a real representation of the data you are working with, you can quickly process it with the following commands:



    $ cat RAW_DATA
    <ITEM ID='81'>,< 1>KWIKSET</1>,< 2>PASS</2>,< 7>KNOB</7>,< 5a>RIGHT</5a>,< 8c>BRASS</8c>,< 2>TYLO</2>,< 2>PASS</2>,< 5a>RIGHT</5a>,< 8c>BRASS</8c>
    <ITEM ID='82'>,< 1>KWIKSET</1>,< 4a>PRIVACY</4a>,< 7>KNOB</7>,< 8b>SATIN</8b>,< 8c>CHROME</8c>,< 2>TYLO</2>,< 4a>PRIVACY</4a>,< 8b>SATIN</8b>,< 8c>CHROME</8c>
    <ITEM ID='83'>,< 1>KWIKSET</1>,< 8b>POLISHED</8b>,< 8c>BRASS</8c>
    $ while read line; do echo "$(cut -d, -f1 <<< "$line"),$(cut -d, -f2- <<< "$line" | tr ',' 'n' | sort -n | uniq | paste -sd,)"; done < RAW_DATA
    <ITEM ID='81'>,< 1>KWIKSET</1>,< 2>PASS</2>,< 2>TYLO</2>,< 5a>RIGHT</5a>,< 7>KNOB</7>,< 8c>BRASS</8c>
    <ITEM ID='82'>,< 1>KWIKSET</1>,< 2>TYLO</2>,< 4a>PRIVACY</4a>,< 7>KNOB</7>,< 8b>SATIN</8b>,< 8c>CHROME</8c>
    <ITEM ID='83'>,< 1>KWIKSET</1>,< 8b>POLISHED</8b>,< 8c>BRASS</8c>


    The while loop reads through each line of the file and processes it separately. Then we want to echo back the new line where cut -d, -f1 <<< "$line" extracts just the first field as it's static and cut -d, -f2- <<< "$line" | tr ',' 'n' | sort -n | uniq | paste -sd, extracts the remaining fields, sorts them numerically, filters for unique values, and uses paste -sd, to rejoin as a comma-delimited list.






    share|improve this answer


























    • He initially had more than one row of data (a table). With that your tr approach would be .... odd? :)

      – tink
      Jan 10 at 4:10











    • I'm not especially attached to awk, if there's a better way to do this... it's just what I know best... but I'm up to learn if there's a better way... :-)

      – Mike
      Jan 10 at 4:11











    • I have 20,000+ rows to go through daily... it needs to sort within the row from left to right, but not top to bottom.

      – Mike
      Jan 10 at 4:12











    • @Mike Please update your question with a correct example of your data and I can update my answer accordingly

      – Jonathan Rouleau
      Jan 10 at 4:13











    • Thanks Jonathan. I'm having problems trying to get it to show up correctly. There are xml tags. The first tag seems deleted, and the rest of the closing tags as well. Is there a better way to format this so it shows up??? I've tried code, html, etc.... it still looks choppy... this is the actual data

      – Mike
      Jan 10 at 4:15


















    0














    Is there any reason you are solely using awk? To solve your problem, you need to first split the data into each unit, sort, remove duplicates, and then rejoin. While you could do this with most any capable programming or scripting language (even C), is it really worth reinventing the wheel when there's already tools captable of doing what you need?



    If the data you posted is a real representation of the data you are working with, you can quickly process it with the following commands:



    $ cat RAW_DATA
    <ITEM ID='81'>,< 1>KWIKSET</1>,< 2>PASS</2>,< 7>KNOB</7>,< 5a>RIGHT</5a>,< 8c>BRASS</8c>,< 2>TYLO</2>,< 2>PASS</2>,< 5a>RIGHT</5a>,< 8c>BRASS</8c>
    <ITEM ID='82'>,< 1>KWIKSET</1>,< 4a>PRIVACY</4a>,< 7>KNOB</7>,< 8b>SATIN</8b>,< 8c>CHROME</8c>,< 2>TYLO</2>,< 4a>PRIVACY</4a>,< 8b>SATIN</8b>,< 8c>CHROME</8c>
    <ITEM ID='83'>,< 1>KWIKSET</1>,< 8b>POLISHED</8b>,< 8c>BRASS</8c>
    $ while read line; do echo "$(cut -d, -f1 <<< "$line"),$(cut -d, -f2- <<< "$line" | tr ',' 'n' | sort -n | uniq | paste -sd,)"; done < RAW_DATA
    <ITEM ID='81'>,< 1>KWIKSET</1>,< 2>PASS</2>,< 2>TYLO</2>,< 5a>RIGHT</5a>,< 7>KNOB</7>,< 8c>BRASS</8c>
    <ITEM ID='82'>,< 1>KWIKSET</1>,< 2>TYLO</2>,< 4a>PRIVACY</4a>,< 7>KNOB</7>,< 8b>SATIN</8b>,< 8c>CHROME</8c>
    <ITEM ID='83'>,< 1>KWIKSET</1>,< 8b>POLISHED</8b>,< 8c>BRASS</8c>


    The while loop reads through each line of the file and processes it separately. Then we want to echo back the new line where cut -d, -f1 <<< "$line" extracts just the first field as it's static and cut -d, -f2- <<< "$line" | tr ',' 'n' | sort -n | uniq | paste -sd, extracts the remaining fields, sorts them numerically, filters for unique values, and uses paste -sd, to rejoin as a comma-delimited list.






    share|improve this answer


























    • He initially had more than one row of data (a table). With that your tr approach would be .... odd? :)

      – tink
      Jan 10 at 4:10











    • I'm not especially attached to awk, if there's a better way to do this... it's just what I know best... but I'm up to learn if there's a better way... :-)

      – Mike
      Jan 10 at 4:11











    • I have 20,000+ rows to go through daily... it needs to sort within the row from left to right, but not top to bottom.

      – Mike
      Jan 10 at 4:12











    • @Mike Please update your question with a correct example of your data and I can update my answer accordingly

      – Jonathan Rouleau
      Jan 10 at 4:13











    • Thanks Jonathan. I'm having problems trying to get it to show up correctly. There are xml tags. The first tag seems deleted, and the rest of the closing tags as well. Is there a better way to format this so it shows up??? I've tried code, html, etc.... it still looks choppy... this is the actual data

      – Mike
      Jan 10 at 4:15
















    0












    0








    0







    Is there any reason you are solely using awk? To solve your problem, you need to first split the data into each unit, sort, remove duplicates, and then rejoin. While you could do this with most any capable programming or scripting language (even C), is it really worth reinventing the wheel when there's already tools captable of doing what you need?



    If the data you posted is a real representation of the data you are working with, you can quickly process it with the following commands:



    $ cat RAW_DATA
    <ITEM ID='81'>,< 1>KWIKSET</1>,< 2>PASS</2>,< 7>KNOB</7>,< 5a>RIGHT</5a>,< 8c>BRASS</8c>,< 2>TYLO</2>,< 2>PASS</2>,< 5a>RIGHT</5a>,< 8c>BRASS</8c>
    <ITEM ID='82'>,< 1>KWIKSET</1>,< 4a>PRIVACY</4a>,< 7>KNOB</7>,< 8b>SATIN</8b>,< 8c>CHROME</8c>,< 2>TYLO</2>,< 4a>PRIVACY</4a>,< 8b>SATIN</8b>,< 8c>CHROME</8c>
    <ITEM ID='83'>,< 1>KWIKSET</1>,< 8b>POLISHED</8b>,< 8c>BRASS</8c>
    $ while read line; do echo "$(cut -d, -f1 <<< "$line"),$(cut -d, -f2- <<< "$line" | tr ',' 'n' | sort -n | uniq | paste -sd,)"; done < RAW_DATA
    <ITEM ID='81'>,< 1>KWIKSET</1>,< 2>PASS</2>,< 2>TYLO</2>,< 5a>RIGHT</5a>,< 7>KNOB</7>,< 8c>BRASS</8c>
    <ITEM ID='82'>,< 1>KWIKSET</1>,< 2>TYLO</2>,< 4a>PRIVACY</4a>,< 7>KNOB</7>,< 8b>SATIN</8b>,< 8c>CHROME</8c>
    <ITEM ID='83'>,< 1>KWIKSET</1>,< 8b>POLISHED</8b>,< 8c>BRASS</8c>


    The while loop reads through each line of the file and processes it separately. Then we want to echo back the new line where cut -d, -f1 <<< "$line" extracts just the first field as it's static and cut -d, -f2- <<< "$line" | tr ',' 'n' | sort -n | uniq | paste -sd, extracts the remaining fields, sorts them numerically, filters for unique values, and uses paste -sd, to rejoin as a comma-delimited list.






    share|improve this answer















    Is there any reason you are solely using awk? To solve your problem, you need to first split the data into each unit, sort, remove duplicates, and then rejoin. While you could do this with most any capable programming or scripting language (even C), is it really worth reinventing the wheel when there's already tools captable of doing what you need?



    If the data you posted is a real representation of the data you are working with, you can quickly process it with the following commands:



    $ cat RAW_DATA
    <ITEM ID='81'>,< 1>KWIKSET</1>,< 2>PASS</2>,< 7>KNOB</7>,< 5a>RIGHT</5a>,< 8c>BRASS</8c>,< 2>TYLO</2>,< 2>PASS</2>,< 5a>RIGHT</5a>,< 8c>BRASS</8c>
    <ITEM ID='82'>,< 1>KWIKSET</1>,< 4a>PRIVACY</4a>,< 7>KNOB</7>,< 8b>SATIN</8b>,< 8c>CHROME</8c>,< 2>TYLO</2>,< 4a>PRIVACY</4a>,< 8b>SATIN</8b>,< 8c>CHROME</8c>
    <ITEM ID='83'>,< 1>KWIKSET</1>,< 8b>POLISHED</8b>,< 8c>BRASS</8c>
    $ while read line; do echo "$(cut -d, -f1 <<< "$line"),$(cut -d, -f2- <<< "$line" | tr ',' 'n' | sort -n | uniq | paste -sd,)"; done < RAW_DATA
    <ITEM ID='81'>,< 1>KWIKSET</1>,< 2>PASS</2>,< 2>TYLO</2>,< 5a>RIGHT</5a>,< 7>KNOB</7>,< 8c>BRASS</8c>
    <ITEM ID='82'>,< 1>KWIKSET</1>,< 2>TYLO</2>,< 4a>PRIVACY</4a>,< 7>KNOB</7>,< 8b>SATIN</8b>,< 8c>CHROME</8c>
    <ITEM ID='83'>,< 1>KWIKSET</1>,< 8b>POLISHED</8b>,< 8c>BRASS</8c>


    The while loop reads through each line of the file and processes it separately. Then we want to echo back the new line where cut -d, -f1 <<< "$line" extracts just the first field as it's static and cut -d, -f2- <<< "$line" | tr ',' 'n' | sort -n | uniq | paste -sd, extracts the remaining fields, sorts them numerically, filters for unique values, and uses paste -sd, to rejoin as a comma-delimited list.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Jan 10 at 4:55

























    answered Jan 10 at 4:07









    Jonathan RouleauJonathan Rouleau

    744




    744













    • He initially had more than one row of data (a table). With that your tr approach would be .... odd? :)

      – tink
      Jan 10 at 4:10











    • I'm not especially attached to awk, if there's a better way to do this... it's just what I know best... but I'm up to learn if there's a better way... :-)

      – Mike
      Jan 10 at 4:11











    • I have 20,000+ rows to go through daily... it needs to sort within the row from left to right, but not top to bottom.

      – Mike
      Jan 10 at 4:12











    • @Mike Please update your question with a correct example of your data and I can update my answer accordingly

      – Jonathan Rouleau
      Jan 10 at 4:13











    • Thanks Jonathan. I'm having problems trying to get it to show up correctly. There are xml tags. The first tag seems deleted, and the rest of the closing tags as well. Is there a better way to format this so it shows up??? I've tried code, html, etc.... it still looks choppy... this is the actual data

      – Mike
      Jan 10 at 4:15





















    • He initially had more than one row of data (a table). With that your tr approach would be .... odd? :)

      – tink
      Jan 10 at 4:10











    • I'm not especially attached to awk, if there's a better way to do this... it's just what I know best... but I'm up to learn if there's a better way... :-)

      – Mike
      Jan 10 at 4:11











    • I have 20,000+ rows to go through daily... it needs to sort within the row from left to right, but not top to bottom.

      – Mike
      Jan 10 at 4:12











    • @Mike Please update your question with a correct example of your data and I can update my answer accordingly

      – Jonathan Rouleau
      Jan 10 at 4:13











    • Thanks Jonathan. I'm having problems trying to get it to show up correctly. There are xml tags. The first tag seems deleted, and the rest of the closing tags as well. Is there a better way to format this so it shows up??? I've tried code, html, etc.... it still looks choppy... this is the actual data

      – Mike
      Jan 10 at 4:15



















    He initially had more than one row of data (a table). With that your tr approach would be .... odd? :)

    – tink
    Jan 10 at 4:10





    He initially had more than one row of data (a table). With that your tr approach would be .... odd? :)

    – tink
    Jan 10 at 4:10













    I'm not especially attached to awk, if there's a better way to do this... it's just what I know best... but I'm up to learn if there's a better way... :-)

    – Mike
    Jan 10 at 4:11





    I'm not especially attached to awk, if there's a better way to do this... it's just what I know best... but I'm up to learn if there's a better way... :-)

    – Mike
    Jan 10 at 4:11













    I have 20,000+ rows to go through daily... it needs to sort within the row from left to right, but not top to bottom.

    – Mike
    Jan 10 at 4:12





    I have 20,000+ rows to go through daily... it needs to sort within the row from left to right, but not top to bottom.

    – Mike
    Jan 10 at 4:12













    @Mike Please update your question with a correct example of your data and I can update my answer accordingly

    – Jonathan Rouleau
    Jan 10 at 4:13





    @Mike Please update your question with a correct example of your data and I can update my answer accordingly

    – Jonathan Rouleau
    Jan 10 at 4:13













    Thanks Jonathan. I'm having problems trying to get it to show up correctly. There are xml tags. The first tag seems deleted, and the rest of the closing tags as well. Is there a better way to format this so it shows up??? I've tried code, html, etc.... it still looks choppy... this is the actual data

    – Mike
    Jan 10 at 4:15







    Thanks Jonathan. I'm having problems trying to get it to show up correctly. There are xml tags. The first tag seems deleted, and the rest of the closing tags as well. Is there a better way to format this so it shows up??? I've tried code, html, etc.... it still looks choppy... this is the actual data

    – Mike
    Jan 10 at 4:15















    0














    I'd use perl for this:



    perl -MList::Util=uniq -F, -lane '
    $item = shift @F;
    @fields = uniq sort @F;
    print join ",", $item, @fields;
    ' file


    outputs:



    <ITEM ID='81'>,< 1>KWIKSET</1>,< 2>PASS</2>,< 2>TYLO</2>,< 5a>RIGHT</5a>,< 7>KNOB</7>,< 8c>BRASS</8c>
    <ITEM ID='82'>,< 1>KWIKSET</1>,< 2>TYLO</2>,< 4a>PRIVACY</4a>,< 7>KNOB</7>,< 8b>SATIN</8b>,< 8c>CHROME</8c>
    <ITEM ID='83'>,< 1>KWIKSET</1>,< 8b>POLISHED</8b>,< 8c>BRASS</8c>


    It can be made into an even more incomprehensible one-liner:



    perl -MList::Util=uniq -F, -lape '$"=","; $_="@{[$F[0], uniq sort @F[1..$#F]]}"' file


    I hope the tag contents do not contain commas.






    share|improve this answer




























      0














      I'd use perl for this:



      perl -MList::Util=uniq -F, -lane '
      $item = shift @F;
      @fields = uniq sort @F;
      print join ",", $item, @fields;
      ' file


      outputs:



      <ITEM ID='81'>,< 1>KWIKSET</1>,< 2>PASS</2>,< 2>TYLO</2>,< 5a>RIGHT</5a>,< 7>KNOB</7>,< 8c>BRASS</8c>
      <ITEM ID='82'>,< 1>KWIKSET</1>,< 2>TYLO</2>,< 4a>PRIVACY</4a>,< 7>KNOB</7>,< 8b>SATIN</8b>,< 8c>CHROME</8c>
      <ITEM ID='83'>,< 1>KWIKSET</1>,< 8b>POLISHED</8b>,< 8c>BRASS</8c>


      It can be made into an even more incomprehensible one-liner:



      perl -MList::Util=uniq -F, -lape '$"=","; $_="@{[$F[0], uniq sort @F[1..$#F]]}"' file


      I hope the tag contents do not contain commas.






      share|improve this answer


























        0












        0








        0







        I'd use perl for this:



        perl -MList::Util=uniq -F, -lane '
        $item = shift @F;
        @fields = uniq sort @F;
        print join ",", $item, @fields;
        ' file


        outputs:



        <ITEM ID='81'>,< 1>KWIKSET</1>,< 2>PASS</2>,< 2>TYLO</2>,< 5a>RIGHT</5a>,< 7>KNOB</7>,< 8c>BRASS</8c>
        <ITEM ID='82'>,< 1>KWIKSET</1>,< 2>TYLO</2>,< 4a>PRIVACY</4a>,< 7>KNOB</7>,< 8b>SATIN</8b>,< 8c>CHROME</8c>
        <ITEM ID='83'>,< 1>KWIKSET</1>,< 8b>POLISHED</8b>,< 8c>BRASS</8c>


        It can be made into an even more incomprehensible one-liner:



        perl -MList::Util=uniq -F, -lape '$"=","; $_="@{[$F[0], uniq sort @F[1..$#F]]}"' file


        I hope the tag contents do not contain commas.






        share|improve this answer













        I'd use perl for this:



        perl -MList::Util=uniq -F, -lane '
        $item = shift @F;
        @fields = uniq sort @F;
        print join ",", $item, @fields;
        ' file


        outputs:



        <ITEM ID='81'>,< 1>KWIKSET</1>,< 2>PASS</2>,< 2>TYLO</2>,< 5a>RIGHT</5a>,< 7>KNOB</7>,< 8c>BRASS</8c>
        <ITEM ID='82'>,< 1>KWIKSET</1>,< 2>TYLO</2>,< 4a>PRIVACY</4a>,< 7>KNOB</7>,< 8b>SATIN</8b>,< 8c>CHROME</8c>
        <ITEM ID='83'>,< 1>KWIKSET</1>,< 8b>POLISHED</8b>,< 8c>BRASS</8c>


        It can be made into an even more incomprehensible one-liner:



        perl -MList::Util=uniq -F, -lape '$"=","; $_="@{[$F[0], uniq sort @F[1..$#F]]}"' file


        I hope the tag contents do not contain commas.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 11 at 0:27









        glenn jackmanglenn jackman

        50.8k571109




        50.8k571109






























            draft saved

            draft discarded




















































            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%2f493599%2fawk-how-to-sort-within-a-row-from-left-to-right-within-row%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

            How to make a Squid Proxy server?

            Is this a new Fibonacci Identity?

            19世紀