'grep positive/negative integer value only
I am looking to grep any positive/negative integers only and no decimals, or any other variation including a number.
I have a testpart1.txt which has:
This is a test for Part 1
Lets write -1324
Amount: $42.27
Numbers:
-345,64
067
Phone numbers:
(111)222-2424
This should output the following code:
This is a test for Part 1
Lets write -1324
067
I am new to bash and I cannot find how to exclude any number separated with a character like a '.' or ','. I am able to get all the numbers with the following code but I am stuck after that:
egrep '[0-9]' testpart1.txt
This gives me the opposite of what I want:
grep '[0-9]\.[0-9]' testpart1.txt
Solution 1:[1]
You may use this grep
:
grep -E '(^|[[:blank:]])[+-]?[0-9]+([[:blank:]]|$)' file
This is a test for Part 1
Lets write -1324
067
Details:
-E
: Enables extended regex matching(^|[[:blank:]])
: Match line start or a space or a tab character[+-]?
: Match optional+
or-
[0-9]+
: Match 1 or more digits([[:blank:]]|$)
: Match line end or a space or a tab character
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | anubhava |