Quantcast
Viewing all articles
Browse latest Browse all 8

Answer by muru for Grep for a line containing only 5 or 6 numbers

grep -E '[0-9]{5}' is looking for numbers with at least 5 digits. What you need is 5 numbers with at least one digit:

grep -E '[0-9]+([^0-9]+[0-9]+){4}'
  • [0-9]+ - a number of at least one digit
  • [^0-9]+[0-9]+ - a number with at least one digit, preceded by at least one non-digit character. We then repeat this 4 times to get 5 numbers separated by non-digits.
  • If the requirement is exactly 5, you might want to surround this regex with [^0-9] so that the entire line is matched (with the anchors, of course).
  • Depending on what you want here (does 1,2,3,4,6 qualify?), you might look at other separators. For example, a proper scientific notation real number would look like: [+-]?(([0-9]+(\.[0-9]+)?)|([0-9]?\.[0-9]+))([eE][+-][0-9]+)? So separators may not include ., e, etc. They may only be whitespace, as mikeserv notes. Or they maybe commas, if it's a CSV record. Or depending on the locale, a comma would be the decimal separator. Vary [^0-9] as per your need.

Viewing all articles
Browse latest Browse all 8

Trending Articles