grep command using wildcards [0-9]
grep ".0000000" data > outputI 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 > outputThe 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?
41 Answer
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\}$"