Home > Blockchain >  Bash script to remove some keywords in a txt file
Bash script to remove some keywords in a txt file

Time:11-27

i am not familiar with bash script, after some research, i found some hints but still need your effort.

Given i have a resources.txt, contains

a
b
c
d

and a whitelist.txt file, contains

c
d

I would like to remove all items that exactly match from whitelist file to resource file. so the expected output is

a
b

Expect c and d is removed because they are in whitelist file.

I have created below script to read it, but don't know how to replace each one by one to resource file.

# read the whitelist file
echo whitelist.txt | awk '{for (i=1; i<=NF; i  ) printf "%s\n",$i}

# replace item in resource file
awk '{sub(/c/,""); print}' resources.txt

Your help is greatly appreciated, thanks a lot!!

CodePudding user response:

I would use this grep:

grep -Fvxf whitelist.txt resources.txt
  • -F fixed/literal strings (no regex)
  • -f FILE get patterns from FILE
  • -x match the whole line
  • -v print lines which don't match
  • This grep is POSIX

CodePudding user response:

Using awk you can use

awk 'FNR==NR{a[$0];next}!($0 in a)' whitelist.txt resources.txt

In parts

awk '
FNR==NR{                       # Only for the first file (whitelist.txt)
  a[$0]                        # Set the value of the whole line as a key in a
  next                         # Go to the next record
}
!($0 in a)                     # Print the line if the value of `$0` does not exists as array index in a
' whitelist.txt resources.txt

Output

a
b
  • Related