Practical Text Pattern Matching with Grep
Create file.txt with the following content:
48 Dec 3BC1977 LPSX 68.00 LVX2A 138
483 Sept 5AP1996 USP 65.00 LVX2C 189
47 Oct 3ZL1998 LPSX 43.00 KVM9D 512
219 dec 2CC1999 CAD 23.00 PLV2C 68
484 nov 7PL1996 CAD 49.00 PLV2C 234
483 may 5PA1998 USP 37.00 KVM9D 644
216 sept 3ZL1998 USP 86.00 KVM9E 234
Count lines containing "48":
grep -c "48" file.txt
Show line numbers for lines containing "48":
grep -n "48" file.txt
Match lines with exact word "48":
grep -w "48" file.txt
Extract records with codes 484 or 483:
grep -E "(484|483)" file.txt
Display lines not starting with 4 or 8:
grep -E "^[^48]" file.txt
Find lines contianing September (Sept):
grep -w Sept file.txt
Show codes beginnnig with K and ending with D:
grep -E "\bK.*D\b" file.txt
Locate codes with two uppercase letters followed by any characters ending in C:
grep -E "\b[A-Z]{2}.*C\b" file.txt
Retrieve records starting with 5 and ending with either 1996 or 1998:
grep -E "\b5.*(1996|1998)\b" file.txt