我需要做一个正则表达式查找并替换文件夹(及其子文件夹)中的所有文件。linux shell命令将执行该操作吗?
例如,我要在所有文件上运行此文件,并用替换后的新文本覆盖旧文件。
sed 's/old text/new text/g' 
    Answers:
没有办法仅使用sed做到这一点。您至少需要一起使用find实用程序:
find . -type f -exec sed -i.bak "s/foo/bar/g" {} \;
此命令将为.bak每个更改的文件创建一个文件。
笔记:
-i参数sed是GNU扩展,因此,如果您正在与BSD一起运行此命令,sed则需要将输出重定向到新文件,然后重命名。find实用程序不会-exec在旧的UNIX框中实现该参数,因此,您将需要使用a | xargs代替。\;为了什么
                    -i它本身不会创建备份文件,而是导致sed在适当位置执行文件操作的原因。
                    {}为了什么?
                    {}会由发现的每个文件名进行更换find,并\;告诉找到的命令,他需要在这一点上,执行完成。
                    我更喜欢使用find | xargs cmd过find -exec因为它更容易记住。
此示例在当前目录或以下目录的.txt文件中用“ bar”全局替换“ foo”:
find . -type f -name "*.txt" -print0 | xargs -0 sed -i "s/foo/bar/g"
该-print0和-0选项可以被排除在外,如果你的文件名不包含时髦的字符,如空格。
find . -type f -name "*.txt" -print0 | xargs -0 sed -i '' "s/foo/bar/g"(注意为-i参数提供一个空字符串)。
                    为了可移植性,我不依赖sed特定于linux或BSD的功能。取而代之的是,我使用overwriteKernighan和Pike在Unix编程环境上的书中的脚本。
该命令是
find /the/folder -type f -exec overwrite '{}' sed 's/old/new/g' {} ';'
和overwrite脚本(这是我使用所有的地方)是
#!/bin/sh
# overwrite:  copy standard input to output after EOF
# (final version)
# set -x
case $# in
0|1)        echo 'Usage: overwrite file cmd [args]' 1>&2; exit 2
esac
file=$1; shift
new=/tmp/$$.new; old=/tmp/$$.old
trap 'rm -f $new; exit 1' 1 2 15    # clean up files
if "$@" >$new               # collect input
then
    cp $file $old   # save original file
    trap 'trap "" 1 2 15; cp $old $file     # ignore signals
          rm -f $new $old; exit 1' 1 2 15   # during restore
    cp $new $file
else
    echo "overwrite: $1 failed, $file unchanged" 1>&2
    exit 1
fi
rm -f $new $old
其思想是仅在命令成功的情况下才覆盖文件。find在您不想使用的地方也有用
sed 's/old/new/g' file > file  # THIS CODE DOES NOT WORK
因为Shell会截断文件,然后sed才能读取文件。
for i in $(ls);do sed -i 's/old_text/new_text/g' $i;done 
    可能想尝试我的大量搜索/替换Perl脚本。与链式实用程序解决方案相比,它具有一些优势(例如,不必处理多个级别的外壳元字符解释)。
#!/usr/bin/perl
use strict;
use Fcntl qw( :DEFAULT :flock :seek );
use File::Spec;
use IO::Handle;
die "Usage: $0 startdir search replace\n"
    unless scalar @ARGV == 3;
my $startdir = shift @ARGV || '.';
my $search = shift @ARGV or
    die "Search parameter cannot be empty.\n";
my $replace = shift @ARGV;
$search = qr/\Q$search\E/o;
my @stack;
sub process_file($) {
    my $file = shift;
    my $fh = new IO::Handle;
    sysopen $fh, $file, O_RDONLY or
        die "Cannot read $file: $!\n";
    my $found;
    while(my $line = <$fh>) {
        if($line =~ /$search/) {
            $found = 1;
            last;
        }
    }
    if($found) {
        print "  Processing in $file\n";
        seek $fh, 0, SEEK_SET;
        my @file = <$fh>;
        foreach my $line (@file) {
            $line =~ s/$search/$replace/g;
        }
        close $fh;
        sysopen $fh, $file, O_WRONLY | O_TRUNC or
            die "Cannot write $file: $!\n";
        print $fh @file;
    }
    close $fh;
}
sub process_dir($) {
    my $dir = shift;
    my $dh = new IO::Handle;
    print "Entering $dir\n";
    opendir $dh, $dir or
        die "Cannot open $dir: $!\n";
    while(defined(my $cont = readdir($dh))) {
        next
            if $cont eq '.' || $cont eq '..';
        # Skip .swap files
        next
            if $cont =~ /^\.swap\./o;
        my $fullpath = File::Spec->catfile($dir, $cont);
        if($cont =~ /$search/) {
            my $newcont = $cont;
            $newcont =~ s/$search/$replace/g;
            print "  Renaming $cont to $newcont\n";
            rename $fullpath, File::Spec->catfile($dir, $newcont);
            $cont = $newcont;
            $fullpath = File::Spec->catfile($dir, $cont);
        }
        if(-l $fullpath) {
            my $link = readlink($fullpath);
            if($link =~ /$search/) {
                my $newlink = $link;
                $newlink =~ s/$search/$replace/g;
                print "  Relinking $cont from $link to $newlink\n";
                unlink $fullpath;
                my $res = symlink($newlink, $fullpath);
                warn "Symlink of $newlink to $fullpath failed\n"
                    unless $res;
            }
        }
        next
            unless -r $fullpath && -w $fullpath;
        if(-d $fullpath) {
            push @stack, $fullpath;
        } elsif(-f $fullpath) {
            process_file($fullpath);
        }
    }
    closedir($dh);
}
if(-f $startdir) {
    process_file($startdir);
} elsif(-d $startdir) {
    @stack = ($startdir);
    while(scalar(@stack)) {
        process_dir(shift(@stack));
    }
} else {
    die "$startdir is not a file or directory\n";
}
    如果文件夹中的文件名具有一些常规名称(如file1,file2 ...),我已将其用于循环。
for i in {1..10000..100}; do sed 'old\new\g' 'file'$i.xml > 'cfile'$i.xml; done