1. 刪除包含字符串的整行,模糊匹配
perl -p -i -e 's/^.*(str).*\n$//g' dir
^:匹配至一行開始,\n之後的位置;
.:匹配任意單個字符;
*:匹配前面的字符無限次;
():指定匹配的pattern;
$:匹配至一行結束,\n之前的位置;
2. 替換指定字符串,精確匹配
perl -p -i -e 's/str1/str2/g' dir
將dir中str1替換爲str2.
3. perl腳本:多個文件逐行合併
#!/bin/perl
use strict;
use warnings;
open IN1,"<","./test1.txt";
open IN2,"<","./test2.txt";
open IN3,"<","./test3.txt";
open OUT,">","./testbig.txt";
while(my $line1 = <IN1> and my $line2 = <IN2> and my $line3 = <IN3>) {
chomp $line1;
chomp $line2;
chomp $line3;
printf OUT ("%-8s%-8s%-8s\n", $line1, $line2, $line3);
}
4. 長文件按行數截取
sed -n '開始行數, 結束行數p' 被截取文件>另存爲文件