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' 被截取文件>另存为文件