Sponsored Content
Top Forums Shell Programming and Scripting Perl script to search and extract using wildcards. Post 302308454 by Mumford on Saturday 18th of April 2009 11:32:23 AM
Old 04-18-2009
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".
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

using wildcards in this perl command

Hi there, is it possible to use wild cards in this statement ssh $remote_server 'perl -pi -e "s,EXP_SERIAL_19b8be67=\"\",EXP_SERIAL_`hostid`=\"UNKNOWN\"," /var/myfile' This command works fine but the bit in bold (the 8 character hostid) will not always be 19b8be67 so I was hoping I could... (2 Replies)
Discussion started by: hcclnoodles
2 Replies

2. Shell Programming and Scripting

Perl Script Syntax to Extract Everything After Special Character

Hi, I am writing a Perl script that reads in many lines, if a line meets the criteria I want to edit, it. For example, the script will return the following example line... test=abc123 All I want to do is strip off the "test=" and just be left with the abc123. In my script I can easily... (3 Replies)
Discussion started by: edrichard
3 Replies

3. Shell Programming and Scripting

Perl script to extract 'ID' From XML File

File1.xml <?xml version.........> - <abcd:abcd_list version="1" www.john_uncle's_server.com" xmlns: - <device id="100"> <firmware>12.4(3d)</firmware> <location id="500">Sitting Room</location> </device> - <device id="101"> <firmware>12.4(3d)</firmware> <location id="501">Class... (1 Reply)
Discussion started by: sureshcisco
1 Replies

4. Shell Programming and Scripting

Perl, open multiple files with wildcards

I have a question regarding Perl scripting. If I want to say open files that all look like this and assign them to a filehandle and then assign the filehandle to a variable, how do I do this? The file names are strand1.fa.gz.tmp strand2.fa.gz.tmp strand3.fa.gz.tmp strand4.fa.gz.tmp ...... (6 Replies)
Discussion started by: japaneseguitars
6 Replies

5. Shell Programming and Scripting

Perl script to extract second column from a xls

Can Anyone tell me how to extract the second column of a xls sheet And compare the content of each row of the column with a .h file. xls sheet is having only one spreadsheet. (2 Replies)
Discussion started by: suvenduperl
2 Replies

6. Shell Programming and Scripting

Perl - grep issue in filenames with wildcards

Hi I have 2 directories t1 and t2 with some files in it. I have to see whether the files present in t1 is also there in t2 or not. Currently, both the directories contain the same files as shown below: $ABC.TXT def.txt Now, when I run the below script, it tells def.txt is found,... (5 Replies)
Discussion started by: guruprasadpr
5 Replies

7. Shell Programming and Scripting

Script to search and extract the gene sub-location from gff file.

Hi, my problem is that I have two files. File no. 1 is a gff text file (say gi1) that has gene information like : ******************** gene 39389788..39395643 /gene="RPSA" /note="Derived by automated computational analysis using ... (2 Replies)
Discussion started by: reena2305
2 Replies

8. Shell Programming and Scripting

Search for a pattern,extract value(s) from next line, extract lines having those extracted value(s)

I have hundreds of files to process. In each file I need to look for a pattern then extract value(s) from next line and then search for value(s) selected from point (2) in the same file at a specific position. HEADER ELECTRON TRANSPORT 18-MAR-98 1A7V TITLE CYTOCHROME... (7 Replies)
Discussion started by: AshwaniSharma09
7 Replies

9. Shell Programming and Scripting

Perl script to extract a word from the file

Hi everyone, I'm a perl newbie and need your help to extract a word inside the list of files with same pattern. <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <ns2:mycode xmlns:ns2="http://www.abcd.com/pqrs/acfSchema-2007a.xsd"> <id>10</id> <name>PaymentServices</name> ... (7 Replies)
Discussion started by: jhamaks
7 Replies

10. Shell Programming and Scripting

Need to extract characters between two search words in a script!!

Hi, I have a log file which is the output from a xml script : <?xml version="1.0" ?> <!DOCTYPE svc_result SYSTEM "MLP_SVC_RESULT_320.DTD"> <svc_result ver="3.2.0"> <slia ver="3.0.0"> <pos> <msid type="MSISDN" enc="ASC">8093078040</msid> <poserr> ... (4 Replies)
Discussion started by: arjunstarz
4 Replies
Test::Output(3) 					User Contributed Perl Documentation					   Test::Output(3)

NAME
Test::Output - Utilities to test STDOUT and STDERR messages. VERSION
Version 0.16 SYNOPSIS
use Test::More tests => 4; use Test::Output; sub writer { print "Write out. "; print STDERR "Error out. "; } stdout_is(&writer,"Write out. ",'Test STDOUT'); stderr_isnt(&writer,"No error out. ",'Test STDERR'); combined_is( &writer, "Write out. Error out. ", 'Test STDOUT & STDERR combined' ); output_is( &writer, "Write out. ", "Error out. ", 'Test STDOUT & STDERR' ); # Use bare blocks. stdout_is { print "test" } "test", "Test STDOUT"; stderr_isnt { print "bad test" } "test", "Test STDERR"; output_is { print 'STDOUT'; print STDERR 'STDERR' } "STDOUT", "STDERR", "Test output"; DESCRIPTION
Test::Output provides a simple interface for testing output sent to STDOUT or STDERR. A number of different utilities are included to try and be as flexible as possible to the tester. Originally this module was designed not to have external requirements, however, the features provided by Sub::Exporter over what Exporter provides is just to great to pass up. Likewise, Capture::Tiny provides a much more robust capture mechanism without than the original Test::Output::Tie. TESTS
STDOUT stdout_is stdout_isnt stdout_is ( $coderef, $expected, 'description' ); stdout_is { ... } $expected, 'description'; stdout_isnt( $coderef, $expected, 'description' ); stdout_isnt { ... } $expected, 'description'; stdout_is() captures output sent to STDOUT from $coderef and compares it against $expected. The test passes if equal. stdout_isnt() passes if STDOUT is not equal to $expected. stdout_like stdout_unlike stdout_like ( $coderef, qr/$expected/, 'description' ); stdout_like { ... } qr/$expected/, 'description'; stdout_unlike( $coderef, qr/$expected/, 'description' ); stdout_unlike { ... } qr/$expected/, 'description'; stdout_like() captures the output sent to STDOUT from $coderef and compares it to the regex in $expected. The test passes if the regex matches. stdout_unlike() passes if STDOUT does not match the regex. STDERR stderr_is stderr_isnt stderr_is ( $coderef, $expected, 'description' ); stderr_is {... } $expected, 'description'; stderr_isnt( $coderef, $expected, 'description' ); stderr_isnt {... } $expected, 'description'; stderr_is() is similar to stdout_is, except that it captures STDERR. The test passes if STDERR from $coderef equals $expected. stderr_isnt() passes if STDERR is not equal to $expected. stderr_like stderr_unlike stderr_like ( $coderef, qr/$expected/, 'description' ); stderr_like { ...} qr/$expected/, 'description'; stderr_unlike( $coderef, qr/$expected/, 'description' ); stderr_unlike { ...} qr/$expected/, 'description'; stderr_like() is similar to stdout_like() except that it compares the regex $expected to STDERR captured from $codref. The test passes if the regex matches. stderr_unlike() passes if STDERR does not match the regex. COMBINED OUTPUT combined_is combined_isnt combined_is ( $coderef, $expected, 'description' ); combined_is {... } $expected, 'description'; combined_isnt ( $coderef, $expected, 'description' ); combined_isnt {... } $expected, 'description'; combined_is() directs STDERR to STDOUT then captures STDOUT. This is equivalent to UNIXs 2>&1. The test passes if the combined STDOUT and STDERR from $coderef equals $expected. combined_isnt() passes if combined STDOUT and STDERR are not equal to $expected. combined_like combined_unlike combined_like ( $coderef, qr/$expected/, 'description' ); combined_like { ...} qr/$expected/, 'description'; combined_unlike ( $coderef, qr/$expected/, 'description' ); combined_unlike { ...} qr/$expected/, 'description'; combined_like() is similar to combined_is() except that it compares a regex ($expected) to STDOUT and STDERR captured from $codref. The test passes if the regex matches. combined_unlike() passes if the combined STDOUT and STDERR does not match the regex. OUTPUT output_is output_isnt output_is ( $coderef, $expected_stdout, $expected_stderr, 'description' ); output_is {... } $expected_stdout, $expected_stderr, 'description'; output_isnt( $coderef, $expected_stdout, $expected_stderr, 'description' ); output_isnt {... } $expected_stdout, $expected_stderr, 'description'; The output_is() function is a combination of the stdout_is() and stderr_is() functions. For example: output_is(sub {print "foo"; print STDERR "bar";},'foo','bar'); is functionally equivalent to stdout_is(sub {print "foo";},'foo') && stderr_is(sub {print STDERR "bar";'bar'); except that $coderef is only executed once. Unlike, stdout_is() and stderr_is() which ignore STDERR and STDOUT respectively, output_is() requires both STDOUT and STDERR to match in order to pass. Setting either $expected_stdout or $expected_stderr to "undef" ignores STDOUT or STDERR respectively. output_is(sub {print "foo"; print STDERR "bar";},'foo',undef); is the same as stdout_is(sub {print "foo";},'foo') output_isnt() provides the opposite function of output_is(). It is a combination of stdout_isnt() and stderr_isnt(). output_isnt(sub {print "foo"; print STDERR "bar";},'bar','foo'); is functionally equivalent to stdout_is(sub {print "foo";},'bar') && stderr_is(sub {print STDERR "bar";'foo'); As with output_is(), setting either $expected_stdout or $expected_stderr to "undef" ignores the output to that facility. output_isnt(sub {print "foo"; print STDERR "bar";},undef,'foo'); is the same as stderr_is(sub {print STDERR "bar";},'foo') output_like output_unlike output_like ( $coderef, $regex_stdout, $regex_stderr, 'description' ); output_like { ... } $regex_stdout, $regex_stderr, 'description'; output_unlike( $coderef, $regex_stdout, $regex_stderr, 'description' ); output_unlike { ... } $regex_stdout, $regex_stderr, 'description'; output_like() and output_unlike() follow the same principles as output_is() and output_isnt() except they use a regular expression for matching. output_like() attempts to match $regex_stdout and $regex_stderr against STDOUT and STDERR produced by $coderef. The test passes if both match. output_like(sub {print "foo"; print STDERR "bar";},qr/foo/,qr/bar/); The above test is successful. Like output_is(), setting either $regex_stdout or $regex_stderr to "undef" ignores the output to that facility. output_like(sub {print "foo"; print STDERR "bar";},qr/foo/,undef); is the same as stdout_like(sub {print "foo"; print STDERR "bar";},qr/foo/); output_unlike() test pass if output from $coderef doesn't match $regex_stdout and $regex_stderr. EXPORTS
By default, all tests are exported, however with the switch to Sub::Exporter export groups are now available to better limit imports. To import tests for STDOUT: use Test::Output qw(:stdout); To import tests STDERR: use Test::Output qw(:stderr); To import just the functions: use Test::Output qw(:functions); And to import all tests: use Test::Output; The following is a list of group names and which functions are exported: stdout stdout_is stdout_isnt stdout_like stdout_unlike stderr stderr_is stderr_isnt stderr_like stderr_unlike output output_is output_isnt output_like output_unlike combined combined_is combined_isnt combined_like combined_unlike tests All of the above, this is the default when no options are given. Sub::Exporter allows for many other options, I encourage reading its documentation. FUNCTIONS
stdout_from my $stdout = stdout_from($coderef) my $stdout = stdout_from { ... }; stdout_from() executes $coderef and captures STDOUT. stderr_from my $stderr = stderr_from($coderef) my $stderr = stderr_from { ... }; stderr_from() executes $coderef and captures STDERR. output_from my ($stdout, $stderr) = output_from($coderef) my ($stdout, $stderr) = output_from {...}; output_from() executes $coderef one time capturing both STDOUT and STDERR. combined_from my $combined = combined_from($coderef); my $combined = combined_from {...}; combined_from() executes $coderef one time combines STDOUT and STDERR, and captures them. combined_from() is equivalent to using 2>&1 in UNIX. AUTHOR
Currently maintained by brian d foy, "bdfoy@cpan.org". Shawn Sorichetti, "<ssoriche@cpan.org>" SOURCE AVAILABILITY
This module is in Github: http://github.com/briandfoy/test-output/tree/master BUGS
Please report any bugs or feature requests to "bug-test-output@rt.cpan.org", or through the web interface at <http://rt.cpan.org>. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes. ACKNOWLEDGEMENTS
Thanks to chromatic whose TieOut.pm was the basis for capturing output. Also thanks to rjbs for his help cleaning the documentation, and pushing me to Sub::Exporter. Thanks to David Wheeler for providing code block support and tests. Thanks to Michael G Schwern for the solution to combining STDOUT and STDERR. COPYRIGHT &; LICENSE Copyright 2005-2013 Shawn Sorichetti, All Rights Reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. perl v5.18.2 2014-01-03 Test::Output(3)
All times are GMT -4. The time now is 01:07 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy