Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Practical Text Pattern Matching with Grep

Tech Sep 30 10

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

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.