grep not equal to condition

I have below files under dir_a and dir_b and i want to sort out number of dir_a and dir_b files seperately and if i do the grep it should retrun 2 files in dir_a and 1 file in dir_b.

/dir_a/12345678
/dir_a/87654321
/dir_a/dir_b/12345687

But i am getting

cat file|grep dir_a|wc -l
       3
cat file|grep dir_b|wc -l
       1

Is there a way we can issue a not equal to condition in grep?

Yes use -v switch to grep; also pass filename to grep and avoid using cat:

grep -v 45 file
/dir_a/87654321

I don't believe a not grep expression will solve your issue. Perhaps you want something like grep "^/dir_a/" , this will print any line that begins with "/dir_a/"

I have already tried those conditions but no luck, so when i want to count files in dir_a it should only give me 2 files but not 3. Is there a better wat to get the accurate number.

cat file|grep dir_a|wc -l
       3
cat file|grep dir_b|wc -l
       1

---------- Post updated at 03:40 PM ---------- Previous update was at 03:37 PM ----------

Btw if it begins with dir_a also i will see 3 files in dir_a even though there are only 2 because when i have a list of files in one file with the full path, that is how you will get the number.

grep '^/dir_a/[^/]*$'

Regards,
Alister

2 Likes

This worked like a charm:b:

For the input you listed, the following should work:

grep -c -e "^/dir_a/[^/]*$" file
grep -c -e "^/dir_a/dir_b/[^/]*$" file
1 Like

This worked like a CHARM :b: