Как сравнить два файла построчно и вывести всю строку, если они разные - PullRequest
0 голосов
/ 16 мая 2018

У меня есть два отсортированных файла в вопросе

1)one is a control file(ctrl.txt) which is external process generated
2)and other is line count file(count.txt) that I generate using `wc -l`

$ more ctrl.txt

Thunderbird|1000
Mustang|2000
Hurricane|3000

$ more count.txt

Thunder_bird|1000
MUSTANG|2000
Hurricane|3001

Я хочуСравните эти два файла, игнорируя складки в столбце 1 (имена файлов), такие как "_" (для Thunder_bird) или "верхний регистр" (для MUSTANG), так что мой вывод показывает только файл ниже какединственный реально другой файл, для которого значения не совпадают.

Hurricane|3000

У меня есть идея сравнить только второй столбец из обоих файлов и вывести целую строку, если они отличаются

Я видел другие примеры в AWK, но я не мог заставить что-либо работать.

1 Ответ

0 голосов
/ 16 мая 2018

Не могли бы вы попробовать awk и сообщить мне, поможет ли это вам.

awk -F"|" 'FNR==NR{gsub(/_/,"");a[tolower($1)]=$2;next} {gsub(/_/,"")} ((tolower($1) in a) && $2!=a[tolower($1)])' cntrl.txt count.txt

Теперь добавляем не-лайнерную форму решения.

awk -F"|" '
FNR==NR{
  gsub(/_/,"");
  a[tolower($1)]=$2;
  next}
{ gsub(/_/,"") }
((tolower($1) in a) && $2!=a[tolower($1)])
' cntrl.txt count.txt

Объяснение: Здесь также добавлено объяснение для приведенного выше кода.

awk -F"|" '                                ##Setting field seprator as |(pipe) here for all lines in Input_file(s).
FNR==NR{                                   ##Checking condition FNR==NR which will be TRUE when first Input_file(cntrl.txt) in this case is being read. Following instructions will be executed once this condition is TRUE.
  gsub(/_/,"");                            ##Using gsub utility of awk to globally subtitute _ with NULL in current line.
  a[tolower($1)]=$2;                       ##Creating an array named a whose index is first field in LOWER CASE to avoid confusions and value is $2 of current line.
  next}                                    ##next is awk out of the box keyword which will skip all further instructions now.(to make sure they are read when 2nd Input-file named count.txt is being read).
{ gsub(/_/,"") }                           ##Statements from here will be executed when 2nd Input_file is being read, using gsub to remove _ all occurrences from line.
((tolower($1) in a) && $2!=a[tolower($1)]) ##Checking condition here if lower form of $1 is present in array a and value of current line $2 is NOT equal to array a value. If this condition is TRUE then print the current line, since I have NOT given any action so by default printing of current line will happen from count.txt file.
' cntrl.txt count.txt                      ##Mentioning the Input_file names here which we have to pass to awk.
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...