Perl script to search and extract using wildcards. | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Perl script to search and extract using wildcards.

Shell Programming and Scripting


Tags
extract, perl, wildcard

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 04-17-2009
CammyD CammyD is offline
Registered User
 
Join Date: Apr 2009
Last Activity: 28 April 2009, 7:37 PM EDT
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Perl script to search and extract using wildcards.

Good evening All,

I have a perl script to pull out all occurrences of a files beginning with xx and ending in .p. I will then loop through all 1K files in a directory. I can grep for xx*.p files but it gives me the entire line. I wish to output to a single colum with only the hits found. This script needs to search for more than one occurrence per line regardless of the position or surrounding text.

Desired output:

Code:
xx1.p
xx3.p
xx4.p
asdfxx.p


Code:
#!/usr/bin/perl -w
$test = "xx1.p  xx2.r \n (xx3.p) ( xx4.p ) \n xx5.r xx.p.pxx \n asdfxx.p \n xxixiadsf.xx.p  \n xx4.p {xx5.p}   (  xx6.p)";
while ($test =~ /(xx.*\.p)/g) {
       print "$1\n";
}

Output

Code:
xx1.p
xx3.p) ( xx4.p
xx5.r xx.p.p
xx.p
xxixiadsf.xx.p
xx4.p {xx5.p}   (  xx6.p

Any help would be appreciated.
Thank you,
Cammy

Last edited by Yogesh Sawant; 04-20-2009 at 05:15 AM.. Reason: added code tags
Sponsored Links
    #2  
Old 04-17-2009
KevinADC KevinADC is offline Forum Advisor  
Registered User
 
Join Date: Jan 2008
Last Activity: 11 September 2009, 3:44 PM EDT
Posts: 729
Thanks: 0
Thanked 0 Times in 0 Posts
The biggest problem with your regexp is the greedy match .* but your search criteria is too vague to want to try and post any code. For example, why does asdfxx.p but not xx5.p? Of course your regexp will not match asdfxx.p anyway. Please post some real sample data and explain the search criteria in more detail.
Sponsored Links
    #3  
Old 04-18-2009
Mumford Mumford is offline
Registered User
 
Join Date: Mar 2009
Last Activity: 29 June 2011, 7:42 PM EDT
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Drop boundaries on the regex and replace the .* with a less greedy \S*:

Code:
#!/usr/bin/perl -w

$test = "xx1.p  xx2.r \n (xx3.p) ( xx4.p ) \n xx5.r xx.p.pxx \n asdfxx.p \n xxix
iadsf.xx.p  \n xx4.p {xx5.p}   (  xx6.p)";

while ($test =~ /\b(xx\S*\.p)\b/g) {
       print "$1\n";
}

Output:
xx1.p
xx3.p
xx4.p
xxixiadsf.xx.p
xx4.p
xx5.p
xx6.p

I'm with KevinADC... I'm a bit confused how asdfxx.p is expected to be part of the output when you say "of a files beginning with xx".
    #4  
Old 04-20-2009
CammyD CammyD is offline
Registered User
 
Join Date: Apr 2009
Last Activity: 28 April 2009, 7:37 PM EDT
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Computer Worked like a charm!

Both of you were correct. I inadvertantly left the asdfxx.p file in the "desired output". I indeed, did not want it to find that match.
I made the changes, as Mumford suggested, and it worked exactly as I wanted it to originally. Thank you both for your expertise!
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Perl script for extract data from xml files allways4u21 Shell Programming and Scripting 3 01-15-2009 06:54 AM
Perl script to extract last date field (yyyy/mm/dd) ganapati Shell Programming and Scripting 1 01-09-2009 12:31 PM
Perl Script Syntax to Extract Everything After Special Character edrichard Shell Programming and Scripting 3 10-05-2008 05:31 PM
using wildcards in this perl command hcclnoodles Shell Programming and Scripting 2 07-17-2007 07:38 AM
Help with perl script to search webpage Terrible Shell Programming and Scripting 5 12-01-2006 02:23 PM



All times are GMT -4. The time now is 12:28 PM.