in reply to Re^2: Using grep to pick out Specific lines in a text file
in thread Using grep to pick out Specific lines in a text file

...and here now with reading from and writing to file. I leave the system commands out as I do not know what they are intended for.

use strict; my @patterns = ( 'unknown type\(0x134e\)', '30.6511', '\(V6.5.1 FP1\)' + ); # now read from file instead open FILE, "<", "data/04102013_loginlogs.txt" or die "Cannot open inpu +t file.\n"; my @array = <FILE>; close(FILE); # open output file for writing open OUT, ">>", "output.txt" or die "Cannot open output file.\n"; foreach my $line (@array) { print OUT $line if scalar grep { $line =~ /$_/ } @patterns; } close OUT;

UPDATE: Removed a ) on line 5.

Replies are listed 'Best First'.
Re^4: Using grep to pick out Specific lines in a text file
by itguy87 (Novice) on Apr 11, 2013 at 20:05 UTC
    well i tried what you have above and it tells me there is a syntax error at line 5

      Here is a version that tries to heed davido's advice:

      use strict; use warnings; use lib "/Library/Perl/5.10.0/"; use autodie; my @patterns = ( 'unknown type\(0x134e\)', '30.6511', '\(V6.5.1 FP1\)' + ); open FILE, "<", "data/04102013_loginlogs.txt" or die "Cannot open inpu +t file.\n"; open OUT, ">>", "output.txt" or die "Cannot open output file.\n"; while( my $line=<FILE> ) { print OUT $line if scalar grep { $line =~ /$_/ } @patterns; } close OUT; close FILE;

      One ) too much. Sorry. I will correct it.

Re^4: Using grep to pick out Specific lines in a text file
by itguy87 (Novice) on Apr 11, 2013 at 20:32 UTC

    Ok so this did search this time, My issue is that i need to pull the line that has all of the following true. unknown type(0x134e) 30.6511 (V6.5.1 FP1)

    Here is a line from the data file

    01 XXXXXXXX 10/Apr/13 01:13:10 unknown type(0x134e) 30.6511 (V6.5.1 FP1) Singer 192.168.1.1

      That is even easier. Just one regex.

      use strict; use warnings; use lib "/Library/Perl/5.10.0/"; use autodie; open FILE, "<", "data/04102013_loginlogs.txt" or die "Cannot open inpu +t file.\n"; open OUT, ">>", "output.txt" or die "Cannot open output file.\n"; while( my $line=<FILE> ) { print OUT $line if $line =~ /unknown type\(0x134e\)\s+30\.6511\s+\(V +6\.5\.1 FP1\)/; } close OUT; close FILE;
Re^4: Using grep to pick out Specific lines in a text file
by itguy87 (Novice) on Apr 11, 2013 at 20:48 UTC

    Perfect, that worked great. Now that it is working i found another issue. In this where "singer" is i pulled 23 different occurances, i only need to print singer in my out put. That tells me that it does show up in the data log. And Singer does change to multiple values.

      Think hash

      If you didn't program your executable by toggling in binary, it wasn't really programming!

      This is based on the assumption that after ...30.6511 (V6.5.1 FP1) comes one or more spaces, then a word (that you are looking for), some more spaces, an IP address, possibly some spaces before the end of line. You need to modify the regex should this assumption not hold.

      use strict; use warnings; use lib "/Library/Perl/5.10.0/"; use autodie; open FILE, "<", "data/04102013_loginlogs.txt" or die "Cannot open inpu +t file.\n"; open OUT, ">>", "output.txt" or die "Cannot open output file.\n"; my %found; while( my $line=<FILE> ) { $found{$1}++ if( $line =~ /unknown type\(0x134e\)\s+30\.6511\s ++\(V6\.5\.1 FP1\)\s+(.*)\s+\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\s*$/ ); } print join( "|", keys %found), "\n"; close OUT; close FILE;

      I have used this test file:

      01 XXXXXXXX 10/Apr/13 01:13:10 unknown type(0x134e) 30.6511 (V6.5.1 FP +1) Singer 192.168.1.1 01 XXXXXXXX 10/Apr/13 01:13:10 unknown type(0x134e) 30.6511 (V6.5.1 FP +1) Singer 192.168.1.1 01 XXXXXXXX 10/Apr/13 01:13:10 unknown type(0x134e) 30.6511 (V6.5.1 FP +1) Runner 192.168.1.10 ddddd 01 XXXXXXXX 10/Apr/13 01:13:10 unknown type(0x134e) 30.6511 (V6.5.1 FP +1) Singer 192.168.1.1 01 XXXXXXXX 10/Apr/13 01:13:10 unknown type(0x134e) 30.6511 (V6.5.1 FP +1) Dancer 192.168.1.1 ddddd ffffff 01 XXXXXXXX 10/Apr/13 01:13:10 unknown type(0x134e) 30.6511 (V6.5.1 FP +1) Singer 192.168.1.1 01 XXXXXXXX 10/Apr/13 01:13:10 unknown type(0x134e) 30.6511 (V6.5.1 FP +1) Singer 192.168.1.1 01 XXXXXXXX 10/Apr/13 01:13:10 unknown type(0x134e) 30.6511 (V6.5.1 FP +1) Singer 192.168.1.1
Re^4: Using grep to pick out Specific lines in a text file
by itguy87 (Novice) on Apr 17, 2013 at 17:50 UTC

    Works great but now i think i want it to look different... woulld look like the blow

    30.6511 (V6.5.1 FP1) KALLATOM@de.ibm.com 255.255.255.255 (then a count of how may timesit showed up)

      Guess what! That's what $found{$1}++ is for. Just print not only the keys but also the values of hash %found.

        That is what i thought but i was working with it and cuold not make it produce what i wanted