作者liyih ()
看板Perl
标题Re: [问题] 删除文字档案中的换行符号
时间Thu Jan 7 08:52:30 2010
※ 引述《RueyJing (瑞)》之铭言:
: 我真的是翻书和上网找资料找到快疯了也找不到..
: 我想将文字档案中的某些换行符号删去..
: 例如:
: An apple a day , \n
: keeps the doctor away \n
: 我本来是写
: while(<档案>){
: chomp ;
: s/An apple a day , \nkeeps the doctor away/
: An apple a day ,keeps the doctor away/gm ;
: }
: 但是却怎样都没办法出现我想要的结果
: 我有想过要把chomp拿掉..可是拿掉就会出现error讯息..
: 请大家帮帮忙教一下啊..我快抓狂了..今天一整天都在想这个问题..
1. 先确定您的档案要一行一行读进来吗?而且使用 \n 来区隔。
这样 \n 只会在每行的最後面出现,经 chomp 处理完也应该不见了。
while (<ARGV>) {
#chomp;
print "[$_]";
}
2. 另外也可以考虑整个档案都读进来,再来做字串取代。
my $s = '';
READFILE: {
local $/;
$s = <ARGV>;
}
3. 确定字串中是否会包含 \n,选择合适的正规表示法来处理。
使用 Perl regexp 中的 modifiers,如 x、m、s、g 等,
像是 s/pattern/replacement/xms 或 s/pattern/replacement/gm。
且记得正确处理空白与换行符号。
m Multiline mode - ^ and $ match internal lines
s match as a Single line - . matches \n
x eXtended legibility - free whitespace and comments
g Global - all occurrences
建议参考:
1.
http://perldoc.perl.org/perlreref.html
2. perlre、perlreref
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 140.114.64.130
1F:推 paulpu:perl -pi -e 's/(An apple a day , )\n/$1/g' 档名 01/07 12:50