Reputation: 17504
I am trying to find out a line having a two digit number in it.Eg:
test.txt
uuLinuxxx
Linux 2011
2011 Linux RedHat
Linux RedHat
2011
2013 2014
2010
/usr/bin
2
Ubuntu 20 world
Desired OP: Ubuntu 20 world
i am using
sed -n '/[0-9]\{2\}/p' test.txt
but the problem is that it is printing all the lines having 2 or more digit number.
Upvotes: 2
Views: 10056
Reputation: 58410
This might work for you (GNU sed):
sed -n '/\b[0-9]\{2\}\b/p' file
or:
sed -nr '/\b[0-9]{2}\b/p' file
or:
sed -r '\b[0-9]{2}\b/!d' file
or:
sed '/\<[0-9]\{2\}\>/!d' file
Upvotes: 5
Reputation: 77105
Use -w
switch to match word (in your case digits).
grep -w '[0-9][0-9]' file
From man
page:
-w, --word-regexp
Select only those lines containing matches that form whole words.
The test is that the matching substring must either be at the beginning of the
line, or preceded by a non-word constituent character. Similarly, it must be either
at the end of the line or followed by a non-word constituent character. Word-
constituent characters are letters, digits, and the underscore.
Upvotes: 2
Reputation: 10039
sed -n 's/.*/²&²/;/[^0-9][0-9]\{2\}[^0-9]/ s/.\(.*\)./\1/p' YourFile
Using a temporary border to allow only 1 check for extraction of the line with a 2 digit only number inside
Upvotes: 1
Reputation: 881423
The problem is that your regular expression is looking for two consecutive digits, which exist is the numbers 20
(good) and 99999999999999999999
(not so good).
What you need is a regular expression ensuring that there are no digits on either side of the two you find, such as:
[^0-9][0-9]{2}[^0-9] # non-digit the two digits then non-digit
In addition, you need to trap those where the two digits are at the start or end of the line (or the only thing on the line). So you need multiple regular expressions separated by the or conjunction |
or as separate -e
arguments:
^[0-9]{2}[^0-9] # at start of line
[^0-9][0-9]{2}[^0-9] # in middle of line
[^0-9][0-9]{2}$ # at end of line
^[0-9]{2}[^0-9]$ # only thing on line
You may also want to choose a better tool for the job, such as grep
. Using the slightly modified input file:
uuLinuxxx
Linux 2011
2011 Linux RedHat
Linux RedHat
2011
2013 2014
2010
/usr/bin
2
Ubuntu 20 world
99 at the start
at the end: 99
88
the following command (split for readability):
grep -E -e '[^0-9][0-9]{2}[^0-9]'
-e '^[0-9]{2}[^0-9]'
-e '[^0-9][0-9]{2}$'
-e '^[0-9]{2}[^0-9]$' test.txt
gives you what you want:
Ubuntu 20 world
99 at the start
at the end: 99
88
Of course, if you have GNU grep
with its Perl-based regular expressions, and you're after "words" which are two-digit numbers, this becomes much easier:
grep -P '\b\d{2}\b' test.txt
but, if you can guarantee that word restriction, the following will also work:
grep -Ew '[0-9]{2}' test.txt
Upvotes: 1