首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

在Perl中,如何从文件中删除^ M?

在Perl中,可以使用正则表达式和文件操作来删除文件中的^M字符。以下是一个示例代码:

代码语言:perl
复制
#!/usr/bin/perl

use strict;
use warnings;

# 打开文件
open(my $input_fh, "<", "input.txt") or die "Can't open input.txt for reading: $!";
open(my $output_fh, ">", "output.txt") or die "Can't open output.txt for writing: $!";

# 读取文件内容并替换^M字符
while (my $line = <$input_fh>) {
    $line =~ s/\r//g;
    print $output_fh $line;
}

# 关闭文件
close($input_fh) or die "Can't close input.txt: $!";
close($output_fh) or die "Can't close output.txt: $!";

在这个示例中,我们首先打开了一个名为input.txt的文件,并将其内容读取到$input_fh变量中。然后,我们创建了一个名为output.txt的新文件,并将其内容写入$output_fh变量中。接下来,我们使用正则表达式s/\r//g来删除每一行中的^M字符,并将结果写入output.txt文件中。最后,我们关闭了两个文件。

需要注意的是,在Perl中,\r表示回车符,因此我们使用s/\r//g来匹配并删除^M字符。另外,在这个示例中,我们将结果写入了一个新的文件output.txt中,而不是直接修改原始文件。这是为了确保我们不会意外地覆盖原始数据。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

  • 《Perl语言入门》——读书笔记

    Perl语言入门 /** * prism.js Github theme based on GitHub's theme. * @author Sam Clarke */ code[class*="language-"], pre[class*="language-"] { color: #333; background: none; font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace; text-align: left; white-space: pre; word-spacing: normal; word-break: normal; word-wrap: normal; line-height: 1.4; -moz-tab-size: 8; -o-tab-size: 8; tab-size: 8; -webkit-hyphens: none; -moz-hyphens: none; -ms-hyphens: none; hyphens: none; } /* Code blocks */ pre[class*="language-"] { padding: .8em; overflow: auto; /* border: 1px solid #ddd; */ border-radius: 3px; /* background: #fff; */ background: #f5f5f5; } /* Inline code */ :not(pre) > code[class*="language-"] { padding: .1em; border-radius: .3em; white-space: normal; background: #f5f5f5; } .token.comment, .token.blockquote { color: #969896; } .token.cdata { color: #183691; } .token.doctype, .token.punctuation, .token.variable, .token.macro.property { color: #333; } .token.operator, .token.important, .token.keyword, .token.rule, .token.builtin { color: #a71d5d; } .token.string, .token.url, .token.regex, .token.attr-value { color: #183691; } .token.property, .token.number, .token.boolean, .token.entity, .token.atrule, .token.constant, .token.symbol, .token.command, .token.code { color: #0086b3; } .token.tag, .token.selector, .token.prolog { color: #63a35c; } .token.function, .token.namespace, .token.pseudo-element, .token.class, .token.class-name, .token.pseudo-class, .token.id, .token.url-reference .token.variable, .token.attr-name { color: #795da3; } .token.entity { cursor: help; } .token.title, .token.title .token.punctuation { font-weight: bold; color: #1d3e81; } .token.list { color: #ed6a43; } .token.inserted { background-color: #eaffea; color: #55a532; } .token.deleted { background-color: #ffecec; color: #bd2c00; } .token.bold { font-weight: bold; } .token.italic { font-style: italic; } /* JSON */ .lan

    02

    如何使用Kubernetes Job运行一次性任务

    在 kubernetes 中,Deployment、DaemonSet会持续运行任务,这些 pod 中的进程在崩溃退出时会重新启动,永远达不到完成态。你也许会遇到这样的场景,当需要运行一个一次性的可完成的任务,其进程终止后,不应该再重新启动,那么 Job 资源类型完全符合你。Kubernetes 中通过 Job 资源提供了对此的支持,它允许你运行一种 pod,该 pod 在内部进程成功结束时,不重启容器。一旦任务完成,pod 就被认为处于完成状态。在发生节点故障时,该节点上由 Job 管理的 pod 将按照 ReplicaSet 的 pod 的方式, 重新安排到其他节点,以确保任务能够成功完成,所以 Job 通常用于执行一次性任务或批处理作业。Job 还可以控制 Pod 的数量,确保一定数量的 Pod 成功完成任务。Job 的一些常用使用场景:

    01
    领券