LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 12-13-2021, 04:05 AM   #1
urs428@gmail.com
LQ Newbie
 
Registered: Dec 2021
Posts: 1

Rep: Reputation: Disabled
Angry 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
-----------------
 
Old 12-13-2021, 06:28 AM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,129

Rep: Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121
Quote:
Originally Posted by urs428@gmail.com View Post
I need sample program either in bash or python
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.
 
1 members found this post helpful.
Old 12-13-2021, 07:00 AM   #3
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
From the LQ Rules:
Quote:
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 View Post
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.

Last edited by shruggy; 12-13-2021 at 07:18 AM.
 
Old 12-13-2021, 09:27 AM   #4
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,662
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
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.
 
Old 12-13-2021, 12:11 PM   #5
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
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.
 
Old 12-13-2021, 12:17 PM   #6
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,226

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
Quote:
Originally Posted by sundialsvcs View Post
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.
And possibly uniq.

(Yes, I know sort can remove duplicates).
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Unable to start any executables or .sh files for Oracle 11g from /u01/app/oracle/product/11.2.0/db_1/bin sye Linux - Newbie 6 01-23-2017 12:18 AM
[SOLVED] Same files on distinct directory trees 0p3r4t4 Linux - General 1 11-25-2011 05:01 AM
How to get Nautilus to sort filenames in good ol' lexicographical order? Dan04 Linux - Software 1 03-14-2010 06:22 PM
sony vaio motion eye camera VCC-U01 xiral Linux - Laptop and Netbook 2 11-26-2007 02:38 PM
how come /u01 space is suddenly increated by 2.1GB? sathyguy Linux - Enterprise 1 11-12-2006 10:24 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:12 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration