perl - find a digit in $_ and add data to $_ in perl
问题描述
I am trying to read data from a file using File I/O in perl. I want to find an integer, compare it and add a integer to the line. My file consists of line like this.
file mfile.txt start_value 0 end_value 13000 errors 30
I want to compare the end_value and want to add 500 or 2000 after that end_value.
file mfile.txt start_value 0 end_value 13000 2000 errors 30
This edit has to be done in the same file. My code goes like this.
print "Enter file ";
$file= <STDIN>;
chomp ($file);
open ( DATA, "+<$file") || die "No file";
while(<DATA>) {
print "$_\n";
@line = split / /, $_;
print "$line[5]";
$epat = $line[5];
if ($epat > 10000) {
$line[6] = "2000 errors";
}
else {
$line[6] = "500 errors";
}
$_ = join (" ", @line);
print "$_";
print "\n";
}
close DATA
I tried to convert $_ to array and find the end_value by calling its index and compare it. It worked fine but how to write this new edit to the same line in the same file? After this the file is not changed. How to do that?
解决方案
You can use perl -i
to change the file in place, but you then need to populate @ARGV with the filename. It writes to another file and renames it back behind the scenes, anyway.
In fact, you can use just a one-liner:
perl -i~ -ape 's/(?=errors)/$F[5] > 10000 ? "2000 " : "500 "/e' -- filename
-i~
creates a backup of the file using the suffix~
-a
splits each line into the@F
array-p
reads the input or argument files line by line and prints each line after processings///e
does substitution, but interprets the replacement as code to run(?=errors)
is a look-ahead assertion, it matches where "errors" start, but doesn't replace the matching part? :
is the ternary operator, it corresponds to anif then else
on the expression level
推荐阅读
- php - PhpUnit 如何运行 --filter 与多个条件
- perl - 将 perl 的 .pm 文件制作为 .exe 文件
- asp.net-core - 依赖注入对象在服务器中存在多长时间(Scoped & Transient)
- r - ruragrch 包在 gjr garch 中使用虚拟变量
- node.js - 在 Windows 上通过 Github Actions 使用 Jest 时,日志中没有失败原因
- reactjs - 使用 reactn 时为令牌包装 fetch 函数
- android - Android 发布 apk commond 无法在颤振中工作
- python - matplotlib 中的指数项间距
- bootstrap-4 - 引导表页面刷新问题
- python - 从文本文件中提取步骤并使用 Python 放入列表中