Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Locked thread
Twlight
Feb 18, 2005

I brag about getting free drinks from my boss to make myself feel superior
Fun Shoe
I have a large file that has strings that I would like to extract into a new file, I know I have to use regex but I'm unsure as to How I should format it. I want to save all words that have WS-xxxxx where xxxx is a name everything else in the file I don't care about. Any Ideas?

Adbot
ADBOT LOVES YOU

Twlight
Feb 18, 2005

I brag about getting free drinks from my boss to make myself feel superior
Fun Shoe

Triple Tech posted:

Well, what you're saving in the ideal Perl world would probably become an array. How that is serialized is completely up to you and shouldn't matter. You could freeze/thaw the entire structure (overkill). The simplest way to go about it would be to have each item be a line in a text file.

There's also YAML, tie'ing that array/file...

This is what I have so far, it cut down a lot of the txt that I didn't need. now each line has this format
code:
ip adress<tab>ws-username(this is the info i want)<tab>unused info

here is my code

open(FILE,"pso.txt");
@arr = <FILE>;

open(NEWFILE,">newfile.txt");
foreach $line(@arr)
{
	if($line =~ m/.ws\-/i)
		{
			print NEWFILE $line;
    }

Twlight
Feb 18, 2005

I brag about getting free drinks from my boss to make myself feel superior
Fun Shoe
hey everyone.

I have a template file that I want to copy, however the new file name will be supplied by the user. How can I get File::Copy to work with me? here is my code

code:
copy ("/location/db.emn-template","/location/<filename supplied by user>");
thanks!

Twlight
Feb 18, 2005

I brag about getting free drinks from my boss to make myself feel superior
Fun Shoe

Triple Tech posted:

I don't understand, is something not working for you? Do you not know how to capture user input? Is it not running?!

oh I wasnt sure that it would take a file name as input for the copy command! sorry!

Twlight
Feb 18, 2005

I brag about getting free drinks from my boss to make myself feel superior
Fun Shoe
One more question. I'm writing a perl script to edit the named.conf file, however i need to edit 2 places in the file once in the middle of the file and again at the end (not exactly the end but near the end. How can I tell perl where exactly I want to edit based on this file?

here is hopefully a better explanation:

code:
  };
        zone "zone" in {
                type master;
                file "zone";
        };
<here i would like to add another zone file>
};
How is this done best?

Twlight
Feb 18, 2005

I brag about getting free drinks from my boss to make myself feel superior
Fun Shoe

Triple Tech posted:

All things in software are possible... :pseudo:

If you can promise that everything is split up by lines nicely, you can just loop through each line in the file, analyze where you want to stop, inject your new stuff, and then keep printing.

A better, "more right", more complicated way would be to have a script interpret and parse the entire conf file, modify the Perl object that represents it, and then serialize it again... That would be the most flawless way.

the file follows this convention:
code:
   zone "zone" in {
                type master;
                file "zone";
        }; 
   zone "zone" in {
                type master;
                file "zone";
        }; 
}; <--- this is the end of the section I want to edit
and so on and so forth.
I want to inject a return at the second to last }; and add my input so I would dump the contents of the file into an array, then I get lost :(

Twlight
Feb 18, 2005

I brag about getting free drinks from my boss to make myself feel superior
Fun Shoe

Triple Tech posted:

Right, so you load up the original conf file and you loop through all of the lines. You analyze a line and decide what to do with it. If do nothing, then just tack it back on to a buffer (another scalar). You're essentially reconstructing the file.

At some point, your testing mechanism will pass, telling you that you're at that particular point in the file. Before you push the original content back to the scalar buffer, you inject your content by pushing it on first. Then push the original.

Now you have a new file represented in Perl-space. Close the file for reading, then reopen it for writing, write out your buffer, and blamo, original file + injection complete.

so something like this?
code:
while(FILE){
$line = $_;

if($line =~m/ the location where im looking to add text/){
print OUTFILE (beast of a print line)
else{
print OUTFILE $line
}}

Twlight
Feb 18, 2005

I brag about getting free drinks from my boss to make myself feel superior
Fun Shoe

wntd posted:

The Perl way is to have someone else do the heavy lifting for you.

I've only just started using perl, I know thats from CPAN and I know how to install things from CPAN, but I have no idea how this works.

Adbot
ADBOT LOVES YOU

Twlight
Feb 18, 2005

I brag about getting free drinks from my boss to make myself feel superior
Fun Shoe
I figured it out :)

What I did was add a comment to the conf file I was trying to add data to, then I did a find/replace on that comment. The replacement would also re-add the comment below the new information making it ready for the next entry.

  • Locked thread