Вы можете столкнуться с проблемами хранения больших файлов в памяти, это немного лучше, так как он только хранит соответствующие строки, после того, как рода проделал тяжелую съемку положения линий в порядке.
# Input must be sorted first, then we only need to keep matching lines in memory
# Once we reach a non-matching line we print the lines in memory, prefixed by count
# with awk, variables are unset to begin with so, we can get away without explicitly initializing
{ # S2, S3, S4 are saved field values
if($2 == S2 && $3 == S3 && $4 == S4) {
# if fields 2,3,4 are same as last, save line in array, increment count
line[count++] = $0;
} else {
# new line with fields 2, 3, 4 different
# print stored lines, prefixed by the count
for(i in line) {
print count, line[i];
}
# reset counter and array
count=0;
delete line;
# save this line in array, increment count
line[count++] = $0;
}
# store field values to compare with next line read
S2 = $2; S3 = $3; S4 = $4;
}
END{ # on EOF we still have saved lines in array, print last lines
for(i in line) {
print count, line[i];
}
}
Привычно сохранить awk
скрипты в файле.
Вы можете использовать это по линии
Сортировать -k2,4 файл | awk -f скрипт
3 ID-fred 4.0 6.0 42.0
3 ID-jacob 4.0 6.0 42.0
3 ID-tessa 4.0 6.0 42.0
2 ID-elsa 5.0 8.0 45.0
2 ID-trudy 5.0 8.0 45.0
1 ID-gerard 6.0 8.0 20.0
Решение awk
:
$ awk -v pat="$(awk '/SETTINGS START/,/SETTINGS END/' file2)" -v p=1 '
/SETTINGS START/{p=0};p;/SETTINGS END/{print pat;p=1}' file1 > file3
ANJALI
NISHA
// +++ CUSTOMIZATION SETTINGS START +++
WELCOME ALL
// +++ CUSTOMIZATION SETTINGS END +++
PREETI
MONA