Script Idea's / Help

Hi there, I'm pretty new to scripting and wondering if anyone had any idea's, scripts or snippets on how I can do the following.

I basically want a shell script that will look at all the files in a directory and find all the names and addresses in them then output them to the screen nicely formatted. Any help would be greatly appreciated...

ta

Please be more accurate. What do you mean by "names and addresses"? It would help if you give an example. Maybe awk will do your work.

Include the first 5-6 lines of information.
And, include what you have in mind for output.

Did you ever solve this?

if not, what kind of files are in the directory? are they all the same type and format? do the fields you want have a common formating or labeling?

Thanks, all solved now, however I want to do the same thing but generate a list of IP addresses that are in many different files within a directory using AWK, can you search for an IP string pattern?

Cheers

What about sharing your solution ?

mrpugster, I'm more like a perl person, so here's a solution using perl:

This will print all lines with IPs.

perl -ne 'print if /((?:\d{1,3}\.){3}\d{1,3})/;' ip-file

This will just print the ips.

perl -ne 'print "$1\n" if /((?:\d{1,3}\.){3}\d{1,3})/;' ip-file

PS: you must change "ip-file" to your file's name.
PPS: will only work for ipv4

thanks red, i'm beginning to see how powerful perl can be, never used it before. if i wanted to execute commands, such as ping each of those IP's, I can just create a simple 'for' loop:

for $1 do;
ping $1

something like that i'm guessing, not entirely sure of the syntax, maybe after that output it into nice columns such as 'Address' and 'Ping Result' will have a go. thx

In this case you don't have to use for. If you wish to ping, just:

perl -ne 'system "your-command $1" if /((?:\d{1,3}\.){3}\d{1,3})/;' ip-file

Now, it entirely depends on the actual content of your data file (ip-file). I created a pattern to match ip addresses /((?:\d{1,3}\.){3}\d{1,3})/ but if you have some data after the IP you could replace that pattern with /((?:\d{1,3}\.){3}\d{1,3})\s*(.*?)$/ so you have:

perl -ne 'print "$1\t$2\n" if /((?:\d{1,3}\.){3}\d{1,3})\s*(.*?)$/;' ip-file

that's pretty neat, i'm looking at doing a nslookup on those ip's too, that's easy to do but obviously generates lots of output, can I apply a pattern match once more to the results to just give me the IP and domain?

Can you mix system commands with the print command on just a single command line with perl?

All interesting stuff. I think i'm going to have to sit down and read up on perl pattern matching.

many thx

No need for external programs. Perl does the look-up for you:

 perl -ne 'use Socket; print "".(gethostbyaddr(inet_aton($1), AF_INET) or "Unknown ip bla bla")."\n" if /((?:\d{1,3}\.){3}\d{1,3})/;' ip-file

But, will it blend?

(couldn't resist)

People have a problem and think "I'll user Perl to solve this".
Now they have two problems... :wink:

Sorry, open door.
(Perl and Logwatch have a special place in my heart)