Найти разницу между двумя файлами и распечатать его в правильном формате

Решил эту проблему, переместив соответствующий сервер в выделенную VLAN и затем зарегистрировав трафик между ним и остальной частью сеть с помощью определенных правил iptable, запускаемых на некоторых портах.

Когда я закончу проект, у меня будет полная запись на моем сайте, и тогда я обновлю этот ответ.

1
14.12.2018, 21:42
3 ответа

Другой простой вариант — использовать comm; ему просто нужен отсортированный ввод, поэтому дайте ему чистый ввод, отфильтровав «действительные номера счетов» (вся строка состоит только из 9 цифр ), затем передайте канал для сортировки перед перенаправлением в новый файл:

grep -Ex '[[:digit:]]{9}' account.txt   | sort > account.txt.sorted
grep -Ex '[[:digit:]]{9}' customer.txt  | sort > customer.txt.sorted

... затем используйте comm, как вы указали:

{ echo 'Missing Account Number:'; comm -23 account.txt.sorted customer.txt.sorted; }

{ echo 'Extra Customer Number:'; comm -13 account.txt.sorted customer.txt.sorted; }

Данные выборки входных данных:

account.txt

garbage
876251251
716126181
888281211
666615211
666615211extra
787878787
111212134
extra

клиент.txt

garbage
876251251
876251251extra
716126181
792342108
792332668
666615211
760332429
791952441
676702288
junk

Результирующий вывод::

Missing Account Number:
111212134
787878787
888281211

Extra Customer Number:
676702288
760332429
791952441
792332668
792342108
2
27.01.2020, 23:23

Да, возможно, проще всего с помощью diff.

$ diff account.txt customer.txt
1c1
< **account.txt**
---
> **customer.txt**
5c5,6
< 888281211
---
> 792342108
> 792332668
7,8c8,10
< 787878787
< 111212134
---
> 760332429
> 791952441
> 676702288

$ diff account.txt customer.txt|grep '^<'
< **account.txt**
< 888281211
< 787878787
< 111212134

$ diff account.txt customer.txt|grep '^>'
> **customer.txt**
> 792342108
> 792332668
> 760332429
> 791952441
> 676702288

Следующий шелл-скрипт diff-scriptболее совершенен.

#!/bin/bash

# assuming 9-digit account and customer numbers

sort account.txt  | uniq > account.srt
sort customer.txt | uniq > customer.srt

diff account.srt customer.srt > diff.txt

echo 'only in account.srt:' > result.txt
< diff.txt grep -E '^< [0-9]{9}$' | sed s'/^< //' >> result.txt

echo 'only in customer.srt:' >> result.txt
< diff.txt grep -E '^> [0-9]{9}$' | sed s'/^> //' >> result.txt

echo "The result is in the file 'result.txt'"
echo "You can read it with 'less result.txt'"

Демонстрационный пример,

$./diff-script
The result is in the file 'result.txt'
You can read it with 'less result.txt'

$ cat result.txt 
only in account.srt:
111212134
787878787
888281211
only in customer.srt:
676702288
760332429
791952441
792332668
792342108
1
27.01.2020, 23:23

Для этой работы я бы выбрал awk. Код ниже работает только для действительных данных 9 чисел в строке. Пустые строки, строки с числами больше или меньше 9 и строки с буквами игнорируются.

$ cat account
876251251

716126181
888281211
asdferfggggg
666615211
787878787
123456789123
111212134

$ cat customer
876251251
716126181
eeeeeeeee
792342108
792332668
666615211
760332429

791952441
676702288

$ awk '/^[0-9]{9}$/{a[$0]++;b[$0]="found only in " FILENAME}END{for (i in a) if (a[i]==1) print i,b[i]}' account customer |sort -k2
111212134 found only in account
787878787 found only in account
888281211 found only in account
676702288 found only in customer
760332429 found only in customer
791952441 found only in customer
792332668 found only in customer
792342108 found only in customer
0
27.01.2020, 23:23

Теги

Похожие вопросы