Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

 ls | grep FILE

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:


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 ...
share|improve this question
UUOG: sed -n "/FILE.xml/s/.FILE.*//p" – Ignacio Vazquez-Abrams Jul 15 '10 at 11:01
its not perl please see b0fh answer – yael Jul 15 '10 at 11:15
And that's why it's not an answer. – Ignacio Vazquez-Abrams Jul 15 '10 at 11:30
up vote 3 down vote accepted

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;
share|improve this answer
THX very good I choose the second option in my perl script – yael Jul 15 '10 at 10:48
something else how to change your syntax in case the "-" not appears for example from AAA1FILE.xml it will return AAA1 – yael Jul 15 '10 at 11:07
You can replace that ls|grep|sed with sed -ne's/.FILE.*//p' /var/tmp – Evan Carroll Jul 29 '10 at 16:01

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

share|improve this answer
@RESULTS = map /(.*)-FILE\.xml$/, <*-FILE.xml>;
share|improve this answer
@RESULTS = map m#/var/tmp/(.*)-FILE.xml#, </var/tmp/*-FILE.xml>;
share|improve this answer
perl -nE'next unless s/-FILE.*//; push state @lines, $_; die @lines if eof;' /var/tmp
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.