如何使用perl的Tie::File?模塊刪除文件固定行
使用perl的Tie::File 模塊刪除文件固定行, 為了說(shuō)明簡(jiǎn)單代碼中處理的是固定第二行開始的3行長(zhǎng)度。下面給出perl代碼:
#! /usr/bin/perl
use v5.14;
use Tie::File;
if (@ARGV == 0) {
say "請(qǐng)輸入一個(gè)文件名 !!!";
exit 1;
}
my $filePath = $ARGV[0];
tie my @arry, 'Tie::File', $filePath;
# 刪除第二行開始的三行內(nèi)容
# 如果這里的索引值越界, 對(duì)文件內(nèi)容沒(méi)有影響
splice @arry, 1, 3; 這里的處理主要利用了perl的Tie::File 模塊把數(shù)組和文件綁定,然后就可以使用perl的splice函數(shù)操作數(shù)組,從而達(dá)到操作文件的目的。對(duì)于Tie::File 模塊的用法,可以使用perldoc Tie::File 查詢到詳細(xì)說(shuō)明:
Perl 模塊 Tie::File應(yīng)用舉例
1 Tie::File
建立一個(gè)list和file的關(guān)系,對(duì)list的操作會(huì)反映到file上去。
use Tie::File;
tie @array, 'Tie::File', filename or die ...;
$array[13] = 'blah'; # line 13 of the file is now 'blah'
print $array[42]; # display line 42 of the file
$n_recs = @array; # how many records are in the file?
$#array -= 2; # chop two records off the end
for (@array) {
s/PERL/Perl/g; # Replace PERL with Perl everywhere in the file
}
# These are just like regular push, pop, unshift, shift, and splice
# Except that they modify the file in the way you would expect
push @array, new recs...;
my $r1 = pop @array;
unshift @array, new recs...;
my $r2 = shift @array;
@old_recs = splice @array, 3, 7, new recs...;
untie @array; # all finished2 應(yīng)用舉例
use Tie::File; my @array; my $filename = "a.txt"; tie(@array,'Tie::File',$filename) or die; print "$array[3] "; print "$#array "; $array[3] = "hello"; $array[1] = "world"; splice(@array, 1, 0, "insert into 0 and 1 line"); delete $array[$#array]; $#array -= 2; print pop @array; untie($array);
3 more help
perldoc Tie::File

對(duì)于splice函數(shù)可以使用 perldoc -f splice 查詢文檔:
Perl 函數(shù) splice
splice ARRAY, OFFSET, LENGTH, LIST splice ARRAY, OFFSET, LENGTH splice ARRAY, OFFSET splice ARRAY
這個(gè)函數(shù)從一個(gè) ARRAY 中刪除 OFFSET 和 LENGTH 指明的元素,并且,如果給出了LIST,則用 LIST 的元素替換它。如果 OFFSET 是負(fù)數(shù),那么該函數(shù)從數(shù)組的后面向前數(shù),但如果該值會(huì)伸到數(shù)組開頭的前面,那么就會(huì)拋出一個(gè)例外。在列表環(huán)境中,splice 返回從該數(shù)組中刪除的元素。在標(biāo)量環(huán)境中,它返回最后刪除的元素,而如果沒(méi)有的話返回 undef。如果新元素的數(shù)量不等于舊元素的數(shù)量,那么該數(shù)組根據(jù)需要伸縮,并且元素的位置根據(jù)銜接后的情況進(jìn)行改變。如果省略了 LENGTH,那么該函數(shù)從數(shù)組里刪除從 OFFSET 開始的所有東西。如果省略了 OFFSET,那么該數(shù)組在讀取的時(shí)候清空。下面的等式成立(假設(shè) $[ 為 0):
splice 函數(shù)還可以方便地用于切開傳遞給子過(guò)程的參數(shù)列表。比如,假設(shè)列表長(zhǎng)度在列表之前傳遞:
sub list_eq { # 比較兩個(gè)列表值
my @a = splice(@_, 0, shift);
my @b = splice(@_, 0, shift);
return 0 unless @a == @b; # 長(zhǎng)度相同?
while(@a) {
return 0 if pop(@a) ne pop(@b);
}
return 1;
}
if (list_eq($len, @foo[1..$len], scalar(@bar), @bar)) { ... }不過(guò),拿數(shù)組引用來(lái)干這事更清晰一些。

Perl 文本文件的讀寫操作、文件的重命名和刪除、多個(gè)文本文件的合并實(shí)現(xiàn)代碼
到此這篇關(guān)于如何使用perl的Tie::File 模塊刪除文件固定行的文章就介紹到這了,更多相關(guān)perl刪除文件固定行內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
通過(guò)perl實(shí)現(xiàn)一個(gè)簡(jiǎn)單的NIDS
使用perl實(shí)現(xiàn)一個(gè)簡(jiǎn)單的NIDS,有需要的朋友可以參考下2013-02-02
使用perl清理電腦上重復(fù)文件實(shí)現(xiàn)代碼(續(xù))
使有perl搜索電腦上的重復(fù)文件并刪除,需要的朋友可以參考下2013-02-02
perl 標(biāo)量和運(yùn)算符的一些知識(shí)介紹
有關(guān)perl的標(biāo)量和運(yùn)算符的一些知識(shí),有需要的朋友可以看看2013-02-02
Perl使用File::Basename獲取文件擴(kuò)展名的代碼
本文為大家介紹的這個(gè)例子,實(shí)現(xiàn)了獲取/home/topgkw中所有文件后綴,其中目錄返回空值2013-02-02

