Perl从目录中的文件中替换字符串 [英] Perl replacing Strings from files in directory

查看:375
本文介绍了Perl从目录中的文件中替换字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图制作一个程序来替换目录中所有文件的字符串。问题的关键是,如果只是在句子的开头或结尾,或者两者兼而有之,我都需要改变它。这是我到目前为止:

  use strict; 
使用警告;
使用File :: Find; #这让我们使用find()函数,类似于Unix
#中的find,这对于递归搜索特别有用。

打印您要使用哪个目录?\ n; #询问要使用的目录
my $ dir = readline STDIN;
chomp $ dir; #用于消除换行符'\ n'

打印你想要搜索什么字符串?\ n; #询问要搜索的字符串。
my $ search = readline STDIN;
chomp $ search;

print你想用什么字符串替换?\\\
; #询问用什么来替换它。
my $ replace = readline STDIN;
chomp $ replace;

打印如果要在句子的开始处替换$ search吗?(y / n)\ n;
my $ beg = readline STDIN;
chomp $ beg;

打印如果在句子末尾,您想要替换$ search吗?(y / n)\ n;
my $ end = readline STDIN;
chomp $ end;


查找(\& txtrep,$ dir); #这个函数让我们遍历目录中的每个文件

txtrep {
if(-f和/.txt$/){#只有当它是一个普通的.txt文件
my $ file = $ _; #设置文件的名称,使用特殊的Perl变量
open(FILE,$ file);
my @lines =< FILE>; #将文件放入数组中,并分隔句子
my @ lines2 = split(。,@lines);
关闭FILE;

if($ beg eqy){
foreach我的$ slot(@ lines2){
$ slot =〜s / ^ $ search / $ replace / gi;


$ b $如果($ end eqy){
foreach我的$ slot(@ lines2){
$ slot =〜s / $搜索/ $替换/ GI;



open(FILE,> $ file);
打印FILE @ lines2;
关闭FILE;




$ b $ p
$ b

在我运行这个之后,它只是删除文件,而且我不知道这个语法是否适合改变字符串@句子的开头和结尾。请让我知道我做错了什么!谢谢!

解决方案

我只想使用perl单行程式

perl -i.bak -pe's / ^ [search] / [replace] / g;'[files]作为行的开始处
perl -i.bak -pe's / [search] $ / [替换] / g;'[文件]的行结束

[搜索]和[替换]是占位符。



'-i.bak'会替换之前的文件进行备份。

快速谷歌搜索: perl单行搜索& repalce示例


I'm trying to make a program that replaces a string in all files in a directory. The catch is that I need to make it possible to only change it if it is at the beginning of a sentence or at the end, or both. This is what I have so far:

use strict;
use warnings;
use File::Find;     #This lets us use the find() function, similar to 'find' in Unix 
                # -- which is especially helpful for searching recursively.

print "Which directory do you want to use?\n"; #Ask what directory to use
my $dir = readline STDIN;
chomp $dir; #Used for eliminating the newline character '\n'

print "What String would you like to search for?\n"; #Ask for what String to search for.
my $search = readline STDIN;
chomp $search;

print "What String would you like to replace it with?\n"; #Ask for what to replace it with.
my $replace = readline STDIN;
chomp $replace;

print "Would you like to replace $search if it is at the beginning of the sentence? (y/n) \n";
my $beg = readline STDIN;
chomp $beg;

print "Would you like to replace $search if it is at the end of the sentence? (y/n) \n";
my $end = readline STDIN;
chomp $end;


find(\&txtrep, $dir); #This function lets us loop through each file in the directory

sub txtrep  {
    if ( -f and /.txt$/) { # Proceeds only if it is a regular .txt file
        my $file = $_;  # Set the name of the file, using special Perl variable
        open (FILE , $file);
        my @lines = <FILE>; #Puts the file into an array and separates sentences
        my @lines2 = split(".", @lines);
        close FILE;

        if ($beg eq "y") {
            foreach my $slot (@lines2) {
                $slot =~ s/^$search/$replace/gi;
            }
        }

        if ($end eq "y") {
            foreach my $slot (@lines2) {
                $slot =~ s/$search$/$replace/gi;
            }
        }

        open (FILE, ">$file");
        print FILE @lines2;
        close FILE;
    }
}

After I run this, it just deletes everything in the files, and I don't know if the syntax is right for changing the string @ the beginning and the end of the sentences. Please let me know what I am doing wrong! Thank you!

解决方案

I would just use a perl one-liner

perl -i.bak -pe 's/^[search]/[replace]/g;' [files] for a beginning of a line perl -i.bak -pe 's/[search]$/[replace]/g;' [files] for the end of the line

[search] and [replace] are place holders.

the '-i.bak' will make a backup of the file prior to replacement.

quick google search: perl one-liner search & repalce example

这篇关于Perl从目录中的文件中替换字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆