example8:套路模板:
8.1 读文件:
open ($fdr,"<$file")
#it is so cool, all lines can been read directly in array without while
@lineall=<$fdr>;
8.2 读文件:
while (<fdr>) $line=$_;
8.3 读路径
local *DH;
opendir(DH,$item)
foreach (readdir(DH))
一次性将路径读入数组
my @subdir1=readdir ($dir);
读路径下的文件:
my $subdir="/bld/xyz/toolgeneric/328";
@files=glob("$subdir/*/*");
example3: remove folder and file which is data time before one year
#!/opt/exp/bin/perl -w
use File::Path;
my $subdir="/home/coolclf/rje/apaloads";
@files=glob("$subdir/*");
foreach my $file (@files) {
my $mtime = (stat $file)[9];
my @t = localtime $mtime;
$date = sprintf "%02u/%02u/%02u %02u:%02u:%02u", $t[4] + 1, $t[3], $t[5] % 100, $t[2], $t[1], $t[0]; ####get file time status
print $date, "\n";
my $a = ($t[5] % 100);
if ($a<13) {
# unlink $file; #only remove file
print "line15:$file \n";
# system("rm -rf $file"); #method 1 :remove folder
# rmtree("$file"); #methold 2:remove folder
unlink <$file/*>; #method 3:remove folder
rmdir $file;
}
}
example4:remove.pl 有数字比较和unless 和rm folder和时间
#!/opt/exp/bin/perl -w
use File::Path;
my $subdir="/opt/cool2b";
my @files=glob("$subdir/*");
foreach my $file (@files) {
#print "file=$file\n";
@array=stat("$file");
my $mtime = (stat $file)[9];
my @t = localtime $mtime;
my $year = ($t[5] % 100);
# print ("line13:year=$year;$t[5]\n");
# if ( $year eq "9" || $year eq "10" || $year eq "11" || ) {
if ( $year > 8 && $year < 10 ) {
print "line15:file=$file\n";
rmdir $file;
}
#$date = sprintf "%02u/%02u/%02u %02u:%02u:%02u", $t[4] + 1, $t[3], $t[5] % 100, $t[2], $t[1], $t[0];
# print $date, "\n";
$uid=$array[4];
# my $name="";
$name=(getpwuid $uid)[0];
# ($name, $passwd, $uid, $gid, $quota,
# $comment, $gcos, $dir, $shell) = getpwuid($uid);
#if ( defined ($name) ) {
unless ( defined ($name) ) {
# print ("file=$file;uid=$uid;name=$name\n");
# print ("line25:file=$file\n");
# rmdir $file;
}
# if ($name ne "" ) {
# if ( $name ) {
# print ("file=$file;uid=$uid;name=$name\n");
# }
}
example1: read from command line
钻石操作符<>是perl的读取文件每一行的操作符,是一个整行输入操作符的特例
#!/opt/exp/bin/perl -w
sub say {
print @_,"\n";
}
my $file =$ARGV[0];
open (my $fh, '<',$file) or die $!;
#@a=<$fh> read file to array; then while read array one by one
while (my $line=<$fh>) {
if ($line =~ /^bjmcl0(\d\d)/) {
if ($1>=33 and $1<=63) {
#read from $_
$line=~s/^/#/;
}
}
# print $line."\n";
say $line;
}
example2: read and write files
#!/opt/exp/bin/perl
print "\nOpen a text file.\n";
open IN, "<data1.txt"; #read a text file
my @raw=<IN>; #using @raw store data
chomp(@raw); #skip enter keys
close(IN);
#print "@raw \n";
my @ref; #make 2d array
#'$#' = length of @raw
for ($i = 0 ; $i <= $#raw ; $i++) {
my @tmp = split(/\s/, $raw[$i]);
$ref[$i]=\@tmp; #each line is a refence in @ref
print "$i;$ref[$i];$tmp[$i] \n";
}
#!/opt/exp/bin/perl -w
open my $fh , '<' ,'1';
open my $fd , ">>" ,"2";
while (my $line=<$fh>) {
print $fd $line;
}
close ($fh);
close ($fd);
下面这段网络的思路我觉得挺对的:留下来仅供参考
做工程就应该是这样,熟练于一种,应用后组合
"我只知道一种操作符,> , 而且每次都只会这么写
open(my $fh, '>', 'filename') or die $!;
实在搞不清楚那么多的读写符号。我在读写文件时,现在只用 File::Slurp 模块的
read_file write_file 函数接口。
在处理文件时,我很少把输出文件和输入文件设置成一个。因为一旦搞错,数据就毁了。
通常是,设置两个不同的文件,一个读,一个写,经过确认没有问题后,也不敢把原先的文件覆写。而是备份起来。
遍历文件中,对于忽略的行,通常用 next 那么在新文件中就不出现了"
===================================
#!/opt/exp/bin/perl -w
open my $fh, '<', '1-2.txt' or die $!;
while ($line=<$fh>) {
if ($line=~/felixzh1/) {
# $line=~s/.*//g;
next;
}
print $fh1 $line;
}
close $fh
example 6: 遍历subdirectory files
grep /bld/xyz/toolgeneric/328/*/*
<pre code_snippet_id="637883" snippet_file_name="blog_20150407_5_4549377" name="code" class="html">ARGV表示命令行参数
#!/opt/exp/bin/perl -wmy $dir=$ARGV[0];print "$dir \n";my $fd;#my @subdir1=readdir ($dir);#print @subdir1;my $subdir="/bld/xyz/toolgeneric/328";@files=glob("$subdir/*/*");----------------------globe define files#print "line10 @files \n";
foreach my $file(@files) {
print "line12:$file \n";
my $fh;
open ($fh ,$file) or next;
while (<$fh>) {
if (/xyz/) {
print "xyz line17 $file:,$_";
}
}
close($fh);
}
</pre><pre code_snippet_id="637883" snippet_file_name="blog_20150407_5_4549377" name="code" class="html">example7:
special variables----INPLACE_EDIT
<p>$^I 内置控制编辑器的值--------------------------仅用于读写同一文件
#!/usr/bin/perl -w
use strict;
$^I = '.bak';
while (<ARGV>) {
s/felixzh1//g;
print;
}</p>
example 9 find2 查找路径--递归
#!/opt/exp/bin/perl -w
use strict;
use warnings;
sub lsr {
sub lsr;
my $cwd = shift;
local *DH;
if (!opendir(DH, $cwd)) {
warn "Cannot opendir $cwd: $! $^E";
return undef;
}
foreach (readdir(DH)) {
if ($_ eq '.' || $_ eq '..') {
next;
}
my $file = $cwd.'/'.$_;
if (!-l $file && -d _) {
$file .= '/';
lsr($file);
}
# process($file, $cwd);
process($file);
}
closedir(DH);
}
my ($size, $dircnt, $filecnt) = (0, 0, 0);
sub process($$) {
my $file = shift;
print $file, "\n";
if (substr($file, length($file)-1, 1) eq '/') {
$dircnt++;
}
else {
$filecnt++;
$size += -s $file;
}
}
lsr('.');
print "$filecnt files, $dircnt directory. $size bytes.\n";
example 10: find 非递归
use strict;
use warnings;
sub lsr_s {
my $cwd = shift;
my @dirs = ($cwd.'/');
my ($dir, $file);
while ($dir = pop(@dirs)) {
local *DH;
if (!opendir(DH, $dir)) {
warn "Cannot opendir $dir: $! $^E";
next;
}
foreach (readdir(DH)) {
if ($_ eq '.' || $_ eq '..') {
next;
}
$file = $dir.$_;
if (!-l $file && -d _) {
$file .= '/';
push(@dirs, $file);
}
# process($file, $dir);
process($file);
}
closedir(DH);
}
}
my ($size, $dircnt, $filecnt) = (0, 0, 0);
sub process($$) {
my $file = shift;
print $file, "\n";
if (substr($file, length($file)-1, 1) eq '/') {
$dircnt++;
}
else {
$filecnt++;
$size += -s $file;
}
}
lsr_s('.');
print "$filecnt file