How do I align columns at common elements but give differing elements their own rows?











up vote
1
down vote

favorite












I am using paste to merge three text files (which do not require sorting) into a single document with three columns.



paste a.txt b.txt c.txt


I'd like elements that the columns have in common to occupy the same row without sharing it with non-matching elements (which they currently do).
By the same token, unique elements should have their own rows. The elements in each column should preserve their original order.



Here's a simple example.



Input



1 1 1
2 2 2
3 4 4
5 5 5
1 1 2
3 3 3


Desired Output



1 1 1
2 2 2
3
4 4
5 5 5
1 1
2
3 3 3


Here's a more complicated example.



Input



000 000 000
002 002 001
006 006 006
008 008 007
009 009 009
011 012 010
013 013 013
015 015 014
016 016 016
018 019 017
020 020 020
021 021 022
024 024 024
026 025 025
028 026 026
118 028 027
119 118 118
032 119 117
036 032 032
037 033 033
039 034 034
040 037 037
042 039 038
043 040 040
045 042 041
046 043 043
048 045 044
046 046
049 047


Desired Output



000 000 000
001
002 002
006 006 006
007
008 008
009 009 009
010
011
012
013 013 013
014
015 015
016 016 016
017
018
019
020 020 020
021 021
022
024 024 024
025 025
026 026 026
027
028 028
118 118 118
117
119 119
032 032 032
033 033
034 034
036
037 037 037
038
039 039
040 040 040
041
042 042
043 043 043
044
045 045
046 046 046
047
048
049


Ideally, I'd like to use tools that are built in to Linux/Unix. I'd also like the output to remain a single document with three columns, e.g., > whatever.csv.



The closest I've been able to get is to run sdiff on the original text files, but although that correctly aligns elements that the files share in common, it does not handle the differences as I would like.










share|improve this question









New contributor




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




















  • You say that the original files do not require sorting. Would it be okay to sort them, individually?
    – Kusalananda
    Dec 5 at 18:11












  • It would be all right as long as each file/column retains its original order.
    – MilesO'Brien
    Dec 5 at 18:17















up vote
1
down vote

favorite












I am using paste to merge three text files (which do not require sorting) into a single document with three columns.



paste a.txt b.txt c.txt


I'd like elements that the columns have in common to occupy the same row without sharing it with non-matching elements (which they currently do).
By the same token, unique elements should have their own rows. The elements in each column should preserve their original order.



Here's a simple example.



Input



1 1 1
2 2 2
3 4 4
5 5 5
1 1 2
3 3 3


Desired Output



1 1 1
2 2 2
3
4 4
5 5 5
1 1
2
3 3 3


Here's a more complicated example.



Input



000 000 000
002 002 001
006 006 006
008 008 007
009 009 009
011 012 010
013 013 013
015 015 014
016 016 016
018 019 017
020 020 020
021 021 022
024 024 024
026 025 025
028 026 026
118 028 027
119 118 118
032 119 117
036 032 032
037 033 033
039 034 034
040 037 037
042 039 038
043 040 040
045 042 041
046 043 043
048 045 044
046 046
049 047


Desired Output



000 000 000
001
002 002
006 006 006
007
008 008
009 009 009
010
011
012
013 013 013
014
015 015
016 016 016
017
018
019
020 020 020
021 021
022
024 024 024
025 025
026 026 026
027
028 028
118 118 118
117
119 119
032 032 032
033 033
034 034
036
037 037 037
038
039 039
040 040 040
041
042 042
043 043 043
044
045 045
046 046 046
047
048
049


Ideally, I'd like to use tools that are built in to Linux/Unix. I'd also like the output to remain a single document with three columns, e.g., > whatever.csv.



The closest I've been able to get is to run sdiff on the original text files, but although that correctly aligns elements that the files share in common, it does not handle the differences as I would like.










share|improve this question









New contributor




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




















  • You say that the original files do not require sorting. Would it be okay to sort them, individually?
    – Kusalananda
    Dec 5 at 18:11












  • It would be all right as long as each file/column retains its original order.
    – MilesO'Brien
    Dec 5 at 18:17













up vote
1
down vote

favorite









up vote
1
down vote

favorite











I am using paste to merge three text files (which do not require sorting) into a single document with three columns.



paste a.txt b.txt c.txt


I'd like elements that the columns have in common to occupy the same row without sharing it with non-matching elements (which they currently do).
By the same token, unique elements should have their own rows. The elements in each column should preserve their original order.



Here's a simple example.



Input



1 1 1
2 2 2
3 4 4
5 5 5
1 1 2
3 3 3


Desired Output



1 1 1
2 2 2
3
4 4
5 5 5
1 1
2
3 3 3


Here's a more complicated example.



Input



000 000 000
002 002 001
006 006 006
008 008 007
009 009 009
011 012 010
013 013 013
015 015 014
016 016 016
018 019 017
020 020 020
021 021 022
024 024 024
026 025 025
028 026 026
118 028 027
119 118 118
032 119 117
036 032 032
037 033 033
039 034 034
040 037 037
042 039 038
043 040 040
045 042 041
046 043 043
048 045 044
046 046
049 047


Desired Output



000 000 000
001
002 002
006 006 006
007
008 008
009 009 009
010
011
012
013 013 013
014
015 015
016 016 016
017
018
019
020 020 020
021 021
022
024 024 024
025 025
026 026 026
027
028 028
118 118 118
117
119 119
032 032 032
033 033
034 034
036
037 037 037
038
039 039
040 040 040
041
042 042
043 043 043
044
045 045
046 046 046
047
048
049


Ideally, I'd like to use tools that are built in to Linux/Unix. I'd also like the output to remain a single document with three columns, e.g., > whatever.csv.



The closest I've been able to get is to run sdiff on the original text files, but although that correctly aligns elements that the files share in common, it does not handle the differences as I would like.










share|improve this question









New contributor




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











I am using paste to merge three text files (which do not require sorting) into a single document with three columns.



paste a.txt b.txt c.txt


I'd like elements that the columns have in common to occupy the same row without sharing it with non-matching elements (which they currently do).
By the same token, unique elements should have their own rows. The elements in each column should preserve their original order.



Here's a simple example.



Input



1 1 1
2 2 2
3 4 4
5 5 5
1 1 2
3 3 3


Desired Output



1 1 1
2 2 2
3
4 4
5 5 5
1 1
2
3 3 3


Here's a more complicated example.



Input



000 000 000
002 002 001
006 006 006
008 008 007
009 009 009
011 012 010
013 013 013
015 015 014
016 016 016
018 019 017
020 020 020
021 021 022
024 024 024
026 025 025
028 026 026
118 028 027
119 118 118
032 119 117
036 032 032
037 033 033
039 034 034
040 037 037
042 039 038
043 040 040
045 042 041
046 043 043
048 045 044
046 046
049 047


Desired Output



000 000 000
001
002 002
006 006 006
007
008 008
009 009 009
010
011
012
013 013 013
014
015 015
016 016 016
017
018
019
020 020 020
021 021
022
024 024 024
025 025
026 026 026
027
028 028
118 118 118
117
119 119
032 032 032
033 033
034 034
036
037 037 037
038
039 039
040 040 040
041
042 042
043 043 043
044
045 045
046 046 046
047
048
049


Ideally, I'd like to use tools that are built in to Linux/Unix. I'd also like the output to remain a single document with three columns, e.g., > whatever.csv.



The closest I've been able to get is to run sdiff on the original text files, but although that correctly aligns elements that the files share in common, it does not handle the differences as I would like.







linux text-formatting columns paste






share|improve this question









New contributor




MilesO'Brien 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




MilesO'Brien 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 2 days ago





















New contributor




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









asked Dec 5 at 18:03









MilesO'Brien

63




63




New contributor




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





New contributor





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






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












  • You say that the original files do not require sorting. Would it be okay to sort them, individually?
    – Kusalananda
    Dec 5 at 18:11












  • It would be all right as long as each file/column retains its original order.
    – MilesO'Brien
    Dec 5 at 18:17


















  • You say that the original files do not require sorting. Would it be okay to sort them, individually?
    – Kusalananda
    Dec 5 at 18:11












  • It would be all right as long as each file/column retains its original order.
    – MilesO'Brien
    Dec 5 at 18:17
















You say that the original files do not require sorting. Would it be okay to sort them, individually?
– Kusalananda
Dec 5 at 18:11






You say that the original files do not require sorting. Would it be okay to sort them, individually?
– Kusalananda
Dec 5 at 18:11














It would be all right as long as each file/column retains its original order.
– MilesO'Brien
Dec 5 at 18:17




It would be all right as long as each file/column retains its original order.
– MilesO'Brien
Dec 5 at 18:17










2 Answers
2






active

oldest

votes

















up vote
1
down vote













BEGIN {
# We assume the default input field separator (changeable with "-F")
# Output will be tab delimited.
OFS = "t"
}
{
# The number of output records that this input record results in.
k=0

# "seen" records which new record a field should be part of.
# There may be NF new records for each input record if all
# fields are unique.
delete seen

# "a" holds all data for the new output records.
# It's basically a 2-dimensional NFxNF matrix
# encodod in a 1-dimensional array.
delete a

# Iterate over the fields
for (i=1; i<=NF; ++i) {
if (!seen[$i]) {
# This data has not been seen before (in this input record),
# assign it to the next output line.

seen[$i] = ++k
}

# Assign the input field to the right spot
a[(seen[$i]-1)*NF + i] = $i
}

# Save NF as this is reset by emptying $0 later.
nf = NF

# Create and output new lines
for (j = 1; j<=k; ++j) {
$0 = ""

# Create new output record
for (i = 1; i<=nf; ++i)
$i = a[(j-1)*nf + i]

# Output record
print
}
}


Testing on the given data:



$ awk -f script.awk file
1 1 1
2 2 2
3
4 4
5 5 5
1 1
2
3 3 3


Testing on other data:



$ cat file
a b c e
1 2 1 1
2 1 1 1
1 1 1 2




$ awk -f script.awk file
a
b
c
e
1 1 1
2
2
1 1 1
1 1 1
2





share|improve this answer























  • This works on my example and on most of my data; consequently, it is the answer. It does not work with some data, doubtless because I have made an assumption of some kind. When I work out what that is, it is probably better to ask a new question that to edit the original post.
    – MilesO'Brien
    Dec 5 at 19:49










  • @MilesO'Brien It's either the handling of the input data, or your interpretation of the output data. It would be interesting to see a lite of data that you believe it does not handle.
    – Kusalananda
    Dec 5 at 21:08










  • To be clear, your script does what I described in the OP on all data that I feed it. It's just that I erred in my assumptions about the data and how to handle it. Now, I'm unsure whether I should start a new thread or add to the OP.
    – MilesO'Brien
    Dec 5 at 22:18










  • I have updated the OP with a more complicated example. On reflection, I have decided that the way in which I worded the OP is compatible with both examples.
    – MilesO'Brien
    2 days ago










  • @MilesO'Brien Ah, so it's the order in which the new lines are added that is wrong, I get it. I will mull over this for a while and make a change later today (I'm a bit busy with other work ATM).
    – Kusalananda
    2 days ago


















up vote
0
down vote













Here's a "brute force" solution in a shell script using paste and read.



#!/bin/sh

paste a.txt b.txt c.txt |
while read -r a b c; do
if [ "$a" = "$b" ] && [ "$b" = "$c" ]; then
printf '%st%st%sn' "$a" "$b" "$c"
elif [ "$a" = "$b" ]; then
printf '%st%sntt%sn' "$a" "$b" "$c"
elif [ "$a" = "$c" ]; then
printf '%stt%snt%sn' "$a" "$c" "$b"
elif [ "$b" = "$c" ]; then
printf '%snt%st%sn' "$a" "$b" "$c"
else
printf '%snt%sntt%sn' "$a" "$b" "$c"
fi
done


There's probably a more elegant solution but I couldn't come up with a good one off the top of my head.



You could probably use awk instead if you prefer it -- I think the result would look very similar. (One advantage of using awk would be that it could potentially do the job of paste at the same time, if that's useful to you.)






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: 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
    });


    }
    });






    MilesO'Brien 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%2f486207%2fhow-do-i-align-columns-at-common-elements-but-give-differing-elements-their-own%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








    up vote
    1
    down vote













    BEGIN {
    # We assume the default input field separator (changeable with "-F")
    # Output will be tab delimited.
    OFS = "t"
    }
    {
    # The number of output records that this input record results in.
    k=0

    # "seen" records which new record a field should be part of.
    # There may be NF new records for each input record if all
    # fields are unique.
    delete seen

    # "a" holds all data for the new output records.
    # It's basically a 2-dimensional NFxNF matrix
    # encodod in a 1-dimensional array.
    delete a

    # Iterate over the fields
    for (i=1; i<=NF; ++i) {
    if (!seen[$i]) {
    # This data has not been seen before (in this input record),
    # assign it to the next output line.

    seen[$i] = ++k
    }

    # Assign the input field to the right spot
    a[(seen[$i]-1)*NF + i] = $i
    }

    # Save NF as this is reset by emptying $0 later.
    nf = NF

    # Create and output new lines
    for (j = 1; j<=k; ++j) {
    $0 = ""

    # Create new output record
    for (i = 1; i<=nf; ++i)
    $i = a[(j-1)*nf + i]

    # Output record
    print
    }
    }


    Testing on the given data:



    $ awk -f script.awk file
    1 1 1
    2 2 2
    3
    4 4
    5 5 5
    1 1
    2
    3 3 3


    Testing on other data:



    $ cat file
    a b c e
    1 2 1 1
    2 1 1 1
    1 1 1 2




    $ awk -f script.awk file
    a
    b
    c
    e
    1 1 1
    2
    2
    1 1 1
    1 1 1
    2





    share|improve this answer























    • This works on my example and on most of my data; consequently, it is the answer. It does not work with some data, doubtless because I have made an assumption of some kind. When I work out what that is, it is probably better to ask a new question that to edit the original post.
      – MilesO'Brien
      Dec 5 at 19:49










    • @MilesO'Brien It's either the handling of the input data, or your interpretation of the output data. It would be interesting to see a lite of data that you believe it does not handle.
      – Kusalananda
      Dec 5 at 21:08










    • To be clear, your script does what I described in the OP on all data that I feed it. It's just that I erred in my assumptions about the data and how to handle it. Now, I'm unsure whether I should start a new thread or add to the OP.
      – MilesO'Brien
      Dec 5 at 22:18










    • I have updated the OP with a more complicated example. On reflection, I have decided that the way in which I worded the OP is compatible with both examples.
      – MilesO'Brien
      2 days ago










    • @MilesO'Brien Ah, so it's the order in which the new lines are added that is wrong, I get it. I will mull over this for a while and make a change later today (I'm a bit busy with other work ATM).
      – Kusalananda
      2 days ago















    up vote
    1
    down vote













    BEGIN {
    # We assume the default input field separator (changeable with "-F")
    # Output will be tab delimited.
    OFS = "t"
    }
    {
    # The number of output records that this input record results in.
    k=0

    # "seen" records which new record a field should be part of.
    # There may be NF new records for each input record if all
    # fields are unique.
    delete seen

    # "a" holds all data for the new output records.
    # It's basically a 2-dimensional NFxNF matrix
    # encodod in a 1-dimensional array.
    delete a

    # Iterate over the fields
    for (i=1; i<=NF; ++i) {
    if (!seen[$i]) {
    # This data has not been seen before (in this input record),
    # assign it to the next output line.

    seen[$i] = ++k
    }

    # Assign the input field to the right spot
    a[(seen[$i]-1)*NF + i] = $i
    }

    # Save NF as this is reset by emptying $0 later.
    nf = NF

    # Create and output new lines
    for (j = 1; j<=k; ++j) {
    $0 = ""

    # Create new output record
    for (i = 1; i<=nf; ++i)
    $i = a[(j-1)*nf + i]

    # Output record
    print
    }
    }


    Testing on the given data:



    $ awk -f script.awk file
    1 1 1
    2 2 2
    3
    4 4
    5 5 5
    1 1
    2
    3 3 3


    Testing on other data:



    $ cat file
    a b c e
    1 2 1 1
    2 1 1 1
    1 1 1 2




    $ awk -f script.awk file
    a
    b
    c
    e
    1 1 1
    2
    2
    1 1 1
    1 1 1
    2





    share|improve this answer























    • This works on my example and on most of my data; consequently, it is the answer. It does not work with some data, doubtless because I have made an assumption of some kind. When I work out what that is, it is probably better to ask a new question that to edit the original post.
      – MilesO'Brien
      Dec 5 at 19:49










    • @MilesO'Brien It's either the handling of the input data, or your interpretation of the output data. It would be interesting to see a lite of data that you believe it does not handle.
      – Kusalananda
      Dec 5 at 21:08










    • To be clear, your script does what I described in the OP on all data that I feed it. It's just that I erred in my assumptions about the data and how to handle it. Now, I'm unsure whether I should start a new thread or add to the OP.
      – MilesO'Brien
      Dec 5 at 22:18










    • I have updated the OP with a more complicated example. On reflection, I have decided that the way in which I worded the OP is compatible with both examples.
      – MilesO'Brien
      2 days ago










    • @MilesO'Brien Ah, so it's the order in which the new lines are added that is wrong, I get it. I will mull over this for a while and make a change later today (I'm a bit busy with other work ATM).
      – Kusalananda
      2 days ago













    up vote
    1
    down vote










    up vote
    1
    down vote









    BEGIN {
    # We assume the default input field separator (changeable with "-F")
    # Output will be tab delimited.
    OFS = "t"
    }
    {
    # The number of output records that this input record results in.
    k=0

    # "seen" records which new record a field should be part of.
    # There may be NF new records for each input record if all
    # fields are unique.
    delete seen

    # "a" holds all data for the new output records.
    # It's basically a 2-dimensional NFxNF matrix
    # encodod in a 1-dimensional array.
    delete a

    # Iterate over the fields
    for (i=1; i<=NF; ++i) {
    if (!seen[$i]) {
    # This data has not been seen before (in this input record),
    # assign it to the next output line.

    seen[$i] = ++k
    }

    # Assign the input field to the right spot
    a[(seen[$i]-1)*NF + i] = $i
    }

    # Save NF as this is reset by emptying $0 later.
    nf = NF

    # Create and output new lines
    for (j = 1; j<=k; ++j) {
    $0 = ""

    # Create new output record
    for (i = 1; i<=nf; ++i)
    $i = a[(j-1)*nf + i]

    # Output record
    print
    }
    }


    Testing on the given data:



    $ awk -f script.awk file
    1 1 1
    2 2 2
    3
    4 4
    5 5 5
    1 1
    2
    3 3 3


    Testing on other data:



    $ cat file
    a b c e
    1 2 1 1
    2 1 1 1
    1 1 1 2




    $ awk -f script.awk file
    a
    b
    c
    e
    1 1 1
    2
    2
    1 1 1
    1 1 1
    2





    share|improve this answer














    BEGIN {
    # We assume the default input field separator (changeable with "-F")
    # Output will be tab delimited.
    OFS = "t"
    }
    {
    # The number of output records that this input record results in.
    k=0

    # "seen" records which new record a field should be part of.
    # There may be NF new records for each input record if all
    # fields are unique.
    delete seen

    # "a" holds all data for the new output records.
    # It's basically a 2-dimensional NFxNF matrix
    # encodod in a 1-dimensional array.
    delete a

    # Iterate over the fields
    for (i=1; i<=NF; ++i) {
    if (!seen[$i]) {
    # This data has not been seen before (in this input record),
    # assign it to the next output line.

    seen[$i] = ++k
    }

    # Assign the input field to the right spot
    a[(seen[$i]-1)*NF + i] = $i
    }

    # Save NF as this is reset by emptying $0 later.
    nf = NF

    # Create and output new lines
    for (j = 1; j<=k; ++j) {
    $0 = ""

    # Create new output record
    for (i = 1; i<=nf; ++i)
    $i = a[(j-1)*nf + i]

    # Output record
    print
    }
    }


    Testing on the given data:



    $ awk -f script.awk file
    1 1 1
    2 2 2
    3
    4 4
    5 5 5
    1 1
    2
    3 3 3


    Testing on other data:



    $ cat file
    a b c e
    1 2 1 1
    2 1 1 1
    1 1 1 2




    $ awk -f script.awk file
    a
    b
    c
    e
    1 1 1
    2
    2
    1 1 1
    1 1 1
    2






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Dec 5 at 19:08

























    answered Dec 5 at 19:02









    Kusalananda

    119k16223364




    119k16223364












    • This works on my example and on most of my data; consequently, it is the answer. It does not work with some data, doubtless because I have made an assumption of some kind. When I work out what that is, it is probably better to ask a new question that to edit the original post.
      – MilesO'Brien
      Dec 5 at 19:49










    • @MilesO'Brien It's either the handling of the input data, or your interpretation of the output data. It would be interesting to see a lite of data that you believe it does not handle.
      – Kusalananda
      Dec 5 at 21:08










    • To be clear, your script does what I described in the OP on all data that I feed it. It's just that I erred in my assumptions about the data and how to handle it. Now, I'm unsure whether I should start a new thread or add to the OP.
      – MilesO'Brien
      Dec 5 at 22:18










    • I have updated the OP with a more complicated example. On reflection, I have decided that the way in which I worded the OP is compatible with both examples.
      – MilesO'Brien
      2 days ago










    • @MilesO'Brien Ah, so it's the order in which the new lines are added that is wrong, I get it. I will mull over this for a while and make a change later today (I'm a bit busy with other work ATM).
      – Kusalananda
      2 days ago


















    • This works on my example and on most of my data; consequently, it is the answer. It does not work with some data, doubtless because I have made an assumption of some kind. When I work out what that is, it is probably better to ask a new question that to edit the original post.
      – MilesO'Brien
      Dec 5 at 19:49










    • @MilesO'Brien It's either the handling of the input data, or your interpretation of the output data. It would be interesting to see a lite of data that you believe it does not handle.
      – Kusalananda
      Dec 5 at 21:08










    • To be clear, your script does what I described in the OP on all data that I feed it. It's just that I erred in my assumptions about the data and how to handle it. Now, I'm unsure whether I should start a new thread or add to the OP.
      – MilesO'Brien
      Dec 5 at 22:18










    • I have updated the OP with a more complicated example. On reflection, I have decided that the way in which I worded the OP is compatible with both examples.
      – MilesO'Brien
      2 days ago










    • @MilesO'Brien Ah, so it's the order in which the new lines are added that is wrong, I get it. I will mull over this for a while and make a change later today (I'm a bit busy with other work ATM).
      – Kusalananda
      2 days ago
















    This works on my example and on most of my data; consequently, it is the answer. It does not work with some data, doubtless because I have made an assumption of some kind. When I work out what that is, it is probably better to ask a new question that to edit the original post.
    – MilesO'Brien
    Dec 5 at 19:49




    This works on my example and on most of my data; consequently, it is the answer. It does not work with some data, doubtless because I have made an assumption of some kind. When I work out what that is, it is probably better to ask a new question that to edit the original post.
    – MilesO'Brien
    Dec 5 at 19:49












    @MilesO'Brien It's either the handling of the input data, or your interpretation of the output data. It would be interesting to see a lite of data that you believe it does not handle.
    – Kusalananda
    Dec 5 at 21:08




    @MilesO'Brien It's either the handling of the input data, or your interpretation of the output data. It would be interesting to see a lite of data that you believe it does not handle.
    – Kusalananda
    Dec 5 at 21:08












    To be clear, your script does what I described in the OP on all data that I feed it. It's just that I erred in my assumptions about the data and how to handle it. Now, I'm unsure whether I should start a new thread or add to the OP.
    – MilesO'Brien
    Dec 5 at 22:18




    To be clear, your script does what I described in the OP on all data that I feed it. It's just that I erred in my assumptions about the data and how to handle it. Now, I'm unsure whether I should start a new thread or add to the OP.
    – MilesO'Brien
    Dec 5 at 22:18












    I have updated the OP with a more complicated example. On reflection, I have decided that the way in which I worded the OP is compatible with both examples.
    – MilesO'Brien
    2 days ago




    I have updated the OP with a more complicated example. On reflection, I have decided that the way in which I worded the OP is compatible with both examples.
    – MilesO'Brien
    2 days ago












    @MilesO'Brien Ah, so it's the order in which the new lines are added that is wrong, I get it. I will mull over this for a while and make a change later today (I'm a bit busy with other work ATM).
    – Kusalananda
    2 days ago




    @MilesO'Brien Ah, so it's the order in which the new lines are added that is wrong, I get it. I will mull over this for a while and make a change later today (I'm a bit busy with other work ATM).
    – Kusalananda
    2 days ago












    up vote
    0
    down vote













    Here's a "brute force" solution in a shell script using paste and read.



    #!/bin/sh

    paste a.txt b.txt c.txt |
    while read -r a b c; do
    if [ "$a" = "$b" ] && [ "$b" = "$c" ]; then
    printf '%st%st%sn' "$a" "$b" "$c"
    elif [ "$a" = "$b" ]; then
    printf '%st%sntt%sn' "$a" "$b" "$c"
    elif [ "$a" = "$c" ]; then
    printf '%stt%snt%sn' "$a" "$c" "$b"
    elif [ "$b" = "$c" ]; then
    printf '%snt%st%sn' "$a" "$b" "$c"
    else
    printf '%snt%sntt%sn' "$a" "$b" "$c"
    fi
    done


    There's probably a more elegant solution but I couldn't come up with a good one off the top of my head.



    You could probably use awk instead if you prefer it -- I think the result would look very similar. (One advantage of using awk would be that it could potentially do the job of paste at the same time, if that's useful to you.)






    share|improve this answer

























      up vote
      0
      down vote













      Here's a "brute force" solution in a shell script using paste and read.



      #!/bin/sh

      paste a.txt b.txt c.txt |
      while read -r a b c; do
      if [ "$a" = "$b" ] && [ "$b" = "$c" ]; then
      printf '%st%st%sn' "$a" "$b" "$c"
      elif [ "$a" = "$b" ]; then
      printf '%st%sntt%sn' "$a" "$b" "$c"
      elif [ "$a" = "$c" ]; then
      printf '%stt%snt%sn' "$a" "$c" "$b"
      elif [ "$b" = "$c" ]; then
      printf '%snt%st%sn' "$a" "$b" "$c"
      else
      printf '%snt%sntt%sn' "$a" "$b" "$c"
      fi
      done


      There's probably a more elegant solution but I couldn't come up with a good one off the top of my head.



      You could probably use awk instead if you prefer it -- I think the result would look very similar. (One advantage of using awk would be that it could potentially do the job of paste at the same time, if that's useful to you.)






      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        Here's a "brute force" solution in a shell script using paste and read.



        #!/bin/sh

        paste a.txt b.txt c.txt |
        while read -r a b c; do
        if [ "$a" = "$b" ] && [ "$b" = "$c" ]; then
        printf '%st%st%sn' "$a" "$b" "$c"
        elif [ "$a" = "$b" ]; then
        printf '%st%sntt%sn' "$a" "$b" "$c"
        elif [ "$a" = "$c" ]; then
        printf '%stt%snt%sn' "$a" "$c" "$b"
        elif [ "$b" = "$c" ]; then
        printf '%snt%st%sn' "$a" "$b" "$c"
        else
        printf '%snt%sntt%sn' "$a" "$b" "$c"
        fi
        done


        There's probably a more elegant solution but I couldn't come up with a good one off the top of my head.



        You could probably use awk instead if you prefer it -- I think the result would look very similar. (One advantage of using awk would be that it could potentially do the job of paste at the same time, if that's useful to you.)






        share|improve this answer












        Here's a "brute force" solution in a shell script using paste and read.



        #!/bin/sh

        paste a.txt b.txt c.txt |
        while read -r a b c; do
        if [ "$a" = "$b" ] && [ "$b" = "$c" ]; then
        printf '%st%st%sn' "$a" "$b" "$c"
        elif [ "$a" = "$b" ]; then
        printf '%st%sntt%sn' "$a" "$b" "$c"
        elif [ "$a" = "$c" ]; then
        printf '%stt%snt%sn' "$a" "$c" "$b"
        elif [ "$b" = "$c" ]; then
        printf '%snt%st%sn' "$a" "$b" "$c"
        else
        printf '%snt%sntt%sn' "$a" "$b" "$c"
        fi
        done


        There's probably a more elegant solution but I couldn't come up with a good one off the top of my head.



        You could probably use awk instead if you prefer it -- I think the result would look very similar. (One advantage of using awk would be that it could potentially do the job of paste at the same time, if that's useful to you.)







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 5 at 18:32









        Daniel Pryden

        1215




        1215






















            MilesO'Brien is a new contributor. Be nice, and check out our Code of Conduct.










            draft saved

            draft discarded


















            MilesO'Brien is a new contributor. Be nice, and check out our Code of Conduct.













            MilesO'Brien is a new contributor. Be nice, and check out our Code of Conduct.












            MilesO'Brien 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.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • 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%2f486207%2fhow-do-i-align-columns-at-common-elements-but-give-differing-elements-their-own%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

            サソリ

            広島県道265号伴広島線

            Accessing regular linux commands in Huawei's Dopra Linux