Perl 文件处理范例
觉得这个范例不错就保存了,原文地址在这里:http://www.cnblogs.com/zhangzhi/archive/2010/10/19/1855302.html
Perl 文件处理范例
. 任意字符
?0或者1个
* 任意个
+ 一个或者以上
$_ 默认数组
$@ 第一被匹配的字符
$` 被匹配字符之前的字符
$' 被匹配字符之后的字符
$1 第一个被匹配的字符,以左括号的顺序算。
<>砖石输入符
=~ 匹配判断符号
\d 数字
\D 非数字
\W 非 [A-Za-z0-9_]
\S 非字符
{n} 重复n次
open FILE, "file.txt" 打开已经存在的文件
open FILE,">file.txt" 打开file.txt,如果不存在的话就创建file.txt
open FILE,">>file.txt" 打开,并将新内容追加到文本的末端,如果不存在的话,创建file.txt
双引号内的转义符
\r 回车
\t 制表符
\f formfeed
\b 退格
\a 响铃
\e escape(ASCII 中的escape 字符)
\007 任何八进制值(这里是,007=bell(响铃))
\x7f 任何十六进制值(这里是,007=bell)
\cC 一个控制符(这里是,ctrl +c)
\\ 反斜线
\” 双引号
\l 下个字符小写
\L 接着的字符均小写直到\E
\u 下个字符大写
\U 接着的字符均大写直到\E
\Q 在non-word 字符前加上\,直到\E
\E 结束\L,\U 和\Q
example1
从一个mail.list中识别 @eric.com的usr name,排序后输出到result.list中。
mail.list:
f@brand.com
d@eric.com
g@syv.com
a@eric.com
h@mail.com
c@eric.com
x@joey.com
b@eric.com
perl script:
代码
#! /usr/bin/perl #perl directory declaration open MAIL,"mail.list"; #open and read mail.list context
open RESULT,">result.list"; #create a new file named" result.list", using filehandle "RESULT" to transfer information $n =0; #define a varibale foreach (<MAIL>){ #processing each line from context <MAIL>
if(/(\@eric\.com)/) # judge if pattern"@eric.com" matched, and store it in "$&"
{ $array[$n]= "$`\n"; # store words before matched("$`") in @array
$n =$n +1; #index add one
}
} @sorted = sort (@array); # sort array by letters print RESULT @sorted; # print array in output file close RESULT; # close file
close MAIL;
result.list
a
b
c
d
example2
将当前目录下所有 .cc结尾的文件 重命名为 .c结尾
#! /usr/bin/perl
@list =glob('./*.cc');
foreach $list(@list){
my $name = $list;
$name =~ s/cc$/c/;
rename $list,$name;
}
example 3
将文件的中的各个module 实例化,输出到新的文件中。
代码
#! /usr/bin/perl open TMP,">instance.v"; while(<>){
if(/^module (.*)\((.*)\);$/m){
$module_name =$1;
$port_list = $2;
@ports = split(/,/,$port_list);
my $n =@ports;
my $i=0; print TMP "$module_name U_$module_name(\n"; for($i=0; $i<$n; $i =$i+1){
print TMP "$ports[$i]($ports[$i]),\n";
if($i==$n -1){
print TMP ");\n";
}
}
}
} close TMP;
close CODE;
windows下perl脚本范例
1 #! C:\strawberry\perl\bin\perl
2 system("update.bat"); #运行脚本
3 open FILE, "file.lst";
4 @lines =<FILE>;#将文本所有内容读入@lines
5
6 foreach $lines (@lines){ #处理@lines中的每行
7 chomp($lines);
8 open SOURCE,"$lines";
9 my @content=<SOURCE>;
10 open Result ,"> ./result/$lines"; #windows下的路径也是用斜杠,而不是反斜杠
11 #$lines =~ s/txt/png/;
12 #print Result "$lines\n";
13 foreach $content(@content)
14 {
15 if($content =~ /Image filename/)
16 {
17 chomp($content);
18 @dirname =split(/"/,$content);
19 print Result "$dirname[$#dirname] \n";
20 print "$dirname[$#dirname] \n";
21 }
22
23
24 if($content =~ /(Objects with ground truth : )/)
25 {
26 $re = $';
27 @num =split(/\s/,$re);
28 print Result "$num[0] \n";
29 }
30
31 if($content =~ /(\(Xmin, Ymin\) - \(Xmax, Ymax\) :)/)
32 {
33 $a=$';
34 if($a =~ /([0-9]+)\D*([0-9]+)\D*([0-9]+)\D*([0-9]+)/)
35 {
36 print Result "$1 $2 $3 $4 \n";
37 }
38
39 }
40
41 }
42 close SOURCE;
43 close Result;
44
45 }
46 close FILE;
47
48
Perl 文件处理范例的更多相关文章
- 分享:perl 文件操作总结
发布:thebaby 来源:net [大 中 小] perl 文件操作,包括打开.关闭文件,读取.定入文件等.原文链接:http://www.jbxue.com/article/3153.html 打 ...
- 单片机成长之路(51基础篇) - 015 关于sdcc的多文件编译范例二
本文是续 单片机成长之路(51基础篇) - 009 关于sdcc的多文件编译范例(一)编写的. 在实际的工作中,单片机的头文件和功能函数不可能同全部放在同一个文件夹下面,我们把单片机成长之路(51基础 ...
- Makefile中怎样调用python和perl文件为自己提供须要的数据
Makefile中怎样调用python和perl文件为自己提供须要的数据,利用print函数对外输出数据 实例代码例如以下 perl.pl #!/usr/bin/perl print("he ...
- perl文件操作
Perl 文件操作 Perl 使用一种叫做文件句柄类型的变量来操作文件. 从文件读取或者写入数据需要使用文件句柄. 文件句柄(file handle)是一个I/O连接的名称. Perl提供了三种文件句 ...
- Perl文件测试操作和stat函数
在shell中通过test命令或者中括号[]可以进行文件测试以及其它类型的测试,例如判断文件是否存在,比较操作是否为真等等.perl作为更强大的文本处理语言,它也有文件测试类表达式,而且和shell的 ...
- Perl文件、目录常用操作
注意,这些操作的对象是文件名(相对路径/绝对路径),而非文件/目录句柄,句柄只是perl和文件系统中文件的关联通道,而非实体对象. 创建文件 在unix类操作系统中有一个touch命令可以非常方便的创 ...
- 单片机成长之路(51基础篇) - 009 关于sdcc的多文件编译范例(一)
本文是续 单片机成长之路(51基础篇) - 006 在Linux下搭建51单片机的开发烧写环境编写的. 本范例主要由(main.c ,delay.h,delay.c,makefile)4个文件组成,s ...
- Perl文件读写
Perl File Handling: open, read, write and close files #==================== Opening files Solution 1 ...
- Linux网络编程:socket文件传输范例
基于TCP流协议的socket网络文件传输Demo: 实现:C语言功能:文件传输(可以传任何格式的文件) /********************************************** ...
随机推荐
- 实验吧—Web——WP之 Guess Next Session
打开链接,他有给出查看原码的按钮,那么我们打开看看 在这个里面,如果GET的值等于session的就会给出flag 那么我们进行抓包改包 在输入框内随意输入一个值然后抓包 将password的值删去, ...
- Two Sum II - Input array is sorted
Given an array of integers that is already sorted in ascending order, find two numbers such that the ...
- 【BZOJ2120】数颜色
看代码学习好,好学好懂好ac 原题: 墨墨购买了一套N支彩色画笔(其中有些颜色可能相同),摆成一排,你需要回答墨墨的提问.墨墨会像你发布如下指令: 1. Q L R代表询问你从第L支画笔到第R支画笔中 ...
- protobuf GetExtension
get extention values from proto file value, err := proto.GetExtension(imp, openrtb.E_Imp) if err != ...
- linux前后台任务的切换以及执行暂停
command & 把command命令放到后台执行 ctrl+z 暂停该任务,并且放到后台 jobs 查看任务 bg n 把jobs号码为n的任务放到后台执行 fg n 把jobs号码为n的 ...
- C# to IL 8 Methods(方法)
The code of a data type is implemented by a method, which is executed by the ExecutionEngine. The CL ...
- Laya播放unity特效
杭州-fun 2017/12/5 20:47:12 其实网上就有你搜下就有了现成的脚本,设置帧数和截取时间它会截屏并保存成贴图导入laya生成atlas就能用了 就是unity的截屏功能 就 ...
- requestAnimationFrame 知识点
与setTimeout相比,requestAnimationFrame最大的优势是由系统来决定回调函数的执行时机.具体一点讲,如果屏幕刷新率是60Hz,那么回调函数就每16.7ms被执行一次,如果刷新 ...
- c++获取键盘输入cin、scanf使用详解
cin是c++标准,scanf是在c中使用的 #include<cstdio> #include<iostream> #include<cstring> using ...
- VS2010生成安装包制作步骤 (转)
阅读目录 VS2010生成安装包制作步骤 回到目录 VS2010生成安装包制作步骤 在VS2010中文旗舰版本中生成winForm安装包,可以复制你电脑中的开发环境,避免你忘记了一下配置然后在别的 ...