Bash - pair each line of file2019 Community Moderator ElectionCommand line tool to “cat” pairwise expansion of all rows in a fileCost-efficiently pair each line of a file with all othersHow to rewrite multiline path into one-line relative pathMerge two files line by line with the delimiter triple pipe symbol “|||”How do I remove all but the file name (with no extension) from a full file path?Insert string or line after last instance of a specific search variable, in a loopcopy every line from a text file that contains a number greater than 5000How to insert a line from file A above the FIRST LINE in file Bextract lines from a file based on sequential pair of patterns, and output to separate filesscript to parse file for two consecutive lines of unequal lengthHow to add a path before the first character of each line in a file.txt and save the same file with those edits?How can I add 10 lines from a file (file2) to another one after 2 lines (file1)?

When to use snap-off blade knife and when to use trapezoid blade knife?

gerund and noun applications

How can an organ that provides biological immortality be unable to regenerate?

Variable completely messes up echoed string

What does "^L" mean in C?

I got the following comment from a reputed math journal. What does it mean?

Am I eligible for the Eurail Youth pass? I am 27.5 years old

Generic TVP tradeoffs?

What can I do if I am asked to learn different programming languages very frequently?

Why is there so much iron?

Can other pieces capture a threatening piece and prevent a checkmate?

Existance of a celestial body big enough for early civilization to be thought of as a second moon

Unfrosted light bulb

How does one measure the Fourier components of a signal?

Print a physical multiplication table

How is the partial sum of a geometric sequence calculated?

Violin - Can double stops be played when the strings are not next to each other?

Light propagating through a sound wave

Is there a term for accumulated dirt on the outside of your hands and feet?

Could Sinn Fein swing any Brexit vote in Parliament?

What does "Four-F." mean?

In Aliens, how many people were on LV-426 before the Marines arrived​?

The average age of first marriage in Russia

PTIJ What is the inyan of the Konami code in Uncle Moishy's song?



Bash - pair each line of file



2019 Community Moderator ElectionCommand line tool to “cat” pairwise expansion of all rows in a fileCost-efficiently pair each line of a file with all othersHow to rewrite multiline path into one-line relative pathMerge two files line by line with the delimiter triple pipe symbol “|||”How do I remove all but the file name (with no extension) from a full file path?Insert string or line after last instance of a specific search variable, in a loopcopy every line from a text file that contains a number greater than 5000How to insert a line from file A above the FIRST LINE in file Bextract lines from a file based on sequential pair of patterns, and output to separate filesscript to parse file for two consecutive lines of unequal lengthHow to add a path before the first character of each line in a file.txt and save the same file with those edits?How can I add 10 lines from a file (file2) to another one after 2 lines (file1)?










9















This question is strongly related to this and this question. I have a file that contains several lines where each line is a path to a file. Now I want to pair each line with each different line (not itself). Also a pair A B is equal to a B A pair for my purposes, so only one of these combinations should be produced.



Example



files.dat reads like this in a shorthand notation, each letter is a file path (absolute or relative)



a
b
c
d
e


Then my result should look something like this:



a b
a c
a d
a e
b c
b d
b e
c d
c e
d e


Preferrably I would like to solve this in bash. Unlike the other questions, my file list is rather small (about 200 lines), so using loops and RAM capacity
pose no problems.










share|improve this question









New contributor




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




















  • Does it have to be in bash proper, or just something available via the bash commandline? Other utilities are better positioned to process text.

    – Jeff Schaller
    10 hours ago











  • @JeffSchaller Something accessible via the bash commandline. I was a bit unclear, sorry

    – Enno
    10 hours ago
















9















This question is strongly related to this and this question. I have a file that contains several lines where each line is a path to a file. Now I want to pair each line with each different line (not itself). Also a pair A B is equal to a B A pair for my purposes, so only one of these combinations should be produced.



Example



files.dat reads like this in a shorthand notation, each letter is a file path (absolute or relative)



a
b
c
d
e


Then my result should look something like this:



a b
a c
a d
a e
b c
b d
b e
c d
c e
d e


Preferrably I would like to solve this in bash. Unlike the other questions, my file list is rather small (about 200 lines), so using loops and RAM capacity
pose no problems.










share|improve this question









New contributor




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




















  • Does it have to be in bash proper, or just something available via the bash commandline? Other utilities are better positioned to process text.

    – Jeff Schaller
    10 hours ago











  • @JeffSchaller Something accessible via the bash commandline. I was a bit unclear, sorry

    – Enno
    10 hours ago














9












9








9








This question is strongly related to this and this question. I have a file that contains several lines where each line is a path to a file. Now I want to pair each line with each different line (not itself). Also a pair A B is equal to a B A pair for my purposes, so only one of these combinations should be produced.



Example



files.dat reads like this in a shorthand notation, each letter is a file path (absolute or relative)



a
b
c
d
e


Then my result should look something like this:



a b
a c
a d
a e
b c
b d
b e
c d
c e
d e


Preferrably I would like to solve this in bash. Unlike the other questions, my file list is rather small (about 200 lines), so using loops and RAM capacity
pose no problems.










share|improve this question









New contributor




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












This question is strongly related to this and this question. I have a file that contains several lines where each line is a path to a file. Now I want to pair each line with each different line (not itself). Also a pair A B is equal to a B A pair for my purposes, so only one of these combinations should be produced.



Example



files.dat reads like this in a shorthand notation, each letter is a file path (absolute or relative)



a
b
c
d
e


Then my result should look something like this:



a b
a c
a d
a e
b c
b d
b e
c d
c e
d e


Preferrably I would like to solve this in bash. Unlike the other questions, my file list is rather small (about 200 lines), so using loops and RAM capacity
pose no problems.







shell-script text-processing






share|improve this question









New contributor




Enno 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




Enno 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 10 hours ago









Jeff Schaller

43.4k1160140




43.4k1160140






New contributor




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









asked 10 hours ago









EnnoEnno

1483




1483




New contributor




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





New contributor





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






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












  • Does it have to be in bash proper, or just something available via the bash commandline? Other utilities are better positioned to process text.

    – Jeff Schaller
    10 hours ago











  • @JeffSchaller Something accessible via the bash commandline. I was a bit unclear, sorry

    – Enno
    10 hours ago


















  • Does it have to be in bash proper, or just something available via the bash commandline? Other utilities are better positioned to process text.

    – Jeff Schaller
    10 hours ago











  • @JeffSchaller Something accessible via the bash commandline. I was a bit unclear, sorry

    – Enno
    10 hours ago

















Does it have to be in bash proper, or just something available via the bash commandline? Other utilities are better positioned to process text.

– Jeff Schaller
10 hours ago





Does it have to be in bash proper, or just something available via the bash commandline? Other utilities are better positioned to process text.

– Jeff Schaller
10 hours ago













@JeffSchaller Something accessible via the bash commandline. I was a bit unclear, sorry

– Enno
10 hours ago






@JeffSchaller Something accessible via the bash commandline. I was a bit unclear, sorry

– Enno
10 hours ago











5 Answers
5






active

oldest

votes


















6














Use this command:



awk ' name[$1]++ 
END PROCINFO["sorted_in"] = "@ind_str_asc"
for (v1 in name) for (v2 in name) if (v1 < v2) print v1, v2
' files.dat


PROCINFO may be a gawk extension. 
If your awk doesn’t support it,
just leave out the PROCINFO["sorted_in"] = "@ind_str_asc" line
and pipe the output into sort (if you want the output sorted).



(This does not require the input to be sorted.)






share|improve this answer






























    5














    $ join -j 2 -o 1.1,2.1 file file | awk '!seen[$1,$2]++ && !seen[$2,$1]++'
    a b
    a c
    a d
    a e
    b c
    b d
    b e
    c d
    c e
    d e


    This assumes that no line in the input file contains any whitespace. It also assumes that the file is sorted.



    The join command creates the full cross product of the lines in the file. It does this by joining the file with itself on a non-existing field. The non-standard -j 2 may be replaced by -1 2 -2 2 (but not by -j2 unless you use GNU join).



    The awk command reads the result of this and only outputs results that are pairs that has not yet been seen.






    share|improve this answer

























    • What do you mean by "the file is sorted"? Sorted by which criteria?

      – Enno
      9 hours ago











    • @Enno Sorted the way sort -b would sort it. join require sorted input files.

      – Kusalananda
      9 hours ago



















    5














    If you have ruby installed:



    $ ruby -0777 -F'n' -lane '$F.combination(2) ' ip.txt
    a b
    a c
    a d
    a e
    b c
    b d
    b e
    c d
    c e
    d e



    • -0777 slurp entire file (should be okay as it is mentioned in OP that file size is small)


    • -F'n' split based on newline, so each line will be an element in $F array


    • $F.combination(2) generate combinations 2 elements at a time


    • print as required

    • if input file can contain duplicates, use $F.uniq.combination(2)



    for 3 elements at a time:



    $ ruby -0777 -F'n' -lane '$F.combination(3) ' ip.txt
    a b c
    a b d
    a b e
    a c d
    a c e
    a d e
    b c d
    b c e
    b d e
    c d e





    With perl (not generic)



    $ perl -0777 -F'n' -lane 'for $i (0..$#F) 
    for $j ($i+1..$#F)
    print "$F[$i] $F[$j]n" ' ip.txt
    a b
    a c
    a d
    a e
    b c
    b d
    b e
    c d
    c e
    d e




    With awk



    $ awk ' a[NR]=$0 
    END for(i=1;i<=NR;i++)
    for(j=i+1;j<=NR;j++)
    print a[i], a[j] ' ip.txt
    a b
    a c
    a d
    a e
    b c
    b d
    b e
    c d
    c e
    d e





    share|improve this answer
































      5














      A python solution.
      The input file is fed to itertools.combinations from the standard library, which generates 2-length tuples that are formatted and printed to standard output.



      python3 -c 'from itertools import combinations
      with open("file") as f:
      lines = (line.rstrip() for line in f)
      lines = (" ".format(x, y) for x, y in combinations(lines, 2))
      print(*lines, sep="n")
      '





      share|improve this answer






























        1














        Here's one in pure shell.



        a=$1
        shift
        for f in "$@"
        do
        echo $a $f
        done
        test $# -gt 1 && /bin/sh $0 "$@"


        Example:



        ~ (137) $ sh test.sh $(cat file.dat)
        a b
        a c
        a d
        a e
        b c
        b d
        b e
        c d
        c e
        d e
        ~ (138) $





        share|improve this answer










        New contributor




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




















        • Command substitution strips trailing newlines, so you're better off with something like <file.dat xargs test.sh than test.sh $(cat file.dat)

          – iruvar
          3 hours ago










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



        );






        Enno 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%2f506815%2fbash-pair-each-line-of-file%23new-answer', 'question_page');

        );

        Post as a guest















        Required, but never shown

























        5 Answers
        5






        active

        oldest

        votes








        5 Answers
        5






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        6














        Use this command:



        awk ' name[$1]++ 
        END PROCINFO["sorted_in"] = "@ind_str_asc"
        for (v1 in name) for (v2 in name) if (v1 < v2) print v1, v2
        ' files.dat


        PROCINFO may be a gawk extension. 
        If your awk doesn’t support it,
        just leave out the PROCINFO["sorted_in"] = "@ind_str_asc" line
        and pipe the output into sort (if you want the output sorted).



        (This does not require the input to be sorted.)






        share|improve this answer



























          6














          Use this command:



          awk ' name[$1]++ 
          END PROCINFO["sorted_in"] = "@ind_str_asc"
          for (v1 in name) for (v2 in name) if (v1 < v2) print v1, v2
          ' files.dat


          PROCINFO may be a gawk extension. 
          If your awk doesn’t support it,
          just leave out the PROCINFO["sorted_in"] = "@ind_str_asc" line
          and pipe the output into sort (if you want the output sorted).



          (This does not require the input to be sorted.)






          share|improve this answer

























            6












            6








            6







            Use this command:



            awk ' name[$1]++ 
            END PROCINFO["sorted_in"] = "@ind_str_asc"
            for (v1 in name) for (v2 in name) if (v1 < v2) print v1, v2
            ' files.dat


            PROCINFO may be a gawk extension. 
            If your awk doesn’t support it,
            just leave out the PROCINFO["sorted_in"] = "@ind_str_asc" line
            and pipe the output into sort (if you want the output sorted).



            (This does not require the input to be sorted.)






            share|improve this answer













            Use this command:



            awk ' name[$1]++ 
            END PROCINFO["sorted_in"] = "@ind_str_asc"
            for (v1 in name) for (v2 in name) if (v1 < v2) print v1, v2
            ' files.dat


            PROCINFO may be a gawk extension. 
            If your awk doesn’t support it,
            just leave out the PROCINFO["sorted_in"] = "@ind_str_asc" line
            and pipe the output into sort (if you want the output sorted).



            (This does not require the input to be sorted.)







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered 9 hours ago









            G-ManG-Man

            13.5k93668




            13.5k93668























                5














                $ join -j 2 -o 1.1,2.1 file file | awk '!seen[$1,$2]++ && !seen[$2,$1]++'
                a b
                a c
                a d
                a e
                b c
                b d
                b e
                c d
                c e
                d e


                This assumes that no line in the input file contains any whitespace. It also assumes that the file is sorted.



                The join command creates the full cross product of the lines in the file. It does this by joining the file with itself on a non-existing field. The non-standard -j 2 may be replaced by -1 2 -2 2 (but not by -j2 unless you use GNU join).



                The awk command reads the result of this and only outputs results that are pairs that has not yet been seen.






                share|improve this answer

























                • What do you mean by "the file is sorted"? Sorted by which criteria?

                  – Enno
                  9 hours ago











                • @Enno Sorted the way sort -b would sort it. join require sorted input files.

                  – Kusalananda
                  9 hours ago
















                5














                $ join -j 2 -o 1.1,2.1 file file | awk '!seen[$1,$2]++ && !seen[$2,$1]++'
                a b
                a c
                a d
                a e
                b c
                b d
                b e
                c d
                c e
                d e


                This assumes that no line in the input file contains any whitespace. It also assumes that the file is sorted.



                The join command creates the full cross product of the lines in the file. It does this by joining the file with itself on a non-existing field. The non-standard -j 2 may be replaced by -1 2 -2 2 (but not by -j2 unless you use GNU join).



                The awk command reads the result of this and only outputs results that are pairs that has not yet been seen.






                share|improve this answer

























                • What do you mean by "the file is sorted"? Sorted by which criteria?

                  – Enno
                  9 hours ago











                • @Enno Sorted the way sort -b would sort it. join require sorted input files.

                  – Kusalananda
                  9 hours ago














                5












                5








                5







                $ join -j 2 -o 1.1,2.1 file file | awk '!seen[$1,$2]++ && !seen[$2,$1]++'
                a b
                a c
                a d
                a e
                b c
                b d
                b e
                c d
                c e
                d e


                This assumes that no line in the input file contains any whitespace. It also assumes that the file is sorted.



                The join command creates the full cross product of the lines in the file. It does this by joining the file with itself on a non-existing field. The non-standard -j 2 may be replaced by -1 2 -2 2 (but not by -j2 unless you use GNU join).



                The awk command reads the result of this and only outputs results that are pairs that has not yet been seen.






                share|improve this answer















                $ join -j 2 -o 1.1,2.1 file file | awk '!seen[$1,$2]++ && !seen[$2,$1]++'
                a b
                a c
                a d
                a e
                b c
                b d
                b e
                c d
                c e
                d e


                This assumes that no line in the input file contains any whitespace. It also assumes that the file is sorted.



                The join command creates the full cross product of the lines in the file. It does this by joining the file with itself on a non-existing field. The non-standard -j 2 may be replaced by -1 2 -2 2 (but not by -j2 unless you use GNU join).



                The awk command reads the result of this and only outputs results that are pairs that has not yet been seen.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited 9 hours ago

























                answered 9 hours ago









                KusalanandaKusalananda

                136k17256424




                136k17256424












                • What do you mean by "the file is sorted"? Sorted by which criteria?

                  – Enno
                  9 hours ago











                • @Enno Sorted the way sort -b would sort it. join require sorted input files.

                  – Kusalananda
                  9 hours ago


















                • What do you mean by "the file is sorted"? Sorted by which criteria?

                  – Enno
                  9 hours ago











                • @Enno Sorted the way sort -b would sort it. join require sorted input files.

                  – Kusalananda
                  9 hours ago

















                What do you mean by "the file is sorted"? Sorted by which criteria?

                – Enno
                9 hours ago





                What do you mean by "the file is sorted"? Sorted by which criteria?

                – Enno
                9 hours ago













                @Enno Sorted the way sort -b would sort it. join require sorted input files.

                – Kusalananda
                9 hours ago






                @Enno Sorted the way sort -b would sort it. join require sorted input files.

                – Kusalananda
                9 hours ago












                5














                If you have ruby installed:



                $ ruby -0777 -F'n' -lane '$F.combination(2) ' ip.txt
                a b
                a c
                a d
                a e
                b c
                b d
                b e
                c d
                c e
                d e



                • -0777 slurp entire file (should be okay as it is mentioned in OP that file size is small)


                • -F'n' split based on newline, so each line will be an element in $F array


                • $F.combination(2) generate combinations 2 elements at a time


                • print as required

                • if input file can contain duplicates, use $F.uniq.combination(2)



                for 3 elements at a time:



                $ ruby -0777 -F'n' -lane '$F.combination(3) ' ip.txt
                a b c
                a b d
                a b e
                a c d
                a c e
                a d e
                b c d
                b c e
                b d e
                c d e





                With perl (not generic)



                $ perl -0777 -F'n' -lane 'for $i (0..$#F) 
                for $j ($i+1..$#F)
                print "$F[$i] $F[$j]n" ' ip.txt
                a b
                a c
                a d
                a e
                b c
                b d
                b e
                c d
                c e
                d e




                With awk



                $ awk ' a[NR]=$0 
                END for(i=1;i<=NR;i++)
                for(j=i+1;j<=NR;j++)
                print a[i], a[j] ' ip.txt
                a b
                a c
                a d
                a e
                b c
                b d
                b e
                c d
                c e
                d e





                share|improve this answer





























                  5














                  If you have ruby installed:



                  $ ruby -0777 -F'n' -lane '$F.combination(2) ' ip.txt
                  a b
                  a c
                  a d
                  a e
                  b c
                  b d
                  b e
                  c d
                  c e
                  d e



                  • -0777 slurp entire file (should be okay as it is mentioned in OP that file size is small)


                  • -F'n' split based on newline, so each line will be an element in $F array


                  • $F.combination(2) generate combinations 2 elements at a time


                  • print as required

                  • if input file can contain duplicates, use $F.uniq.combination(2)



                  for 3 elements at a time:



                  $ ruby -0777 -F'n' -lane '$F.combination(3) ' ip.txt
                  a b c
                  a b d
                  a b e
                  a c d
                  a c e
                  a d e
                  b c d
                  b c e
                  b d e
                  c d e





                  With perl (not generic)



                  $ perl -0777 -F'n' -lane 'for $i (0..$#F) 
                  for $j ($i+1..$#F)
                  print "$F[$i] $F[$j]n" ' ip.txt
                  a b
                  a c
                  a d
                  a e
                  b c
                  b d
                  b e
                  c d
                  c e
                  d e




                  With awk



                  $ awk ' a[NR]=$0 
                  END for(i=1;i<=NR;i++)
                  for(j=i+1;j<=NR;j++)
                  print a[i], a[j] ' ip.txt
                  a b
                  a c
                  a d
                  a e
                  b c
                  b d
                  b e
                  c d
                  c e
                  d e





                  share|improve this answer



























                    5












                    5








                    5







                    If you have ruby installed:



                    $ ruby -0777 -F'n' -lane '$F.combination(2) ' ip.txt
                    a b
                    a c
                    a d
                    a e
                    b c
                    b d
                    b e
                    c d
                    c e
                    d e



                    • -0777 slurp entire file (should be okay as it is mentioned in OP that file size is small)


                    • -F'n' split based on newline, so each line will be an element in $F array


                    • $F.combination(2) generate combinations 2 elements at a time


                    • print as required

                    • if input file can contain duplicates, use $F.uniq.combination(2)



                    for 3 elements at a time:



                    $ ruby -0777 -F'n' -lane '$F.combination(3) ' ip.txt
                    a b c
                    a b d
                    a b e
                    a c d
                    a c e
                    a d e
                    b c d
                    b c e
                    b d e
                    c d e





                    With perl (not generic)



                    $ perl -0777 -F'n' -lane 'for $i (0..$#F) 
                    for $j ($i+1..$#F)
                    print "$F[$i] $F[$j]n" ' ip.txt
                    a b
                    a c
                    a d
                    a e
                    b c
                    b d
                    b e
                    c d
                    c e
                    d e




                    With awk



                    $ awk ' a[NR]=$0 
                    END for(i=1;i<=NR;i++)
                    for(j=i+1;j<=NR;j++)
                    print a[i], a[j] ' ip.txt
                    a b
                    a c
                    a d
                    a e
                    b c
                    b d
                    b e
                    c d
                    c e
                    d e





                    share|improve this answer















                    If you have ruby installed:



                    $ ruby -0777 -F'n' -lane '$F.combination(2) ' ip.txt
                    a b
                    a c
                    a d
                    a e
                    b c
                    b d
                    b e
                    c d
                    c e
                    d e



                    • -0777 slurp entire file (should be okay as it is mentioned in OP that file size is small)


                    • -F'n' split based on newline, so each line will be an element in $F array


                    • $F.combination(2) generate combinations 2 elements at a time


                    • print as required

                    • if input file can contain duplicates, use $F.uniq.combination(2)



                    for 3 elements at a time:



                    $ ruby -0777 -F'n' -lane '$F.combination(3) ' ip.txt
                    a b c
                    a b d
                    a b e
                    a c d
                    a c e
                    a d e
                    b c d
                    b c e
                    b d e
                    c d e





                    With perl (not generic)



                    $ perl -0777 -F'n' -lane 'for $i (0..$#F) 
                    for $j ($i+1..$#F)
                    print "$F[$i] $F[$j]n" ' ip.txt
                    a b
                    a c
                    a d
                    a e
                    b c
                    b d
                    b e
                    c d
                    c e
                    d e




                    With awk



                    $ awk ' a[NR]=$0 
                    END for(i=1;i<=NR;i++)
                    for(j=i+1;j<=NR;j++)
                    print a[i], a[j] ' ip.txt
                    a b
                    a c
                    a d
                    a e
                    b c
                    b d
                    b e
                    c d
                    c e
                    d e






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited 9 hours ago

























                    answered 9 hours ago









                    SundeepSundeep

                    7,4711927




                    7,4711927





















                        5














                        A python solution.
                        The input file is fed to itertools.combinations from the standard library, which generates 2-length tuples that are formatted and printed to standard output.



                        python3 -c 'from itertools import combinations
                        with open("file") as f:
                        lines = (line.rstrip() for line in f)
                        lines = (" ".format(x, y) for x, y in combinations(lines, 2))
                        print(*lines, sep="n")
                        '





                        share|improve this answer



























                          5














                          A python solution.
                          The input file is fed to itertools.combinations from the standard library, which generates 2-length tuples that are formatted and printed to standard output.



                          python3 -c 'from itertools import combinations
                          with open("file") as f:
                          lines = (line.rstrip() for line in f)
                          lines = (" ".format(x, y) for x, y in combinations(lines, 2))
                          print(*lines, sep="n")
                          '





                          share|improve this answer

























                            5












                            5








                            5







                            A python solution.
                            The input file is fed to itertools.combinations from the standard library, which generates 2-length tuples that are formatted and printed to standard output.



                            python3 -c 'from itertools import combinations
                            with open("file") as f:
                            lines = (line.rstrip() for line in f)
                            lines = (" ".format(x, y) for x, y in combinations(lines, 2))
                            print(*lines, sep="n")
                            '





                            share|improve this answer













                            A python solution.
                            The input file is fed to itertools.combinations from the standard library, which generates 2-length tuples that are formatted and printed to standard output.



                            python3 -c 'from itertools import combinations
                            with open("file") as f:
                            lines = (line.rstrip() for line in f)
                            lines = (" ".format(x, y) for x, y in combinations(lines, 2))
                            print(*lines, sep="n")
                            '






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered 8 hours ago









                            iruvariruvar

                            12.1k62960




                            12.1k62960





















                                1














                                Here's one in pure shell.



                                a=$1
                                shift
                                for f in "$@"
                                do
                                echo $a $f
                                done
                                test $# -gt 1 && /bin/sh $0 "$@"


                                Example:



                                ~ (137) $ sh test.sh $(cat file.dat)
                                a b
                                a c
                                a d
                                a e
                                b c
                                b d
                                b e
                                c d
                                c e
                                d e
                                ~ (138) $





                                share|improve this answer










                                New contributor




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




















                                • Command substitution strips trailing newlines, so you're better off with something like <file.dat xargs test.sh than test.sh $(cat file.dat)

                                  – iruvar
                                  3 hours ago















                                1














                                Here's one in pure shell.



                                a=$1
                                shift
                                for f in "$@"
                                do
                                echo $a $f
                                done
                                test $# -gt 1 && /bin/sh $0 "$@"


                                Example:



                                ~ (137) $ sh test.sh $(cat file.dat)
                                a b
                                a c
                                a d
                                a e
                                b c
                                b d
                                b e
                                c d
                                c e
                                d e
                                ~ (138) $





                                share|improve this answer










                                New contributor




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




















                                • Command substitution strips trailing newlines, so you're better off with something like <file.dat xargs test.sh than test.sh $(cat file.dat)

                                  – iruvar
                                  3 hours ago













                                1












                                1








                                1







                                Here's one in pure shell.



                                a=$1
                                shift
                                for f in "$@"
                                do
                                echo $a $f
                                done
                                test $# -gt 1 && /bin/sh $0 "$@"


                                Example:



                                ~ (137) $ sh test.sh $(cat file.dat)
                                a b
                                a c
                                a d
                                a e
                                b c
                                b d
                                b e
                                c d
                                c e
                                d e
                                ~ (138) $





                                share|improve this answer










                                New contributor




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










                                Here's one in pure shell.



                                a=$1
                                shift
                                for f in "$@"
                                do
                                echo $a $f
                                done
                                test $# -gt 1 && /bin/sh $0 "$@"


                                Example:



                                ~ (137) $ sh test.sh $(cat file.dat)
                                a b
                                a c
                                a d
                                a e
                                b c
                                b d
                                b e
                                c d
                                c e
                                d e
                                ~ (138) $






                                share|improve this answer










                                New contributor




                                EdC 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 answer



                                share|improve this answer








                                edited 5 hours ago





















                                New contributor




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









                                answered 6 hours ago









                                EdCEdC

                                112




                                112




                                New contributor




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





                                New contributor





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






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












                                • Command substitution strips trailing newlines, so you're better off with something like <file.dat xargs test.sh than test.sh $(cat file.dat)

                                  – iruvar
                                  3 hours ago

















                                • Command substitution strips trailing newlines, so you're better off with something like <file.dat xargs test.sh than test.sh $(cat file.dat)

                                  – iruvar
                                  3 hours ago
















                                Command substitution strips trailing newlines, so you're better off with something like <file.dat xargs test.sh than test.sh $(cat file.dat)

                                – iruvar
                                3 hours ago





                                Command substitution strips trailing newlines, so you're better off with something like <file.dat xargs test.sh than test.sh $(cat file.dat)

                                – iruvar
                                3 hours ago










                                Enno is a new contributor. Be nice, and check out our Code of Conduct.









                                draft saved

                                draft discarded


















                                Enno is a new contributor. Be nice, and check out our Code of Conduct.












                                Enno is a new contributor. Be nice, and check out our Code of Conduct.











                                Enno 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%2f506815%2fbash-pair-each-line-of-file%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

                                Станькаў Зьмест Назва | Гісторыя | Насельніцтва | Турыстычная інфармацыя | Галерэя | Асобы | Заўвагі | Крыніцы | Літаратура | Вонкавыя спасылкі | Навігацыйнае мэню53°37′48″ пн. ш. 27°13′40″ у. д. / 53.63° пн. ш. 27.22778° у. д. / 53.63; 27.2277853°37′48″ пн. ш. 27°13′40″ у. д. / 53.63° пн. ш. 27.22778° у. д. / 53.63; 27.22778djvu"Бабёнка – это я понимаю!". Як русіфікавалі тапонімы.Святыя руіны ажываюць210Станькаўв. СтанькаваСтаньково: история и новости - сайт деревниСтанькава: сядзіба Гутэн-Чапскіхррр

                                J. J. Abrams Índice Traxectoria | Filmografía | Premios | Notas | Véxase tamén | Menú de navegacióne"J.J. Abrams: Biography"Arquivado"'Star Trek' sequel on track"Arquivado"J.J. Abrams Producing Samurai Jack Movie"Arquivado"EXCLUSIVE: J.J. Abrams Goes Into Warp Speed with Star Trek and Beyond"Arquivado"David Semel To Direct Jonah Nolan/J.J. Abrams' CBS Pilot 'Person Of Interest'"Arquivado"Fox orders J.J. Abrams pilot 'Alcatraz'"ArquivadoJ. J. AbramsJ. J. AbramsWorldCat81800131p24091041000XX116709414031616ma11226833654496ID052246713376222X511412nm00091900000 0001 1772 5428no98124254ID0000002883100650044xx0054597000141374297344064w64f2mjx14255303415344

                                Першая сусьветная вайна Зьмест Чыньнікі | Асноўныя падзеі | Беларусь у гады вайны | Балянс вайны | Глядзіце таксама | Крыніцы | Вонкавыя спасылкі | Навігацыйнае мэнюПершая сусьветная вайна: памяць павінна жыць124 (27489)41990-763xЗападный мир отмечает 90 лет со дня окончания Первой мировой войныПершая сусьветная вайнаСпадчына Першай сусьветнай вайныГісторыя вайныАрхіў дакумэнтаў вайныМапы Эўропы ў ходзе вайныГісторыя пад знакам Пагоні/Першая сусветная вайна і Беларусь