grep command using wildcards [0-9]

in flag
grep ".0000000" data > output

I extract the all numeric data ending with .0000000 in the data text file. When I changed this code using wildcard as follows:

grep ".[0-9][0-9][0-9][0-9][0-9][0-9][0-9]" data > output

The above code is supposed to extract all numeric data ending with any seven digits after the dot, but it does not work as it is supposed to be. How I can modify the above code to extract all numeric data ending with any seven digits after the dot?

hr flag
`.` in grep regex matches any single character - if you want to match a literal dot (period) you need `\.` or `[.]`
ar flag
@steeldriver your comment looks like the answer! :)
hr flag
@user68186 maybe... I dunno. The description "it does not work as it is supposed to be" is rather vague... it probably at least needs some kind of boundary or anchor as well if the OP wants *exactly* 7 digits
deepblue avatar
in flag
@steeldriver, if you answer this question I'll accept it. Your solution works.
hr flag

It's not clear from your description whether your expression is failing to match things you want, or matching things you don't want.

If it's the latter, then it may be because . in a grep regular expression matches any single character (except the newline character - however grep is normally line-based anyway). To match a literal dot (period) you need to escape it \. or place it in a character set as you have done for the decimal digit ranges:

grep "[.][0-9][0-9][0-9][0-9][0-9][0-9][0-9]"

You also mention that the expression should match data "ending with" - it's not clear whether you mean a line ending or a word boundary - these are respectively $ and \b (or \>) ex.

grep "[.][0-9][0-9][0-9][0-9][0-9][0-9][0-9]$"

grep "[.][0-9][0-9][0-9][0-9][0-9][0-9][0-9]\b"

You can also shorten the expression using a quantifier - switching to extended regular expression (ERE) mode1:

grep -E "[.][0-9]{7}$"

1 In GNU grep, you can use quantifiers in basic regular expression (BRE) mode by escaping the braces grep "[.][0-9]\{7\}$"


Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.