0

I have a file with numbers and words and I need to get only numbers:

File:

ASCII
Baron
Cbada
Dbadal
ebadals
Abadessa
Abadesses
Abadia
Abarca
jbarques
Zbat
Abdalong
Rbdies
Abdon
Abel
ibelard
Abell
Abella
Abellan
Abellana
quebellanes
370
410
1
12
123
1234
12345
123456
1234567
12345678
123456789
87654321
7654321
654321
54321
4321
321
21
937895432
938965432
930895432
913769890
916767800
91676780
977895432

if I do egrep "\b[^9][0-9]+" I do not get the one starting with 97.

And obviously "\b[^91][0-9]+" don't show the ones starting with 9 or 1.

If I do egrep -v "\b9[13]" I get the words, which I don't want...

3 Answers3

2

Yes, thanks to the comment of @QuickishFM I came up with this solution:

egrep -v "\b9[13]" file | egrep "[0-9]+"

Thanks

  • Yes, `9[13]` is even shorter than `9(1|3)` and works with the grep without "e"! – Freddy Apr 25 '19 at 20:11
  • No worries - The process of using _pipes_ to daisy chain the inputs to process them bit by bit is fairly common in Linux - for example, if you wanted to find the local IP address, you could run `ifconfig | grep 192.168 | cut -d " " -f 10` which runs ifconfig, then greps for the line with the IP, then delimits by spaces and selects the IP address. If you are new to Linux or Unix I suggest practising this - it may greatly help you! – QuickishFM Apr 25 '19 at 20:58
1

Try this:

<file egrep '^[0-9]+' | egrep -v '^9(1|3)'

or

egrep -v '^9(1|3)|[[:alpha:]]' file

-v, --invert-match
       Invert the sense of matching, to select non-matching lines.

Freddy
  • 1,465
  • 6
  • 13
0

Using grep -P (PCRE) with a negative lookahead to make sure lines don't begin with 91 or 93:

grep -P  '^(?!9[13])[0-9]+$' file
370
410
1
12
123
1234
12345
123456
1234567
12345678
123456789
87654321
7654321
654321
54321
4321
321
21
977895432
Toto
  • 17,001
  • 56
  • 30
  • 41