0

I will fetch the file names from the file say: FILE_A, and will search these file names in another file say: File_B Using the script say: script.sh

I want to print those file names which are not present in a file say: FILE_B. I use the code but it didn't work.

Code in the script->script.sh is as follows:

#!/bin/bash
while read line

do
       grep -v "$line" FILE_B

done<FILE_A

please help me. why it is not working and what is the solution of it?

Incognito
  • 2,964
  • 2
  • 27
  • 40
ANUJ GUPTA
  • 13
  • 4

5 Answers5

2

grep can read its input from a file; no need for a loop.

grep -Fxvf FILE_A FILE_B

The -F option specifies that the input is literal strings, not regular expressions. Otherwise an input which contains regex metacharacters would not match itself; or not only itself. For example, the regular expression a.c matches "aac", "abc", etc.

The -x option requires a full-line match. Otherwise, the input "bc" would match on any line containing it as a substring, such as "abcd".

The -v option says to print non-matching lines instead of matching.

Finally, the lowercase -f option specifies a file name as its argument to use as input for the patterns to match.

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • It is not giving the output though the command is running, your answer need some more addition/modification to print it. Please reply. – ANUJ GUPTA Mar 11 '14 at 06:20
  • I can reply but not really help without more information. It works for me. http://pastebin.com/9y2YvwEN – tripleee Mar 11 '14 at 07:42
  • but it is not working in my environment, the sample u shown over pastebin(link mention in the reply), i need it be working as similar. if u know why it is not working then please share the information. – ANUJ GUPTA Mar 13 '14 at 10:29
  • I'll write this slowly: If you don't provide additional information, we can only guess. DOS line feeds? Cosmic rays? Voices inside your head? – tripleee Mar 13 '14 at 10:35
  • Try this: http://pastebin.com/PZdwWSzt – tripleee Mar 13 '14 at 10:54
1

comm is good for this, but it requires the input files to be sorted. If that's not a problem:

# lines in FILE_A that are not in FILE_B
comm -23 <(sort FILE_A) <(sort FILE_B)
glenn jackman
  • 238,783
  • 38
  • 220
  • 352
0

This script works but does not print what you want. For each filename in FILE_A it prints all the OTHER filenames in FILE_B. Instead you should print the filename yourself if grep does not find it:

while read line
do
  grep "$line" FILE_B >/dev/null || echo "$line"
done <FILE_A
SzG
  • 12,333
  • 4
  • 28
  • 41
0
  1. No extra linefeed between while and do
  2. grep -v expr file will print all lines of those files, not containing expr. What you want, is just the result whether it's found or not. You need to test the exit state.

Try:

#!/bin/bash
while read line
do
       grep -q "$line" FILE_B || echo "$line"

done<FILE_A

grep returns exit 0 if a line was found. The || concatenation with echo means: execute echo when exit state != 0- i.e. when $line was not found.

Axel Amthor
  • 10,980
  • 1
  • 25
  • 44
0

Use this instead

#!/bin/bash
while read line
do
    if grep -qw $line "file_B"
    then
    echo $line
    fi
done < file_A
Ankit Kumar
  • 1,433
  • 1
  • 16
  • 24
  • It prints the file name which are present in FILE_B I need to print only all those file names which are not present in FILE_B. your ans needs some modifications. Please reply. – ANUJ GUPTA Mar 11 '14 at 06:03
  • It prints the file name which are present in FILE_B I need to print only all those file names which are not present in FILE_B. your ans needs some modifications. Please reply. – ANUJ GUPTA Mar 11 '14 at 06:10