interesting grep behaviour

I suppose that this is not actually a script question, but I noticed this while working on a bash script homework assignment and I have been impressed with the quality of posts here -- so that is why I posted it here.

I have this text file named textfile:

total 40
-rwxr-xr-x 1 joeblow users 356 Jun 14 13:28 count.ok
-rwxr-xr-x 1 joeblow users 411 Jun 14 13:30 count.s
-rwxr-xr-x 1 joeblow users 344 Jun 14 12:50 count.shhht
-rwxr-xr-x 1 joeblow users 168 Jun 13 11:55 example.s
-rwxr-xr-x 1 joeblow users 71 Jun 13 19:37 example2.s
-rwxr-xr-x 1 joeblow users 262 Jun 12 20:21 findtext.s
-rwxr-xr-x 1 joeblow users 203 Jun 12 11:58 findtext.txt
-rwxr-xr-x 1 joeblow users 547 Jun 13 11:59 right.s
-rwxr-xr-x 1 joeblow users 286 Jun 12 20:12 showargs.s
-rwxr-xr-x 1 joeblow users 197 Jun 12 11:58 showargs.txt
-rw-r--r-- 1 joeblow users 0 Jun 14 16:32 textfile

I do a "grep -n .s textfile"

$ grep -n .s textfile
2:-rwxr-xr-x 1 joeblow users 356 Jun 14 13:28 count.ok
3:-rwxr-xr-x 1 joeblow users 411 Jun 14 13:30 count.s
4:-rwxr-xr-x 1 joeblow users 344 Jun 14 12:50 count.shhht
5:-rwxr-xr-x 1 joeblow users 168 Jun 13 11:55 example.s
6:-rwxr-xr-x 1 joeblow users 71 Jun 13 19:37 example2.s
7:-rwxr-xr-x 1 joeblow users 262 Jun 12 20:21 findtext.s
8:-rwxr-xr-x 1 joeblow users 203 Jun 12 11:58 findtext.txt
9:-rwxr-xr-x 1 joeblow users 547 Jun 13 11:59 right.s
10:-rwxr-xr-x 1 joeblow users 286 Jun 12 20:12 showargs.s
11:-rwxr-xr-x 1 joeblow users 197 Jun 12 11:58 showargs.txt
12:-rw-r--r-- 1 joeblow users 0 Jun 14 16:32 textfile

Why would lines that do not contain ".s" be listed??

Thanks,
-dog

try

grep -n "\.s" textfile
1 Like

Hi
grep .s means to grep for the pattern: ' any character(.) followed by s". In your case, the lines containing the word "users" in the group section get satisfied by your search pattern, and hence the below result.

Guru.