Grep lines accordingly

Hi Guys,
I need help..
I have 2 files with some lines the script will grep lines available in file1 with file2 & prints if a line is missing on file2

#!/bin/bash
set -x
while read path
do
egrep $path file2
if [ `echo $?` != 0 ];then
echo "path $path is missing " >> output
fi
done < file1
more file1
/dell/sony/power/IS/server
/dell/sony/power/IS/
/dell/sony/power/
/dell/sony/
/dell/
/dev/testcase failing

more file2
/dell/sony/power/IS/server
/dell/sony/power/IS/
/dell/sony/power/
/dell/

I m getting the wrong OUTPUT:

path /dev/testcase failing is missing

output should be like >

path /dev/testcasefor/failing  is missing 
path /dell/sony/ is missing
awk '{arr[$0]++}       
       END {for (i in arr){if(arr<2 ){print i, " is missing"}} } ' file1  file2 > error_listing

awk may be a better choice. Try something like the above.

1 Like

The output is correct, /dev/sony is present. The grep command will match any part of a line that matches. You probably want to use the -w flag for exact word search.

Hi.

If you allow exact, entire line matches:

$ fgrep -x -v -f z9 z8
/dell/sony/
/dev/testcase failing

Because /dell can match a number of strings.

       -x, --line-regexp
              Select only those matches that exactly match the whole line.
-- excerpt from man fgrep

Best wishes ... cheers, drl

1 Like

Hi,
For fun (under linux for "xargs"):

$ sort file1 file2 file2 | uniq -u | xargs -IXX echo path XX is missing 
path /dell/sony/ is missing
path /dev/testcase failing is missing

Regards.

1 Like