grep problem in perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting grep problem in perl
# 1  
Old 10-15-2008
CPU & Memory grep problem in perl

Hi,

I'm writing one perl script to select only a number part from a line. These lines looks as:

raj born on 20080201 at delhi
result for the equation [(A+B) * (B*C)] is (886005)
peter get married on 20031230
Maria born on 20080201 at paris

my output file should only contains the numbers, by removing the duplicate.
i.e, for the above line, my output file should contains only below lines.

20080201
886005
20031230

I've writen the code as below, seem to be some problem in 'grep' part.

#!/usr/bin/perl

use File::Find;
use strict;

my $in_file = "/software/app//input_file.txt";
my $out_file = "/software/app//output_file.txt";

my @outLines;
my $line;
my $cur_num;

open (FILE, $in_file ) or die "Cannot open file: $!";
open( OUTFILE, "> " . $out_filename ) or die "Cannot open file: $!";

while ( $line = <FILE> ) {
$cur_num =~ grep( /[0-9]/ , $line);
push(@outLines, $cur_num);
}

print ( OUTFILE @outLines );

close FILE;
close ( OUTFILE );
undef ( @outLines );

Any help would be much appreciated.

Much Regards / Lokesha
# 2  
Old 10-15-2008
Bug

Thanks,

I've got answer from my friend, it is as below:

#!/usr/bin/perl

use File::Find;
use strict;

my $directory = "/software/app/AlgoSuite/glk/FILES";
#my $directory = "/w94236_app/u01/test/Data/RiskWatch/Handler/OUTPUT/Current";
my $out_filename = "/software/app/AlgoSuite/glk/output_file.txt";

find (\&process, $directory);

sub process
{
my @outLines; #Data we are going to output
my $line; #Data we are reading line by line
my $cur_num;

# Only parse files that end in .html
if ( $File::Find::name =~ /\ErrorTrades.info$/ ) {
open( FILE, $File::Find::name ) or die "Cannot open file: $!";
open( OUTFILE, "> " . $out_filename ) or die "Cannot open file: $!";

while ( $line = <FILE> ) {
if ($line =~ m/^Check the LEGAL/){
$line =~ s/\D*//;
$line =~ s/\D*$//;
push(@outLines, $line . "\n");
}
}

print( OUTFILE sort @outLines );

close( FILE );
close( OUTFILE );
undef( @outLines );
}
}

Thanks for the forum again...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

sed Or Grep Problem OR Terminal Problem?

I don't know if you guys get this problem sometimes at Terminal but I had been having this problem since yesterday :( Maybe I overdid the Terminal. Even the codes that used to work doesn't work anymore. Here is what 's happening: * I wanted to remove lines containing digits so I used this... (25 Replies)
Discussion started by: Nexeu
25 Replies

2. Shell Programming and Scripting

How to grep a pattern in perl?

hello Everyone i am a newbie. i have a file which contains the following E:\gtmproj\script\i486_nt\obj\check_geomtools.exe: o:\portsrc\spg\system_1\i486_nt\advapps\TK-2\objmt\winclockmtq.lib E:\gtmproj\script\i486_nt\obj\check_geomtools.exe:... (12 Replies)
Discussion started by: Rashid Khan
12 Replies

3. Shell Programming and Scripting

Grep in PERL

Hi, Can anybody let me know how this grep will work. The input and output is not known. Also can you give me the details of any link where i can find clearly about grep Thanks in advance (1 Reply)
Discussion started by: irudayaraj
1 Replies

4. Web Development

problem with exporting vairable from one perl cgi to another perl cgi script while redirecting.

Can anyone tell me how to export a variable from one perl CGI script to another perl cgi script when using a redirect. Upon running the login.pl the user is prompted to enter user name and password. Upon entering the correct credentials (admin/admin) the user is redirected to welcome page. My... (3 Replies)
Discussion started by: Arun_Linux
3 Replies

5. Shell Programming and Scripting

Perl + and Grep

Hi All i have this script that uses glob to look in /var/log/messages.* my @messagefiles = glob "/var/log/messages.*"; and the code that uses it is this grep { /NVRM: Xid/ } @messages) but this spits out this /var/log/messages-20111030:Oct 25 13:43:04 brent kernel: NVRM:... (10 Replies)
Discussion started by: ab52
10 Replies

6. Shell Programming and Scripting

grep -v equivalent in perl

I have to do grep -v in a perl script. I want to exclude blank lines and lines having visitor. #grep -v visitor abc.txt |grep '.' file:abc.txt 1340 not booked 16D:D9 tourist 8 1341 not booked 16C:D4 tourist 25 1342 not booked 16D:C4 visitor 7 1343 not booked 01C:D9 visitor 6 1344... (4 Replies)
Discussion started by: dynamax
4 Replies

7. Shell Programming and Scripting

Grep and map in perl

Hi guys, I'm trying to learn grep and map and having a little problem. Let's say I have a file which contains: Apple: abcdcabdadddbac I want to replace any combinations of three of abcd, thus when I do this: print grep {s/{3}/X/g} <F>; # will do the subtitution fine, output XXXX ... (1 Reply)
Discussion started by: new bie
1 Replies

8. Shell Programming and Scripting

grep in perl

Hello I want to grep a line from a file saved in some directory. Can anyone please correct the code below: #!/usr/bin/perl -w $file = "/home/output.txt" $grep_line = "closing zip for topic"; `grep $grep_line* $file`; (1 Reply)
Discussion started by: sureshcisco
1 Replies

9. Shell Programming and Scripting

Perl grep

OK here's the situation: I have got these lines which I have got to parse. If the line contains a particular string and any element from a previously defined array I need to take that particular line and do some further processing. if ((grep(/$_/,$1)) && (grep($pattern,@myarr))) { #Do... (2 Replies)
Discussion started by: King Nothing
2 Replies

10. Shell Programming and Scripting

grep using Perl

I'm using perl to do a grep of each line in a vendor file and find its occurrences in a specific directory. Any values found is saved in @dir. .....(file opened, etc.) .... while ($line=<FILE>){ @dir = `grep $line * `; } It's the specific usage of the system grep that I'm having... (7 Replies)
Discussion started by: gavineq
7 Replies
Login or Register to Ask a Question