Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading found information from a file 1

Status
Not open for further replies.

inforeqd

Technical User
Jan 8, 2001
95
0
0
US
I have created a very nice data collection script that
does numerous things. One certain aspect of the script
is that it finds the location of specific files on a *nix
box. For example, I do the simple *nix find command
for all aliases files on the machine then push the
output to a file called system_aliases. The output looks
like this

194610 4 -rw-r--r-- 1 root root 1048 Mar 3 2001 /etc/aliases

Naturally, the output would be deliminated by the width
of the browser your using.

The next step which I am unsure of how to do is this.

I want to take the contents of the files that were created. Read in the location of each file

So,

open (IN, $file_with_information);
open (OUT, ">./file_output_to");
while (<IN>)
{ $buffer .= $_; } # read the entire file into $buffer

Heres where i lose it. How do i choose just the /etc/aliases file specifically when I have an output as
depicted earlier??

Then once I've been able to read in just that file how
do I manipulate it in a manner that basically lets me
cat the output to an outfile. Oh, and by the way I have
numerous files that I will run this same routine on so how
do I make just one out file with all the data in one place?

Just so everyone knows. I have looked around in alot
of books but nothing jumps out. I hope that this group
could add some light and help.

TIA
Infor

 
i didn't fully understand what you want to do,but maybe this will help. you should use regular expressions.
Code:
#!/usr/bin/perl

#i will create a variable to hold the string we're searching for
#so it will be more dynamic.

my $string_to_search = &quot;/etc/aliases&quot;;

open (IN, $file_with_information);
open (OUT, &quot;>./$file_output_to&quot;);
 while (<IN>) { # read the file line by line
	if (/\b$string_to_search\b/) { #if line cotains $string_to_search
		print OUT $_; #print the found line into OUT
	}
}
close(OUT);
close(IN);
hope this helps :) (-:
 
Just as an aside. If you want to read a whole file in real quick you should consider undefining the line separator thus.

{ #brackets limit the scope of local
local $/ = undef;
$buffer = <IN>; #the whole will now be read here
}

paulf
 
paul and stak,

thanks for the information. Let me try to clarify a little
better. i see where you guys are going and I think
that the code snippet stak gave will work great for the search string that will be performed at a later date. So you gave me a gold nugget in that department.

With that said heres hopefully a little better
understanding of what im getting at. Sorry for all
the confusion.

I do a find for all aliases files on the system and get a return value like this put into a file.

194610 4 -rw-r--r-- 1 root root 1048 Mar 3 2001 /etc/aliases

and

49150 4 -rw-r--r-- 1 root root 710 Jun 3 1999 /usr/share/texmf/aliases

But I still have to do a manual review to find what im looking for. Staks code comes in here I think.

Note that the location of the aliases files is different and I am unsure of what the find command will result in. So, what I was thinking of doing was to use perl to read in the field that that is the location of the aliases files one at a time (i.e. /etc/aliases and /usr/share/texmf/aliases .. treat the last entry in that field like a file, as it truly is).

I then just wanted to do a quick open of the file (aliases files here) and push the output to a centrally located file that will contain all this information or with the snippent presented by stak go ahead and do the analysis for the string that I'm looking for and either push a common output statement like (the file contains xxx or the file doesnt contain xxx) to a file. Does that make anymore sense?? I guess part of the problem in doing this is I cant figure out how to explain it ;)

The more i write about it the more clear things get for me at least, now if i just could code whats in my mind.

Thanks again to everyone hopefully I can get some more answers.

TIA

Info
 
take a look at this chunk of code. see if it helps you. it takes out on;y the last part from the line, if it is something like /var/etc
i hope it helps you. let me know if there's anything else i can help you with. sorry i can't really understand what you want to do :)

Code:
$lines[0] = &quot;194610 4 -rw-r--r-- 1 root  root   1048 Mar  3  2001 /etc/aliases&quot;;
$lines[1] = &quot;49150 4 -rw-r--r-- 1 root  root   710 Jun  3  1999 /usr/share/texmf/aliases&quot;;
$lines[2] = &quot;49150  4 -rw-r--r-- 1 root   root  710 Jun  3  1999 /cookie/mama&quot;;
$lines[3] = &quot;49150  4 -rw-r--r--   1 root   root   710 Jun  3 1999 /lineyop&quot;;
$lines[4] = &quot;49150  4 -rw-r--r--   1 root  root   710 Jun  3  1999 /cookie/mama/aliases&quot;;

foreach (@lines) {
	if (/^[^\/]*(.*)$/) {
                $file_name = $1;
		print $file_name,&quot;\n&quot;;
	}
}
(-:
 
stak,

beautiful .. it worked just like it should and I only had to
make a quick mod for it to work with what I had earlier.

Thanks .. now I'll add that little snippet earlier and all should be well .. if not .. I'll be back.


Thanks again .. appreciated
INFO
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top