1

In my /var/tmp I have the following files:

 ls | grep FILE
   AAA-FILE.xml
   BBB-FILE.xml

With the following command I cut the first word of the files as follows:

ls /var/tmp | grep FILE.xml | sed "s/.FILE.*//"

I get:

AAA
BBB

How can I do the same thing with Perl in order to insert the "AAA" and "BBB" in to @RESULTS? For example:

@RESULTS=perl syntax ...
Gareth
  • 18,809
yael
  • 13

5 Answers5

3

Assuming you want to keep using the existing command, you could do

my @results = `ls /var/tmp | grep FILE.xml | sed "s/.FILE.*//"`;
chomp for @results;

the chomp is to get rid of the trailing newline.

Without resorting to calling external commands:

my @results;
opendir DIR,"/var/tmp" or die "Cannot open dir: $!";
for (readdir DIR) {
    /(.*)-FILE.*/ or next;
    push @results, $1;
}
closedir DIR;
b0fh
  • 2,275
2

@RESULTS = grep(s[/var/tmp/(.*)-FILE.xml][$1],</var/tmp/*>);

David Webb
  • 12,036
2
@RESULTS = map /(.*)-FILE\.xml$/, <*-FILE.xml>;
Gareth
  • 18,809
jdporter
  • 31
  • 2
1
@RESULTS = map m#/var/tmp/(.*)-FILE.xml#, </var/tmp/*-FILE.xml>;
jdporter
  • 31
  • 2
1
perl -nE'next unless s/-FILE.*//; push state @lines, $_; die @lines if eof;' /var/tmp
Evan Carroll
  • 8,895