0

申し訳ありませんが、これが冗長な場合ですが、部分的に機能しているperlスクリプトがあります。どちらかを抽出する正規表現foo|barと、指定された文字列のプレフィックスがあります。しかし、問題は、私の文字列もファイル名であり、その内容を開いて取得したいことですlocale_col.dat.2010120813.png(以下の期待される出力を参照)。

出力は次のようになります。

Content:/home/myhome/col/.my_file_del.mail@locale.foo.org
Key1:foo:Key2:col
Content:/home/myhome/col/.my_file_del.dp1.bar.net
Key1:bar:Key2:col
Content:/home/myhome/jab/.my_file_del.mail@locale.foo.org
Key1:foo:Key2:jab
Content:/home/myhome/jab/.my_file_del.dp1.bar.net
Key1:bar:Key2:jab

1回のパスで文字列のリスト(FileList.txtからのファイル名)を読み取り、ファイル名パスから特定の値を抽出し(regexを使用)、その内容のファイル名を開くことができるように、これを微調整するのに助けが必要です。それが理にかなっていることを願っていますか、それともこれを2つのperlスクリプトに分割することを検討していますか?ご入力いただきありがとうございます。

コード(WIP):

open FILE, "< /home/myname/FileList.txt";
while (<FILE>) {
 my $line = $_;
   chomp($line);
      print "Content:$_"; #This is just printing the filenames. 
                #I want to get the contents of those file names instead. Stuck here.
      if ($line =~ m/home\/myname\/(\w{3}).*[.](\w+)[.].*/){
         print "Key1:$2:Key2:$1\n";
      }
}
close FILE;

FileList.txtの内容:

/home/myname/col/.my_file_del.mail@locale.foo.org
/home/myname/col/.my_file_del.dp1.bar.net
/home/myname/jab/.my_file_del.mail@locale.foo.org
/home/myname/jab/.my_file_del.dp1.bar.net

リストされたファイルの1つのコンテンツの例:(抽出するにはここでヘルプが必要です)

$ cat .my_file_del.mail@locale.foo.org 
locale_col.dat.2010120813.png

期待される出力:

Content:locale_col.dat.2010120813.png
Key1:foo:Key2:col
...
..
4

2 に答える 2

3

ファイル名をお持ちの場合は、それらを開いてみませんか?

use strict;
use warnings;
use 5.010;
use autodie;

open my $fh, '<', '/home/myname/FileList.txt';
while (my $line = <$fh>) {
    chomp $line;
    say "Key1:$2:Key2:$1" if m!home/myname/(\w{3})[^.]*[.](\w+)[.].*!;
    next unless -e $line; #We skip to the next line unless the file exists
    open my $inner_fh, '<', $file;
    while (<$inner_fh>) {
        say;
    }
}
于 2010-12-10T18:18:34.910 に答える
3

これを行う方法は次のとおりです。

#!/usr/bin/perl
# ALWAYS these 2 lines !!!
use strict;
use warnings;

my $file = '/home/myname/FileList.txt';
# use 3 args open and test openning for failure
open my $FILE, '<', $file or die "unable to open '$file' for reading: $!";
while (my $line = <$FILE>) {
    chomp($line);
    print "Content:$line\n"; #This is just printing the filenames. 
    #I want to get the contents of those file names instead. Stuck here.
    if ($line =~ m#home/myname/(\w{3}).*[.](\w+)[.].*#) {
        open my $file2, '<', $line or die "unable to open '$file' for reading: $!";
        while(my line2 = <$file2>) {
          print $line2;
        }
        close $file2;
        print "Key1:$2:Key2:$1\n";
    }
}
close $FILE;
于 2010-12-10T18:20:21.517 に答える