perl 文件/路径 读写删
来源:互联网 发布:魏志香 知乎 编辑:程序博客网 时间:2024/05/02 00:49
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 -wuse 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 -wuse 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 -wsub 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 onewhile (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/perlprint "\nOpen a text file.\n";open IN, "<data1.txt"; #read a text filemy @raw=<IN>; #using @raw store datachomp(@raw); #skip enter keysclose(IN);#print "@raw \n";my @ref; #make 2d array #'$#' = length of @rawfor ($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 -wopen 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 -wopen 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 -wuse 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
- perl 文件/路径 读写删
- perl文件读写
- perl 读写文件
- perl简单读写文件
- Perl文件读写
- Perl 文件读写操作
- perl读写文件
- PERL中的文件读写
- Perl文件读写
- perl 读写文件
- Perl入门(五)Perl文件读写
- 用 Perl 读写 Excel 文件
- 用 Perl 读写 Excel 文件
- perl 文件读写详细说明
- perl 文件读写详细说明
- Perl简单的文件读写
- Perl—— 文件读写
- Perl 简单读写XML 文件
- 程序员的“非程序员”之路
- Android Design(设计)\Get Started(从这里开始)\Creative Vision(创造性视觉)
- 点云三维重建有关原理
- 7000万嫁女煤老板折射中国式土豪困境:富而不贵
- 升级可能导致数据库不可用[待看]
- perl 文件/路径 读写删
- AwesomePlayer的准备工作
- 如何在iOS地图上高效的显示大量数据
- Sharepoint lookup字段的关联列表查询
- 明星煤老板的喜与忧
- 【分享】娱乐圈大V微博信息数据
- maven - Maven Weed
- ftp
- 现在就开始使用AngularJS的三个重要原因