如何用perl 从文件里面抓一段内容出来?
时间:2011-05-06
来源:互联网
假设文件 output.txt的内容如下:
====================
the following are the list of products:
airplan
jet
bullet
gun
the following are the list of money:
RMB
Dollar
Riyuan
====================
现在我想把两个list的title和内容,分别放到两个不同的文件中去,比如:
the following are the list of products:
shoe
table
bug
放到文件 product.txt里面去。同样的,另外一个放到 money.txt里面去。但我只知道用//可以查找,但接下怎么把这块内容截出来,我就不会了。向大侠们请教。
open FILE, "< output.txt";
while(<FILE>)
{
if (/the following are the list of products:/)
{
}
if (/the following are the list of money:/)
{
}
}
====================
the following are the list of products:
airplan
jet
bullet
gun
the following are the list of money:
RMB
Dollar
Riyuan
====================
现在我想把两个list的title和内容,分别放到两个不同的文件中去,比如:
the following are the list of products:
shoe
table
bug
放到文件 product.txt里面去。同样的,另外一个放到 money.txt里面去。但我只知道用//可以查找,但接下怎么把这块内容截出来,我就不会了。向大侠们请教。
open FILE, "< output.txt";
while(<FILE>)
{
if (/the following are the list of products:/)
{
}
if (/the following are the list of money:/)
{
}
}
作者: 百分百好牛 发布时间: 2011-05-06
- open FILE, "< output.txt";
- open OUT, ">> out.txt" or die "fail:$!";
- while(<FILE>)
- {
- if (/the following are the list of products:/)
- {
- print OUT "$_";
- }
-
- if (/the following are the list of money:/)
- {
-
- }
- }
作者: lkk2003rty 发布时间: 2011-05-06
谢谢楼上的兄弟先,你的这个方法,只能把 title 如"the following are the list of products:" 打印到文件吧,后面紧跟着的list呢?
谢谢。
谢谢。
作者: 百分百好牛 发布时间: 2011-05-06
回复 百分百好牛
用个变量标示 控制下 就是了
my $flag;
if(/regex/)
{
$flag=1;
}
print OUT "$_" if $flaf == 1;
类似这样 不同的段 flag设成不同的值就是了
用个变量标示 控制下 就是了
my $flag;
if(/regex/)
{
$flag=1;
}
print OUT "$_" if $flaf == 1;
类似这样 不同的段 flag设成不同的值就是了
作者: lkk2003rty 发布时间: 2011-05-06
let me try it, thx.
作者: 百分百好牛 发布时间: 2011-05-06
QUOTE:
假设文件 output.txt的内容如下:
====================
the following are the list of products:
air ...
百分百好牛 发表于 2011-05-06 10:05
====================
the following are the list of products:
air ...
百分百好牛 发表于 2011-05-06 10:05
$ cat output.pl
- use strict;
- use warnings;
-
- my $sOut_file="";
- open(FILE, "<", "output.txt") or die "can't open output.txt\n";
-
- while(<FILE>){
- # the following are the list of products:
- # the following are the list of money:
- if (/the following are the list of (.+):/){
- close(FHout) if($sOut_file ne "");
- $sOut_file = "$1.txt";
- open(FHout, ">", $sOut_file) or die "can' open $sOut_file\n";
- print "open $sOut_file file to write data\n";
- }
- print FHout $_;
- }
- close FHout;
open products.txt file to write data
open money.txt file to write data
$
作者: jason680 发布时间: 2011-05-06
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28