Beefy Boxes and Bandwidth Generously Provided by pair Networks RobOMonk
laziness, impatience, and hubris
 
PerlMonks  

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

by Kiran Kumar K V N (Initiate)
on Jan 05, 2007 at 12:30 UTC ( [id://593170]=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^8: Sorting names using Regular Expressions and placing them in different Files.
in thread Sorting names using Regular Expressions and placing them in different Files.

#!/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 $!; ## Search each Line in the Input File. for my $searchline(<INPUT>) { ## Search for parameter 'BSC-'. if( $searchline =~ /BSC-/) { ## Read a line,copy it,& delete 'BSC-' in copy. chomp; my $copy = $_; $copy =~ s/BSC-//; ## Print the original line and the copy. print "$_ $copy\n"; ## Write to the Output File 'BSC'. my $line = 'BSC'; print BSC $searchline; } ## Search for parameter 'SBSCSubsystem-'. if( $searchline =~ /SBSCSubsystem-/) { ## Read a line,copy it,& delete 'SBSCSubsystem-' in + copy. chomp; my $copy = $_; $copy =~ s/SBSCSubsystem-//; ## Print the original line and the copy. print "$_ $copy\n"; ## Write to the Output File 'SBSCSubsystem'. my $line = 'SBSCSubsystem'; print SBSCSubsystem $searchline; } ## Search for parameter 'MCBTSSubsystem-'. if( $searchline =~ /MCBTSSubsystem-/) { ## Read a line,copy it,& delete 'MCBTSSubsystem-' in c +opy. chomp; my $copy = $_; $copy =~ s/MCBTSSubsystem-//; ## Print the original line and the copy. print "$_ $copy\n"; ## Write to the Output File 'MCBTSSubsystem'. my $line = 'MCBTSSubsystem'; print MCBTSSubsystem $searchline; } } ## 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 $!;
  • Comment on Re^9: Sorting names using Regular Expressions and placing them in different Files.
  • Download Code

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

    The above is giving the following compile Errors :- 1) Use of uninitialized value in scalar chomp at Data1.pl line 62, <INPUT> line 5310. 2) Use of uninitialized value in substitution (s///) at Data1.pl line 64, <INPUT> line 5310. 3) Use of uninitialized value in concatenation (.) or string at Data1.pl line 67, <INPUT> line 5310. Please help me in resolving this to get correct O/P.

      Line 62: use chomp $searchline instead of just chomp.
      Line 64: use $searchline instead of $_ on the previous line.
      Line 67: use $searchline instead of $_.

      ---
      It's all fine and dandy until someone has to look at the code.
Re^10: Sorting names using Regular Expressions and placing them in different Files.
by talexb (Chancellor) on Jan 05, 2007 at 13:32 UTC

    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.

      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

        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://593170]
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.