Ondrej11
Ondrej11

Reputation: 63

Find numbers using regular expression with egrep in Linux terminal

I want to find all separated words (which means characters between two spaces), that are decimal numbers including plus and minus signs in Linux terminal using egrep.

My solution:

(?<= |\n|\t)[\+\-]?[0-9]+(?= |\n|\t)

Explanation:

This code works well in program Kiki 0.5.6 where I test implementation, but if I copy it to terminal, it doesn't work. I think that terminal doesn't recognize special parentheses constructions (?= or ?<=). Am I right? How can I apply to terminal?

For example: my text:

1.fasfa
123asfavdsvdas156
1safsavdsvsd1sdva5s31as35d1va
595s6dva2sdvas9
asd9as5dv92s
sd559vs fs5s94 4dfs dfa4s44 459    9dasf 8sdfa 5sfa
napr. uNIveRziTA
sfaf 2262 2226 56565 adss
uNiVerZita
uNIVERZITa
123
123 sadasf 123456 sfafs 134
-1234- -25- -5- 5- --55
-
-55
123 100 999 124 6262 62 6 2 62 62 65 26565 22 62 62652 +665 +0649  ---662 265 959 595 099 199 -059 -0245 -444
--1245 -555-5-55 --555- 555-
+25
 -55 
+++55 +5 ++5 ++55+665+
samo samo  samo   samo otec otec skola skola     samo lamo samo lamo
re20. (?<=(\t|\n| ))([+-])?[1-9][0-9]*(?= |$|\n)
--- ---
doma doma doma doma doma doma doma doma doma
[email protected] [email protected] [email protected] [email protected]

23:56:59.555
00:00:00.000
23:59:59.999

31/12/2099
00/12/2054
01/01/2000

matches:

459
2262
2226
56565
123
123
123456
134
-55
123
100
999
124
6262
62
6
2
62
65
26565
22
62
62652
+655
+0649

Upvotes: 1

Views: 220

Answers (1)

hek2mgl
hek2mgl

Reputation: 158150

egrep does not support lookaround assertions. However, GNU grep comes with perl compatible regular expressions using the -P switch:

grep -oP '(?<=\s|^)[+-]?[0-9]+(?=\s|$)' input

Note that you can simplify |\n|\t to \s which stands for whitespace character. In order to match numbers that start at the begin of a line and numbers that end at the end of the line I've added ^ and $ as alternatives for \s.

Upvotes: 1

Related Questions