Bash - pair each line of file

Enno

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.

G-Man Says 'Reinstate Monica'

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

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Write first 10 characters of each line to new file using bash

분류에서Dev

Bash: Path or link to a line in a file?

분류에서Dev

Frequency of specific symbols on each line of a text file

분류에서Dev

Grep last N characters of each line in a file

분류에서Dev

Number of same line in each text file

분류에서Dev

Add and number blank line above each line in a file

분류에서Dev

Remove a certain line from Bash history file

분류에서Dev

sed bash variable to delete a line in file

분류에서Dev

How to Read string from file and compare to each line of second file

분류에서Dev

file.each_line can only be called once

분류에서Dev

Command Line, File Name for each script executed. SQL

분류에서Dev

Sort words on each line, editing the file in-place

분류에서Dev

spread a file into multiple .csv files per each line of inputfile read

분류에서Dev

How to view the `.bash_history` file via command line?

분류에서Dev

Bash: How to store a specific line of CLI output into a file?

분류에서Dev

bash ignorecase match pattern in file and color that line, print all to screen

분류에서Dev

xslt - for each - line-by-line

분류에서Dev

Bash with cat for automatic merging pair of files

분류에서Dev

add / at the end of each line

분류에서Dev

Line by line text editing with Bash

분류에서Dev

How would I read the contents of a text file and run a command for each line?

분류에서Dev

What command(s) will feed a tab-delimited text file and cut each line to 80 characters?

분류에서Dev

Java how to read a txt file and make each line it's own string array

분류에서Dev

How to do data analysis using Python, of a file with thousands of dictionaries in each line

분류에서Dev

What is an efficient BASH one-liner for adding line every 130 characters to a file without lines?

분류에서Dev

Find a string in a file and then find the first line above containing another string with bash

분류에서Dev

How can I pass parameter in Talend exported job, through command line or bash script file?

분류에서Dev

remove first line in bash

분류에서Dev

concatenation to previous line in bash

Related 관련 기사

  1. 1

    Write first 10 characters of each line to new file using bash

  2. 2

    Bash: Path or link to a line in a file?

  3. 3

    Frequency of specific symbols on each line of a text file

  4. 4

    Grep last N characters of each line in a file

  5. 5

    Number of same line in each text file

  6. 6

    Add and number blank line above each line in a file

  7. 7

    Remove a certain line from Bash history file

  8. 8

    sed bash variable to delete a line in file

  9. 9

    How to Read string from file and compare to each line of second file

  10. 10

    file.each_line can only be called once

  11. 11

    Command Line, File Name for each script executed. SQL

  12. 12

    Sort words on each line, editing the file in-place

  13. 13

    spread a file into multiple .csv files per each line of inputfile read

  14. 14

    How to view the `.bash_history` file via command line?

  15. 15

    Bash: How to store a specific line of CLI output into a file?

  16. 16

    bash ignorecase match pattern in file and color that line, print all to screen

  17. 17

    xslt - for each - line-by-line

  18. 18

    Bash with cat for automatic merging pair of files

  19. 19

    add / at the end of each line

  20. 20

    Line by line text editing with Bash

  21. 21

    How would I read the contents of a text file and run a command for each line?

  22. 22

    What command(s) will feed a tab-delimited text file and cut each line to 80 characters?

  23. 23

    Java how to read a txt file and make each line it's own string array

  24. 24

    How to do data analysis using Python, of a file with thousands of dictionaries in each line

  25. 25

    What is an efficient BASH one-liner for adding line every 130 characters to a file without lines?

  26. 26

    Find a string in a file and then find the first line above containing another string with bash

  27. 27

    How can I pass parameter in Talend exported job, through command line or bash script file?

  28. 28

    remove first line in bash

  29. 29

    concatenation to previous line in bash

뜨겁다태그

보관