3

I have 2 text file and want remover lines in file A which contain the strings in file B

file A:

joe     ball     1335
john    dyer     1365
dylan   fisher   1795
ian     gill     1913
eric    kelly    1101

file B:

1795
1913

And I want Bash code get result like this:

joe     ball     1335
john    dyer     1365
eric    kelly    1101

I try this codes but the answer did not work out

$ grep -vwF -f A B
$ awk -F'[ ,]' 'FNR==NR{a[$1];next} !($4 in a)'
pedram
  • 35
  • 3

3 Answers3

5
awk  'NR==FNR{a[$1];next} !($3 in a)' fileB fileA

It uses space as field separator, and $1 is the first column element of a line, $3 is the 3rd column element of the line. use array a store fileB elements a[$1]. checks the 3rd column element of fileA whether in array a, if not print the whole line Output:

joe     ball     1335
john    dyer     1365
eric    kelly    1101
Haifeng Zhang
  • 30,077
  • 19
  • 81
  • 125
0

Using grep:

$ grep -v -f B A
joe     ball     1335
john    dyer     1365
eric    kelly    1101

That is at the simplest but will create collisions under circumstances. If you mangle B a bit with for example sed first (add space to beginning and $ to the end of search strings):

$ sed 's/$/$/;s/^/ /' B
 1795$
 1913$

and use it in process substitution:

$ grep -v -f <(sed 's/$/$/;s/^/ /' B) A
joe     ball     1335
john    dyer     1365
eric    kelly    1101
James Brown
  • 36,089
  • 7
  • 43
  • 59
-1

cat file_B | while read values; do grep $values file_A >> newfile; done

RJJ
  • 184
  • 3
  • 11
  • so many errors in so little code... UUOC, missing setting of IFS, missing read argument, unquoted variable, matching across the whole line instead of the desired field, appending to the file in the loop instead of writing to it once outside the loop, and [using a shell loop to manipulate text](http://unix.stackexchange.com/questions/169716/why-is-using-a-shell-loop-to-process-text-considered-bad-practice). – Ed Morton Dec 19 '16 at 14:46
  • not erors, no. it works and answers the question. your link isn't relevant either. – RJJ Dec 20 '16 at 21:13
  • UUOC not only has it's own abbreviation it has it's own web page so you're in a minority in thinking its fine. You can add 20 more useless commands (e.g. `awk '1' | tr 'a' 'a' | sed 's/x/x' | ...` ) without a problem but that's not a reason to do so. The link I provided is extremely relevant, you should really re-read it until you understand all of it's points and ask questions if you don't understand any of them. People are asked to provide simple, minimal examples that demonstrate their problem - YMMV with assuming that what they post is the full extent of their real input/problem. – Ed Morton Dec 20 '16 at 21:18
  • No, it does not work. It will produce the expected output for those few lines of sample input but it will find false matches and/or fail to find correct matches given other input and will be extremely and noticeably slow given even moderately sized input files. Software that produces the expected output given a small sample input set is the starting point to identifying a solution, not the end point. You clearly have your mind set that there's nothing wrong with your code and so there's really nothing more I can say but good luck with that. – Ed Morton Dec 20 '16 at 21:44