please advice how to match only the valid IPs ( 255.255.255.255 ) from the file.txt and insert only the valid IP into VALID_IP.txt file
- ( see VALID_IP.txt for example )
the solution should be implemented in my ksh script ( so perl or sed or awk is fine also )
more file.txt
e32)5.500.5.5*kjcdr
##@$1.1.1.1+++jmjh
1.1.1.1333
33331.1.1.1
@5.5.5.??????
~3de.ede5.5.5.5
1.1.1.13444r54
192.9.30.174
&&^#%5.5.5.5
:5.5.5.5@%%^^&*
:5.5.5.5:
**22.22.22.22
172.78.0.1()*5.4.3.277
example of VALID_IP.txt file
1.1.1.1
192.9.30.174
5.5.5.5
5.5.5.5
5.5.5.5
22.22.22.22
172.78.0.1
The following is a suitable regex, split onto 4 different lines for the sake of my own sanity.
Output:
Obviously this doesn't match your example. Why? Because we can't tell that that a 3 doesn't belong with a one. As you can see, garbage numbers can't be cleanly guessed at.
It's slightly cleaner with perl
but it still gets false positives
Perl one liner
I recommend using range checking instead of hairy regexes. You can do this in ksh without using an external utility or another language. Although Iain's solution is nice, it's not a core module.
Here's pure ksh. There's no need to make it a one-liner, just use a function. Code like this is easier to understand, easier to check for correctness and easier to maintain.
This is ksh 93, I haven't tested it in ksh 88. It also runs unchanged in Bash 3.2 or higher.