Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

179
Views
How can I match lines with exactly three numbers?

How can I use grep to match 3 numbers in a file? My file looks like this:

123  
122    
222  
333443  
fdsfs5454353  
dsfsfjsk4654641

Note that some of the lines contain trailing spaces. I want to only match three digit numbers. I tried:

grep -E [0-9]{3} test.txt
grep -E '\<[0-9]{3}\>' test.txt
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

grep '^[0-9][0-9]*' test|awk '{if(length($0) == 3) print $0}' 

or if you have whitespace:

sed 's/[ \t]*$//' test|grep   '^[0-9][0-9]*'|awk '{if(length($0) == 3) print $0}'

(thanks @shellter)

over 4 years ago · Santiago Trujillo Report

0

Use Extended Regular Expressions with Bounds

I asked if you meant numbers with exactly three digits, or each three-digit match in a string. You replied that you wanted only lines that contained exactly three digits.

Extended grep provides an easy solution for this. Consider the following:

$ egrep '^\d{3}\b' /tmp/corpus 
123  
122    
222

This uses a bound (also known as a range) to look for exactly three digits at the start of each line, followed by a word boundary. The word boundary will match trailing space or the end of line, ensuring that you get the proper match in either case.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!