I have a number of single-column text files containing unsorted values. The aim is to join them, however, the "join" utility of linux requires the files to be sorted. Any idea how to do that without sorting?

A.txt

0000;
0001;
0002;
0003;

B.txt

0000;
0011;
0012;
0003;

C.txt

0000;
0024;
0003;
0025;

Desired Output:

0000;
0003;
有帮助吗?

解决方案

To overcome the "number of files beforehand" and the "repetitive elements" problems of twalberg's fine awk program, I'd use the more verbose:

#!/usr/bin/python2

from sys import argv

# collect all lines from each file in their own set

sets = []
for path in argv[1:]:
    with open(path) as infile:
        s = set(infile.readlines())
        sets.append(s)

# find the common items in all sets

common = sets[0]
for s in sets[1:]:
    common = common.intersection(s)

# print the common items in the order they appear in the
# first file

with open(argv[1]) as infile:
    for line in infile:
        if line in common:
            common.remove(line) # prevents duplicates
            print line,

其他提示

This, I believe, requires GNU awk for the multidimensional array:

gawk '
    FNR == 1 {nfiles++}
    {seen[$1][FILENAME] = 1} 
    END {for (item in seen) if (length(seen[item]) == nfiles) print item}
' A.txt B.txt C.txt
0000;
0003;

TXR Lisp solution:

(defvar hash-list
  (collect-each ((a *args*))
    (hash-construct '(:equal-based) (zip (get-lines (open-file a))))))

(if hash-list
  (dohash (key val [reduce-left hash-isec hash-list])
    (put-line key)))

$ txr join.tl
$ txr join.tl A.txt
0000;
0001;
0002;
0003;
$ txr join.tl A.txt B.txt C.txt
0000;
0003;
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top