Beefy Boxes and Bandwidth Generously Provided by pair Networks vroom
No such thing as a small change
 
PerlMonks  

Re^10: Sorting names using Regular Expressions and placing them in different Files.

by talexb (Chancellor)
on Jan 05, 2007 at 13:32 UTC ( [id://593190]=note: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.


in reply to Re^9: Sorting names using Regular Expressions and placing them in different Files.
in thread Sorting names using Regular Expressions and placing them in different Files.

Here's how I would re-write this code:

#!/usr/bin/perl -w use strict; # Filter specific strings from an input file into specific files. { # Open input and output files. open ( INPUT, '<', 'html_LogFiles' ) or die "Unable to open input file: $!"; open ( BSC, '>', 'BSC' ) or die "Unable to open BSC: $!"; # Update: Sorry, got the names of these two wrong. # That's why copy and paste is bad, bad, bad. open ( SBSCSubsystem, '>', 'SBSCSubsystem' ) or die "Unable to open SBSCSubsystem: $!"; open ( MCBTSSubsystem, '>', 'MCBTSSubsystem' ) or die "Unable to open MCBTSSubsystem: $!"; # Process input lines into output files. Note that an # input line may match more than one pattern and # therefore may appear in more than one output file. while(<INPUT>) { if ( /BSC-/ ) { chomp; my $copy = $_; $copy =~ s/BSC-//; print BSC "$_ $copy\n"; } if ( /SBSCSubsystem/ ) { chomp; my $copy = $_; $copy =~ s/SBSCSubsystem//; print SBSCSubsystem "$_ $copy\n"; } if ( /MCBTSSubsystem/ ) { chomp; my $copy = $_; $copy =~ s/MCBTSSubsystem//; print MCBTSSubsystem "$_ $copy\n"; } } # Close input and output files. close ( INPUT ); close ( BSC ); close ( SBSCSubsystem ); close ( MCBTSSubsystem );

One comment at the top of the file, and three more comments after that. Your coding style adds too many comments, which means there's too much to read -- keep it simple.

If I had more time, I'd recommend a hash-based data structure to define the string you're looking for. That way, adding more filters would be a matter of adding an entry to a table, not cutting and pasting a block of code.

Alex / talexb / Toronto

"Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds

Updates 1406, January 5: Sorry, bungled file names of last two output files. Fixed now.

  • Comment on Re^10: Sorting names using Regular Expressions and placing them in different Files.
  • Download Code

Replies are listed 'Best First'.
Re^11: Sorting names using Regular Expressions and placing them in different Files.
by Kiran Kumar K V N (Initiate) on Jan 09, 2007 at 08:29 UTC

    Hi All,

    #!/usr/bin/perl -w use strict; use warnings; ## Open I/P File "bsm1_LogFiles" for reading. open(INPUT,'<','bsm1_LogFiles') or die $!; ## Read from the Input File. my $line = <INPUT>; ## Open the O/P File "BSC" for writing. open(BSC,'>','BSC') or die $!; ## Open the O/P File "SBSCSubsystem" for writing. open(SBSCSubsystem,'>','SBSCSubsystem') or die $!; ## Open the O/P File "MCBTSSubsystem" for writing. open(MCBTSSubsystem,'>','MCBTSSubsystem') or die $!; ## Process input lines into output files. while(<INPUT>) { ## For parameter 'BSC-'. if ( /BSC-/ ) { ## Read a line, copy it, and delete 'BSC-' in the copy. chomp $line; my $copy = $_; $copy =~ s/BSC-//; ## Print the original line and the copy. print BSC "$_ $copy\n"; } ## For parameter 'SBSCSubsystem-'. if ( /SBSCSubsystem-/ ) { ## Read a line, copy it, and delete 'SBSCSubsystem-' in the cop +y. chomp $line; my $copy = $_; $copy =~ s/SBSCSubsystem-//; ## Print the original line and the copy. print SBSCSubsystem "$_ $copy\n"; } ## For parameter 'MCBTSSubsystem-'. if ( /MCBTSSubsystem-/ ) { ## Read a line, copy it, and delete 'MCBTSSubsystem-' in the co +py. chomp $line; my $copy = $_; $copy =~ s/MCBTSSubsystem-//; ## Print the original line and the copy. print MCBTSSubsystem "$_ $copy\n"; } } ## Close the I/P File "bsm1_LogFiles"; close (INPUT) or die $!; ## Close the O/P File "BSC"; close (BSC) or die $!; ## Close the O/P File "SBSCSubsystem"; close (SBSCSubsystem) or die $!; ## Close the O/P File "MCBTSSubsystem"; close (MCBTSSubsystem) or die $!;

    1) The above code gives the one of the Output to the File BSC as :-

    -rw-rw-rw- 1 bsmbin bsmbin 303 Dec 3 02:06 BSC-20041202143000

    -rw-rw-rw- 1 bsmbin bsmbin 303 Dec 3 02:06 20041202143000

    -rw-rw-rw- 1 bsmbin bsmbin 303 Dec 3 02:06 BSC-20041202140000

    -rw-rw-rw- 1 bsmbin bsmbin 303 Dec 3 02:06 20041202140000

    2) What if, I need to get the output to the File BSC as just:-

    BSC-20041202143000 20041202143000

    BSC-20041202140000 20041202140000

    Similar Output for other 2 Files SBSCSubsystem and MCBTSSubsystem. Please let me know.

      What have you tried? Can you make a guess?

      Alex / talexb / Toronto

      "Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds

        Hi Alex,

        I am trying to print only each Filename with the concatenated one, rather than the complete details of each File

        i.e. to just print :-

        BSC-20041202143000

        20041202143000

        instead of

        -rw-rw-rw- 1 bsmbin bsmbin 303 Dec 3 02:06 BSC-20041202143000

        -rw-rw-rw- 1 bsmbin bsmbin 303 Dec 3 02:06 20041202143000

        Please let me know your views...

      Kiran,

      Here's what you want ..

      #!/usr/bin/perl -w use strict; # Filter specific strings from an input file into specific # files. { # Open input and output files. open ( INPUT, '<', 'html_LogFiles' ) or die "Unable to open input file: $!"; open ( BSC, '>', 'BSC' ) or die "Unable to open BSC: $!"; open ( SBSCSubsystem, '>', 'BSC' ) or die "Unable to open SBSCSubsystem: $!"; open ( MCBTSSubsystem, '>', 'BSC' ) or die "Unable to open MCBTSSubsystem: $!"; # Process input lines into output files. Note that # an input line may match more than one pattern and # therefore may appear in more than one output # file. while(<INPUT>) { # This breaks the incoming line into the part before # the pattern, the pattern itself, and after the # pattern. We then output the last two parts, # followed by the last part. This effectively # truncates the first part. if ( /^(.+)(BSC-)(.+)$/ ) { print BSC "$2$3 $3\n"; } if ( /^(.+)(SBSCSubsystem-)(.+)$/ ) { print SBSCSubsystem "$2$3 $3\n"; } if ( /^(.+)(MCBTSSubsystem-)(.+)$/ ) { print MCBTSSubsystem "$2$3 $3\n"; } } # Close input and output files. close ( INPUT ); close ( BSC ); close ( SBSCSubsystem ); close ( MCBTSSubsystem ); }

      Alex / talexb / Toronto

      "Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://593190]
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.