Я не думаю, что есть команда unix, которую вы могли бы просто использовать для этой задачи. Но вы можете создать небольшой сценарий оболочки для команд comm
и grep
, как показано в следующем примере:
#!/bin/bash
# Prepare 200 (small) test files
rm data-*.txt
for i in {1..200} ; do
echo "${i}" >> "data-${i}.txt"
# common line
echo "foo common line" >> "data-${i}.txt"
done
# Get the common lines between file1 and file2.
# file1 and file2 can be random files out of the set,
# ideally they are the smallest ones
comm -12 data-1.txt data-2.txt > common_lines
# Now grep through the remaining files for those lines
for file in data-{3..100}.txt ; do
# For each remaining file reduce the common_lines to those
# which are found in that file
grep -Fxf common_lines "${file}" > tmp_common_lines \
&& mv tmp_common_lines > common_lines
done
# Print the common lines
cat common_lines
Тот же подход можно использовать для больших файлов. Это займет больше времени, но потребление памяти останется линейным.