CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Problem in regular expression!!

 



gevni
Novice

May 21, 2013, 4:35 PM

Post #1 of 14 (460 views)
Problem in regular expression!! Can't Post

Hi i have csv file and i want to print the lines that have .java string in first column. I try to make some regular expression but it shows me some error. Can you plz check what I am doing wrong with regular expression.

Thanks
My csv file is

Code
AST.java,1,1,1,1,7,0,6,0,1,7,7,134 
AST.swt,1,1,1,1,7,0,9,0,0,7,7,138
AST.java,1,1,1,1,7,0,6,0,1,7,6,174
AST.swt,1,1,1,1,7,0,6,0,1,7,5,123


My code is

Code
#!/usr/bin/perl 

use warnings;
use strict;

open (EXEC, "+<my_file.csv")or die("Could not open file.");
open (FILE, ">>new.csv")or die("Could not open file.");
while(<EXEC>)
{
chomp;
if(/(\S+)\.\(\java\)\,\(\d+)\,\(\d+)\,\(\d+)\,\(\d+)\,\(\d+)\,\(\d+)\,\(\d+)\,\(\d+)\,\(\d+)\,\(\d+)\,\(\d+)\,\(\d+)/)
{
printf FILE "$_";

}
}

close(EXEC);
close(FILE);
exit;



recruiter
User

May 21, 2013, 4:54 PM

Post #2 of 14 (453 views)
Re: [gevni] Problem in regular expression!! [In reply to] Can't Post

One way you can do this:


Code
   /\s*.java.*/



(This post was edited by hwnd on May 21, 2013, 5:03 PM)


BillKSmith
Veteran

May 21, 2013, 7:35 PM

Post #3 of 14 (442 views)
Re: [gevni] Problem in regular expression!! [In reply to] Can't Post

Or use a one-liner.

Code
perl -pe"$_='' unless /^[^,]java/" my_file.csv >new.csv



Corrected regexp. Should be:


Code
perl -pe"$_='' unless /^[^,]*java/" my_file.csv >new.csv


This is the windows version (double quotes around the code. Two single quotes after the equal sign). Reverse single and double quotes on UNIX.

Note: The regexp matches java before the first comma.
Good Luck,
Bill

(This post was edited by BillKSmith on May 22, 2013, 9:51 AM)


gevni
Novice

May 22, 2013, 4:41 AM

Post #4 of 14 (436 views)
Re: [hwnd] Problem in regular expression!! [In reply to] Can't Post

Hi @hwnd it give me the same file with all lines as an output. I want only these lines that have .java string. like that

Code
AST.java,1,1,1,1,7,0,6,0,1,7,7,134  
AST.java,1,1,1,1,7,0,6,0,1,7,6,174



(This post was edited by gevni on May 22, 2013, 4:44 AM)


gevni
Novice

May 22, 2013, 4:43 AM

Post #5 of 14 (435 views)
Re: [BillKSmith] Problem in regular expression!! [In reply to] Can't Post

 
I have mac, i try this liner command but it won't execute. Might be I am trying wrong quotes order. Can you plz specify it for mac?

Code



      
    


BillKSmith
Veteran

May 22, 2013, 5:22 AM

Post #6 of 14 (429 views)
Re: [gevni] Problem in regular expression!! [In reply to] Can't Post

I do not know anything about mac OS. I can explain what I am trying to do.

The quotes after the '=' are part of the perl program. The intention is to return a null string. For this purpose, it makes no difference whether you use single or double quotes. You could even use q() or qq() instead. As far as your OS is concerned, -pe"...." is a command-line argument for the perl interpreter. Again, perl will accept either kind of quotes. You have to find out how to write an argument that contains quotes.
Good Luck,
Bill


gevni
Novice

May 22, 2013, 5:58 AM

Post #7 of 14 (423 views)
Re: [BillKSmith] Problem in regular expression!! [In reply to] Can't Post

I change the quote order then it works. But it generate the empty file. I think same regular expression issue.


BillKSmith
Veteran

May 22, 2013, 9:57 AM

Post #8 of 14 (399 views)
Re: [gevni] Problem in regular expression!! [In reply to] Can't Post

Yes it is the regexp. I made a typo in my previous post. Note the correction that I have already made. (I omitted the '*' needed to match multiple non-comma characters). Sorry about that.Blush
Good Luck,
Bill


Laurent_R
Enthusiast / Moderator

May 22, 2013, 10:40 AM

Post #9 of 14 (398 views)
Re: [hwnd] Problem in regular expression!! [In reply to] Can't Post


In Reply To
One way you can do this:


Code
   /\s*.java.*/



This won't work. Perhaps you meant something like this:


Code
/\w\.java/



FishMonger
Veteran / Moderator

May 22, 2013, 11:03 AM

Post #10 of 14 (397 views)
Re: [gevni] Problem in regular expression!! [In reply to] Can't Post

gevni,
please post beginner questions like this in the Beginner section.


Code
#!/usr/bin/perl	 

use strict;
use warnings;

open my $exec_fh, '<', 'my_file.csv' or die("Could not open 'my_file.csv' $!");
open my $out_fh, '>>', 'new.csv' or die "Could not open 'new.csv' $!";

print $out_fh grep { /^[^.]+\.java/ } <$exec_fh>;



(This post was edited by FishMonger on May 22, 2013, 11:14 AM)


FishMonger
Veteran / Moderator

May 22, 2013, 11:10 AM

Post #11 of 14 (396 views)
Re: [gevni] Problem in regular expression!! [In reply to] Can't Post

Here's another option.

Code
#!/usr/bin/perl 

use strict;
use warnings;

#open my $exec_fh, '<', 'my_file.csv' or die("Could not open 'my_file.csv' $!");
#open my $out_fh, '>>', 'new.csv' or die "Could not open 'new.csv' $!";

#print $out_fh grep { /^[^.]+\.java/ } <$exec_fh>;

while ( <DATA> ) {
print if (split /,/, $_)[0] =~ /java/;
}

__DATA__
AST.java,1,1,1,1,7,0,6,0,1,7,7,134
AST.swt,1,1,1,1,7,0,9,0,0,7,7,138
AST.java,1,1,1,1,7,0,6,0,1,7,6,174
AST.swt,1,1,1,1,7,0,6,0,1,7,5,123



FishMonger
Veteran / Moderator

May 22, 2013, 11:24 AM

Post #12 of 14 (394 views)
Re: [BillKSmith] Problem in regular expression!! [In reply to] Can't Post


In Reply To
Or use a one-liner.

Code
perl -pe"$_='' unless /^[^,]java/" my_file.csv >new.csv



Corrected regexp. Should be:


Code
perl -pe"$_='' unless /^[^,]*java/" my_file.csv >new.csv


This is the windows version (double quotes around the code. Two single quotes after the equal sign). Reverse single and double quotes on UNIX.

Note: The regexp matches java before the first comma.


Wouldn't it be better/cleaner/shorter to use -n instead of -p?


Code
perl -ne "print if /^[^,]*java/" my_file.csv > new.csv



BillKSmith
Veteran

May 22, 2013, 11:46 AM

Post #13 of 14 (390 views)
Re: [FishMonger] Problem in regular expression!! [In reply to] Can't Post

You are right, considering the wording of the original question. In general, I do not see any difference.
Good Luck,
Bill


Laurent_R
Enthusiast / Moderator

May 22, 2013, 12:01 PM

Post #14 of 14 (388 views)
Re: [FishMonger] Problem in regular expression!! [In reply to] Can't Post

I also think it would be much clearer.

EDIT: I did not see Bill's answer when I posted mine.


(This post was edited by Laurent_R on May 22, 2013, 12:02 PM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives