I want to remove a line from a file which contains a particular character only once, if it is present more than once or is not present then keep the line in file.
For example:
DTHGTY
FGTHDC
HYTRHD
HTCCYD
JUTDYC
Here, the character which I want to remove is C
so, the command should remove lines FGTHDC
and JUTDYC
because they have C
exactly once.
How can I do this using either sed
or awk
?
Best Answer
In
awk
you can set the field separator to anything. If you set it toC
, then you'll have as many fields +1 as occurrences ofC
.So if you say
awk -F'C' '{print NF}' <<< "C1C2C3"
you get4
:CCC
consists in 3C
s, and hence 4 fields.You want to remove lines in which
C
occurs exactly once. Taking this into consideration, in your case you will want to remove those lines in which there are exactly twoC
-fields. So just skip them: