简体   繁体   中英

How do you list all files without a suffix using Perl?

I want to add a suffix to all the files without a suffix in a directory with mixed content.

  1. I only want to fetch files without a suffix
  2. Then I want to add a suffix to them (like, say, .txt or .html )

It's part one I'm having trouble with.

I'm using glob to fetch all the files. Here's the code excerpt:

 my @files = grep ( -f, <*> );

-f makes sure only files are added, and the * wildcard allows all names.

But how do I rewrite that to only fetch files that have no suffix? Or in the least, how do I wash the array of suffixed files?

You can just tack on another grep statement:

my @files = grep !/\.\w{1,4}$/, grep -f, <*>;
#           -------------------

Or you can, as Borodin points out, do it in one:

my @files = grep !/\.\w{1,4}$/ && -f, <*>;

You can change the regex to fit better depending on what type of suffixes you have. The regex looks for files which do not match a period, followed by 1 to 4 alphanumeric characters, at the end of the string. I opted for a rather loose regex to match a multitude of possible suffixes.

没有后缀的文件就是名称中没有点的文件。

my @files = grep { -f and not /\./ } <*>;

使用grep ,您需要添加一个正则表达式:

my @files = grep { !/\.\w+\z/ && -f } ( <*> );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM