读写perl中的文件 [英] Read and Write to a file in perl
问题描述
这个
只是
的一个例子。
让我们假设上面是 out.txt 。我想读 out.txt 并写入到同一个文件中。
< Hi>
< this>
<就是>
<例子>
修改 out.txt 。
我想在一些行的开始和结尾添加标签。
因为我将会多次阅读这个文件,所以我不能每次都把它写到一个不同的文件上。
编辑1
我尝试过使用+<
但是它给出了这样的输出:
您好
这
只是
一个例子。
< Hi>
< this>
<就是>
<例子>
** out.txt **
编辑2
参考代码:
open(my $ fh,+<,out.txt); #或死无法打开< C:\ Users \daanishs\workspace\CCoverage\out.txt:$!; $($ fh>)
{
$ s1 =<;
$ s2 = $ _;
$ s3 =>;
$ str = $ s1。 $ s2。 $ S3;
print $ fh$ str;
正试图做的是有缺陷的。文件以
H i / this / ...
如果你想改变它的位置,在处理完第一行之后,看起来如下:
< H i> / i s / ...
注意你如何破坏th?您需要复制文件,修改副本,用副本替换原始文件。
最简单的方法是将此副本存储在内存中。 p>
my $ file;
{#读取文件
open(my $ fh,'<',$ qfn)
或者无法打开\ $ qfn \:$!\\ \
;
本地$ /;
$ file =< $ fh>;
#更改文件
$ file =〜s /^(.*)\\\
/< $ 1> \\\
/ mg;
保存更改
打开(my $ fh,'>',$ qfn)
或者死掉Can not create \$ qfn\ :$!\\\
;
print($ fh $ file);
如果你想使用这个磁盘:
重命名($ qfn,$ qfn.old)
或死无法重命名\$ qfn\:$ !\\\
;
open(my $ fh_in,'<',$ qfn.old)
或死无法打开\$ qfn\:$!\ N;
open(my $ fh_out,'>',$ qfn)
或死不能创建\$ qfn\:$!\\\
;
while(< $ fh_in>){
chomp;
$ _ =< $ _>;
print($ fh_out$ _\\\
);
}
unlink($ qfn.old);
使用一招,上面可以简化为
local @ARGV = $ qfn;
本地$ ^ I ='';
while(<>){
chomp;
$ _ =< $ _>;
print(ARGV$ _\\\
);
}
或作为单行:
perl -i -pe'$ _ =< $ _>'文件
this
is just
an example.
Lets assume the above is out.txt. I want to read out.txt and write onto the same file.
<Hi >
<this>
<is just>
<an example.>
Modified out.txt.
I want to add tags in the beginning and end of some lines.
As I will be reading the file several times I cannot keep writing it onto a different file each time.
EDIT 1
I tried using "+<"
but its giving an output like this :
Hi
this
is just
an example.
<Hi >
<this>
<is just>
<an example.>
**out.txt**
EDIT 2
Code for reference :
open(my $fh, "+<", "out.txt");# or die "cannot open < C:\Users\daanishs\workspace\CCoverage\out.txt: $!";
while(<$fh>)
{
$s1 = "<";
$s2 = $_;
$s3 = ">";
$str = $s1 . $s2 . $s3;
print $fh "$str";
}
解决方案 The very idea of what you are trying to do is flawed. The file starts as
H i / t h i s / ...
If you were to change it in place, it would look as follows after processing the first line:
< H i > / i s / ...
Notice how you clobbered "th"? You need to make a copy of the file, modify the copy, the replace the original with the copy.
The simplest way is to make this copy in memory.
my $file;
{ # Read the file
open(my $fh, '<', $qfn)
or die "Can't open \"$qfn\": $!\n";
local $/;
$file = <$fh>;
}
# Change the file
$file =~ s/^(.*)\n/<$1>\n/mg;
{ # Save the changes
open(my $fh, '>', $qfn)
or die "Can't create \"$qfn\": $!\n";
print($fh $file);
}
If you wanted to use the disk instead:
rename($qfn, "$qfn.old")
or die "Can't rename \"$qfn\": $!\n";
open(my $fh_in, '<', "$qfn.old")
or die "Can't open \"$qfn\": $!\n";
open(my $fh_out, '>', $qfn)
or die "Can't create \"$qfn\": $!\n";
while (<$fh_in>) {
chomp;
$_ = "<$_>";
print($fh_out "$_\n");
}
unlink("$qfn.old");
Using a trick, the above can be simplified to
local @ARGV = $qfn;
local $^I = '';
while (<>) {
chomp;
$_ = "<$_>";
print(ARGV "$_\n");
}
Or as a one-liner:
perl -i -pe'$_ = "<$_>"' file
这篇关于读写perl中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!