URGENT -task is to find all distinct IP addresses from all the files in the /root/u01/testing/ directory and print them in lexicographical
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
URGENT -task is to find all distinct IP addresses from all the files in the /root/u01/testing/ directory and print them in lexicographical
Hi friends,
I need sample program either in bash or python
Question:You have a hierarchy of directories and somefiles in some of those directories.
e.g /u01/testing/dir1/file1.txt,file2.txt....
/u01/testing/dir2/file.txt ,file1.txt ....
/u01/testing/dir2/file2.txt .......
some of these files contain IP addresses inside the text. An IP address is a string of form x.x.x.x where x is a number from 0 to 255(inclusive).
example file.txt that looks like below.
file1.txt
---------
hello mike 127.0.0.1
this is some example 128.99.107.55
file with some correct and incorrect 128.128.4.11 ip 0.11.1115.78 addresses
file1.txt
---------
hello rocky 127.65.64.1 127.0.64.1 127.0.0.1
example 128.57.107.76 128.57.907.70
file with some correct and incorrect 67.128.4.11 ip 7.7.7.8 addresses
file5.txt
---------
hello rocky 127.69.64.1 127.0.68.1 127.0.1.1
example 128.57.10.76 128.57.9079.70
file with some correct and incorrect 67.128.4.11 ip 7.78.7.8 addresses
task is to find all distinct IP addresses from all the files in the /root/u01/testing/ directory and print them in lexicographical order.
output should be contain only valid ip addresses
-----------------
Good for you.
What have you tried ?. What didn't work as you expected ?. You attempt to solve your problem, we'll help where we can with problems you run in to.
Do not post homework assignments verbatim. We're happy to assist if you have specific questions or have hit a stumbling point, however. Let us know what you've already tried and what references you have used (including class notes, books, and searches) and we'll do our best to help. Keep in mind that your instructor might also be an LQ member.
That said,
Quote:
Originally Posted by urs428@gmail.com
print them in lexicographical order.
Lexicographical order, really? What for? If I were your instructor, I would require them to be printed in numerical order.
Now think ... maybe find the file names, extracting only the names ... then "pipe" that into egrep or awk to extract the IP-address-like portions ... then "pipe" that into sort.
Unix/Linux is specifically designed to make it easy to construct solutions like that, even on the command line, where several child processes work together by "piping" their STDOUT into the next's STDIN.
Nowadays, -R is supported by both GNU and *BSD grep, so find is probably unneeded. But the assignment strikes me as a completely contrived task.
Let's say those were some log files. In that case, it'd be reasonable to assume that they contained only valid IP addresses. Then you wouldn't have to validate them. Just matching against (\d{1,3}\.){3}\d{1,3} would be enough. And I definitely would like them to be printed in numerical order, so that spotting contiguous ranges would be easier.
Or those were some configuration files that might contain errors. In that case, a more hands-on task would be to match only against invalid IP addresses, i.e. series of four numbers separated by dots where at least one of the numbers were greater than 255. Of course, this wouldn't catch all the errors, but at least, the most obvious typos. But there's no point in sorting the results then. Rather, I'd like each invalid IP to be accompanied by the file name and the line number it was found at. And perhaps, some lines of surrounding context, too.
Now think ... maybe find the file names, extracting only the names ... then "pipe" that into egrep or awk to extract the IP-address-like portions ... then "pipe" that into sort.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.