使用命令行从文本文件中删除行

时间:2013-10-08 01:24:02

标签: perl shell command-line command

我有一个文本文件,需要删除其中不包含http的所有行。或者,它可以将包含http的所有文件输出到新文件中。

原始文件的名称是list.txt,我需要生成一个名为new.txt的新文件

我知道有几种方法可以通过命令行执行此操作,但我真正想要的是最快的方式,因为我需要使用多个文件来执行此操作,并且每个文件都是一些大小的演出。

3 个答案:

答案 0 :(得分:2)

最快,最短的解决方案,

fgrep -v "http"

当然,grep,egrep,awk,perl等使其更具可替代性。

这是一个简短的shell脚本。编辑包含的“delhttp.sh”,

#!/bin/bash
if [ $# -eq 0 ] ; then
    fgrep -v "http"
elif [ $# -eq 1 ] ; then
    f1=${1:-"null"}
    if [ ! -f $f1 ]; then echo "file $f1 dne"; exit 1; fi
    fgrep -v "http" $f1 #> $f2
elif [ $# -eq 2 ]; then
    f1=${1:-"null"}
    if [ ! -f $f1 ]; then echo "file $f1 dne"; exit 1; fi
    f2=${2:-"null"}
    fgrep -v "http" $f1 > $f2
fi

然后使用

使该文件可执行
chmod +x delhttp.sh

这是一个perl脚本(如果你愿意),编辑“delhttp.pl”包含,

#!/bin/env perl
use strict;
use warnings;
my $f1=$ARGV[0]||"-";
my $f2=$ARGV[1]||"-";
my ($fh, $ofh);
open($fh,"<$f1") or die "file $f1 failed";
open($ofh,">$f2") or die "file $f2 failed";
while(<$fh>) { if( !($_ =~ /http/) ) { print $ofh "$_"; } }

再次,使用

使该文件可执行
chmod +x delhttp.pl

答案 1 :(得分:2)

perl -i -lne 'print if(/http/)' your_file

如果没有http,上面的命令将删除文件中的所有行。 如果你坚持保留原始文件备份,你可以无论如何给出和选择“.bak”,如下所述:

perl -i.bak -lne 'print if(/http/)' your_file

由此生成your_file.bak,它只是原始文件的副本,原始文件将根据您的需要进行修改。 你也可以使用awk:

awk '/http/' your_file

这将输出到控制台。无论如何你可以使用'&gt;'将输出存储在新文件中。

答案 2 :(得分:1)

你可以使用grep。使用-v反转匹配感,选择不匹配的行。

grep -v 'http' list.txt

使用Perl one-liner:

perl -ne '/^(?:(?!http).)*$/ and print' list.txt > new.txt