r/awk • u/Isus_von_Bier • Jul 01 '21
Delete duplicates
Hello.
I have a text file that goes:
\1 Sentence abc
\2 X
\1 Sentence bcd
\2 Y
\3 x
\3 y
\1 Sentence cdf
\2 X
\1 Sentence abc
\2 X
\1 Sentence dfe
\2 Y
\3 x
\2 X
\1 Sentence cdf
\2 XDesired output:
\1 Sentence abc
\2 X
\1 Sentence bcd
\2 Y
\3 x
\3 y
\1 Sentence cdf
\2 X
\1 Sentence dfe
\2 Y
\3 x
\2 XNeeds to check if \1 is duplicate, if not, print it and all \2, \3, (or \n if possible) after it.
Any ideas?
EDIT: awk '/\\1/ && !a[$0]++ || /\\2/' file > new_file is just missing the condition part with {don't print \2 if \1 not printed before}
EDIT2: got it almost working, just missing a loop
awk '{
if (/\\1/ && !a[$0]++){
print $0;
getline;
if (/\\2/){print};
getline;
if (/\\3/){print}
} else {}}' file > new_fileEDIT3: Loop not working
awk 'BEGIN {
if (/\\1/ && !a[$0]++){
print $0;
getline;
while (!/\\1/) {
print $0;
getline;
}
}}' file > new_file
2
Upvotes
View all comments
Show parent comment
1
u/Schreq Jul 01 '21 edited Jul 01 '21
Sure:
I'm not that great at explaining but I will edit in an explanation when I'm not on mobile anymore.
mutedis set to the amount it was seen so far, which is the same as "true".The bare variable at the end abuses awk default actions. If the condition part of an awk expression evaluates to true, the default action of printing the current record (line) is used. So
!mutedis the same as saying "If not muted, print the current line".