[英]How to make a script for two txt files with different names in perl
I want to make the same calculations in two similar files, but I do not want to double the code for each file nor to create two scripts for this.我想在两个相似的文件中进行相同的计算,但我不想将每个文件的代码加倍,也不想为此创建两个脚本。
my $file = "file1.txt";
my $tempfile = "file1_temp.txt";
if (not defined $file) {
die "Input file not found";
}
open(my $inputFileHandler, '<:encoding(UTF-8)', $file)
or die "Could not open file '$file' $!";
open(my $outs, '>', $tempfile) or die $!;
/*Calculations made*/
close($outs);
copy($tempfile,$file) or die "Copy failed: $!";
unlink($tempfile) or die "Could not delete the file!\n";
close($inputFileHandler);
So i want to do the exact calculations for file2.txt_temp and copy it in file2.txt is there a way to do it without writing the code again for file2?所以我想对 file2.txt_temp 进行精确计算并将其复制到 file2.txt 有没有办法在不为 file2 再次编写代码的情况下做到这一点?
Thank you very much.非常感谢。
Write your code as a Unix filter.将您的代码编写为 Unix 过滤器。 Read the data from STDIN
and write it to STDOUT
.从STDIN
读取数据并将其写入STDOUT
。 Your code will be simpler and your program will be more flexible.你的代码会更简单,你的程序会更灵活。
#!/usr/bin/perl
use strict;
use warnings;
while (<STDIN>) {
# do a calculation using the data that is in $_
print $output_data
}
The cleverness is in how you call the program:聪明之处在于您如何调用程序:
$ ./my_clever_filter < file1.txt > file1_out.txt
$ ./my_clever_filter < file2.txt > file2_out.txt
SeeThe Unix Filter Model: What, Why and How?请参阅Unix 过滤器模型:什么、为什么和如何? for far more information.了解更多信息。
Assuming your code is well written (not manipulating any globals, ...) you could use a for-loop假设你的代码写得很好(不操作任何全局变量,......)你可以使用 for 循环
foreach my $prefix ('file1', 'file2') {
my $file = $prefix . ".txt";
my $tempfile = $prefix . "_temp.txt";
...
}
There is a certain Perl feature that is designed especially for cases like this, and that is this:有一个 Perl 特性是专门为这样的情况设计的,那就是:
$ perl -pi -e'/*Calculations made*/' file1.txt file2.txt ... fileN.txt
Loosely referred to as "in-place edit", which basically does what your code does: It writes to a temp file and then overwrites the original.松散地称为“就地编辑”,它基本上执行您的代码所做的工作:它写入临时文件,然后覆盖原始文件。
Which will apply your calculations to the files named as arguments.这会将您的计算应用于命名为参数的文件。 If you have complex calculations you can put them in a file and skip the -e'....'
part如果您有复杂的计算,您可以将它们放在一个文件中并跳过-e'....'
部分
$ perl -pi foo.pl file1.txt ...
Say for example that your "calculations" consist of incrementing each pair of numbers by 1:举例来说,您的“计算”包括将每对数字增加 1:
s/(\d+) (\d+)/($1 + 1) . ($2 + 1)/ge
You would do either你会做任何一个
$ perl -pi -e's/(\d+) (\d+)/($1 + 1) . ($2 + 1)/ge' file1.txt file2.txt
$ perl -pi foo.pl file1.txt file2.txt
Where foo.pl
contains the code.其中foo.pl
包含代码。
Be aware that the -i
switch is destructive, so make backups before running the command.请注意, -i
开关具有破坏性,因此请在运行该命令之前进行备份。 You can supply a backup extension to save a backup, but that backup is overwritten if you run the command again.您可以提供备份扩展来保存备份,但如果再次运行该命令,该备份将被覆盖。 Eg -i.bak
.例如-i.bak
。
-p
places a while (<>)
loop around your code, followed by a print
of each line, -p
在代码周围放置一个while (<>)
循环,然后print
每一行,-i.bak
does the editing of the original file, and saves a backup with the extension, if it is supplied. -i.bak
对原始文件进行编辑,并保存带有扩展名的备份(如果提供)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.