I need some help in understanding how to grep the right info in perl.
Here is the requirement. I have some files in a subdir './somedir':
The files I want to retrieve are:
I wrote a piece of perl code to do this job:
I know the line in blue works. But this part ‘!/\d\.\d\.[0-9]_b’ looks redundant to me. However, w/o that part, neither line in RED works as I expected!!
Here is the output while the line in red is used:
1, $_ = abc.5.1.0_b249.tar.gz
2, $_ = abc.5.1.1_b008.tar.gz
3, $_ = abc.5.2.0_b187.tar.gz
4, $_ = abc.5.2.1_b188.tar.gz
5, $_ = abc.5.2_b186.tar.gz
6, $_ = abc.5.3.0_b161.tar.gz
7, $_ = abc.5.3.1_b141.tar.gz
8, $_ = abc.5.3_b170.tar.gz
Why would ‘grep (/\d\.\d_b/, readdir(DIR))’ return something with \d\.\d\.\d_b? Why not only return \d\.\d_b? What is the better way to code this?
Many thanks!!
Here is the requirement. I have some files in a subdir './somedir':
Code:
% ls
abc.5.1.0_b249.tar.gz
abc.5.1.0.sp3_b249.tar.gz
abc.5.1.1_b008.tar.gz
abc.5.2.0_b187.tar.gz
abc.5.2.0.ga_b187.tar.gz
abc.5.2.1_b188.tar.gz
abc.5.2_b186.tar.gz
abc.5.2.sp2_b186.tar.gz
abc.5.3.0_b161.tar.gz
abc.5.3.0.ga_b161.tar.gz
abc.5.3.1_b141.tar.gz
abc.5.3_b170.tar.gz
abc.5.3.sp1_b170.tar.gz
efg
lmn
Code:
% ls abc*5.?_b*
abc.5.2_b186.tar.gz
abc.5.3_b170.tar.gz
I wrote a piece of perl code to do this job:
Code:
my $dir = './dir4RdDir/';
opendir(DIR, $dir);
[COLOR=blue]#my @files = sort(grep (/\d\.\d_b/ && !/\d\.\d\.[0-9]_b/, readdir(DIR)));[/color]
[COLOR=red]
#my @files = sort(grep (/\d\.\d[b]\[/b]_b/, readdir(DIR)));
my @files = sort(grep (/\d\.\d_b/, readdir(DIR)));
[/color]
my $i = 1;
foreach (@files) {
print "$i, \$_ = $_\n";
$i++;
}
I know the line in blue works. But this part ‘!/\d\.\d\.[0-9]_b’ looks redundant to me. However, w/o that part, neither line in RED works as I expected!!
Here is the output while the line in red is used:
1, $_ = abc.5.1.0_b249.tar.gz
2, $_ = abc.5.1.1_b008.tar.gz
3, $_ = abc.5.2.0_b187.tar.gz
4, $_ = abc.5.2.1_b188.tar.gz
5, $_ = abc.5.2_b186.tar.gz
6, $_ = abc.5.3.0_b161.tar.gz
7, $_ = abc.5.3.1_b141.tar.gz
8, $_ = abc.5.3_b170.tar.gz
Why would ‘grep (/\d\.\d_b/, readdir(DIR))’ return something with \d\.\d\.\d_b? Why not only return \d\.\d_b? What is the better way to code this?
Many thanks!!