Use awk sed command and while loop to remove records from second file
I have two output files:
- FILE-A contains over 70,000 unique records.
- FILE-B contains a unique list that I need to remove from FILE-B.
FILE-A:
TOM
JACK
AILEY
BORG
ROSE
ELI
FILE-B content:
TOM
ELI
I want to remove everything listed in FILE-B from file-A.
FILE-C (results file):
JACK
AILEY
BORG
ROSE
I am guessing I need an operator while r for i
. Can someone help me with this? I need to cat
both read FILE-A and for every line in FILE-B I need to remove this from FILE-A.
Which command should you use?
+3
source to share
4 answers
You don't need a loop, just one command awk
or sed
:
AWK:
awk 'FNR==NR {a[$0];next} !($0 in a)' FILE-B FILE-A >FILE-C
SED:
sed "s=^=/^=;s=$=$/d=" FILE-B | sed -f- FILE-A >FILE-C
Note:
- As long as the sed version is running for the data shown, it will not process any text in FILE-B that could be interpreted as a regex pattern.
- Awk's solution reads FILE-B completely into memory. He has no restriction on interpreting the text asregexas a solution
sed
.
+1
source to share