Hi, I am having a lot of issues using grep. I am trying to counts the number of lines which start with the letter “L”or “W” and end with the number 3.
I can find and count the lines that start with L or W just fine, but I cannot get it to work when i add the bit about the line ending with the number 3.

here is one of the lines it should pick up:
Lylac, Kenneth R. BS UNDECLARED 3.403
Starts with an L and ends with a 3.

so far i have tried just about everything to get it to work. Here is what i have working so far:

$ grep -c '^[L,W]' rolllist.txt

I know the $ is gets the end of the line, but i cant get it to work.
Any help would be much appreciated.

What have you tried?

Lots of things, but nothing has worked. I am still learning how to use grep so I am bashing my head against a wall. :D
I havent even been able to get it to count the number of lines that end in 3. If I could get that I can figure the rest out.

Try

grep -c '^[LW].*3$' rolllist.txt

^ From the begining of the line
[LW] match any character in the brackets
.* followed by any number of any character
3 then a 3
$ at the end of line.

[c33207@snowball ~]$ grep -c '^[LW].*3$' rolllist.txt
0

Nope :(

[shibblez@plymouth Download]$ echo "Lylac, Kenneth R. BS UNDECLARED 3.403" | grep -c '^[LW].*3$'
1

Works for me. Are you sure that the line ends with 3? There aren't any trailing spaces?

[shibblez0@plymouth Download]$ echo "Lylac, Kenneth R. BS UNDECLARED 3.403 " | grep -c '^[LW].*3$'
0
[shibblez@plymouth Download]$ echo "Lylac, Kenneth R. BS UNDECLARED 3.403 " | grep -c '^[LW].*3 *$'
1
[shibblez@plymouth Download]$

Try '^[LW].*3 *$' as your RE - it ignores any number of spaces at EOL.

Hmmm.. but that requires me to add that to the file, it doesnt count the 2 other lines that have L or W as the first letter and 3 as the last.
White, Brian C. 0011578904 **Web Registered** BS COMPUTER SCIENCE 3.003

I don't understand. This RE works whether or not there is a trailing space.

[shibblez@plymouth Download]$ echo "White, Brian C. 0011578904 **Web Registered** BS COMPUTER SCIENCE 3.003" | grep -c '^[LW].*3 *$'
1
[shibblez@plymouth Download]$ echo "White, Brian C. 0011578904 **Web Registered** BS COMPUTER SCIENCE 3.003 " | grep -c '^[LW].*3 *$'
1
[shibblez@plymouth Download]$

But that is still only counting the file you echo, it doesnt count any of the others.

[shibblez@plymouth ~]$ cat testdata.txt
White, Brian C. 0011578904 **Web Registered** BS COMPUTER SCIENCE 3.003
White, Brian C. 0011578904 **Web Registered** BS COMPUTER SCIENCE 3.003
Smith, George   0012132343 **Web Registered** BS COMPUTER SCIENCE 3.004
Smith, George   0012132343 **Web Registered** BS COMPUTER SCIENCE 3.004
Lylac, Kenneth R. BS UNDECLARED 3.403
[shibblez@plymouth ~]$ hexdump -C testdata.txt
00000000  57 68 69 74 65 2c 20 42  72 69 61 6e 20 43 2e 20  |White, Brian C. |
00000010  30 30 31 31 35 37 38 39  30 34 20 2a 2a 57 65 62  |0011578904 **Web|
00000020  20 52 65 67 69 73 74 65  72 65 64 2a 2a 20 42 53  | Registered** BS|
00000030  20 43 4f 4d 50 55 54 45  52 20 53 43 49 45 4e 43  | COMPUTER SCIENC|
00000040  45 20 33 2e 30 30 33 0a  57 68 69 74 65 2c 20 42  |E 3.003.White, B|
00000050  72 69 61 6e 20 43 2e 20  30 30 31 31 35 37 38 39  |rian C. 00115789|
00000060  30 34 20 2a 2a 57 65 62  20 52 65 67 69 73 74 65  |04 **Web Registe|
00000070  72 65 64 2a 2a 20 42 53  20 43 4f 4d 50 55 54 45  |red** BS COMPUTE|
00000080  52 20 53 43 49 45 4e 43  45 20 33 2e 30 30 33 [B]20[/B]  |R SCIENCE 3.003 |
00000090  0a 53 6d 69 74 68 2c 20  47 65 6f 72 67 65 20 20  |.Smith, George  |
000000a0  20 30 30 31 32 31 33 32  33 34 33 20 2a 2a 57 65  | 0012132343 **We|
000000b0  62 20 52 65 67 69 73 74  65 72 65 64 2a 2a 20 42  |b Registered** B|
000000c0  53 20 43 4f 4d 50 55 54  45 52 20 53 43 49 45 4e  |S COMPUTER SCIEN|
000000d0  43 45 20 33 2e 30 30 34  0a 53 6d 69 74 68 2c 20  |CE 3.004.Smith, |
000000e0  47 65 6f 72 67 65 20 20  20 30 30 31 32 31 33 32  |George   0012132|
000000f0  33 34 33 20 2a 2a 57 65  62 20 52 65 67 69 73 74  |343 **Web Regist|
00000100  65 72 65 64 2a 2a 20 42  53 20 43 4f 4d 50 55 54  |ered** BS COMPUT|
00000110  45 52 20 53 43 49 45 4e  43 45 20 33 2e 30 30 34  |ER SCIENCE 3.004|
00000120  [B]20[/B] 0a 4c 79 6c 61 63 2c  20 4b 65 6e 6e 65 74 68  | .Lylac, Kenneth|
00000130  20 52 2e 20 42 53 20 55  4e 44 45 43 4c 41 52 45  | R. BS UNDECLARE|
00000140  44 20 33 2e 34 30 33 0a                           |D 3.403.|
00000148
[shibblez@plymouth ~]$ grep '^[LW].*3 *$' testdata.txt
White, Brian C. 0011578904 **Web Registered** BS COMPUTER SCIENCE 3.003
White, Brian C. 0011578904 **Web Registered** BS COMPUTER SCIENCE 3.003
Lylac, Kenneth R. BS UNDECLARED 3.403
[shibblez@plymouth ~]$

Here's a file with 5 records in it. Records 2 and 4 have a trailing space which is highlighted in bold. The RE matches 3 out of the 5 line, which is correct.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.