如何在 Perl 中修剪文件的内容?
我想在 Perl 中将文件的内容从某个字符删除到文件中的某个字符。我如何使用脚本来做到这一点?
文件有这样的:
Syslog logging: enabled (11 messages dropped, 2 messages rate-limited,
0 flushes, 0 overruns, xml disabled, filtering disabled)
Console logging: level informational, 81 messages logged, xml disabled,
filtering disabled
Monitor logging: level debugging, 0 messages logged, xml disabled,
filtering disabled
Buffer logging: level informational, 28 messages logged, xml disabled,
filtering disabled
Logging Exception size (4096 bytes)
Count and timestamp logging messages: disabled
No active filter modules.
Trap logging: level informational, 83 message lines logged
Log Buffer (4096 bytes):
*Oct 4 13:42:03.210: %SEC_LOGIN-5-LOGIN_SUCCESS: Login Success [user: ] [Source: ] [localport: ] at UTC Mon Oct 4 2010
修剪后的新文件应该是这样的
*Oct 4 13:42:03.210: %SEC_LOGIN-5-LOGIN_SUCCESS: Login Success [user: ] [Source: ] [localport: ] at UTC Mon Oct 4 2010
I would like to remove contents of a file from a certain character to a certain character in the file in Perl. How do I do that using a script?
The file has this:
Syslog logging: enabled (11 messages dropped, 2 messages rate-limited,
0 flushes, 0 overruns, xml disabled, filtering disabled)
Console logging: level informational, 81 messages logged, xml disabled,
filtering disabled
Monitor logging: level debugging, 0 messages logged, xml disabled,
filtering disabled
Buffer logging: level informational, 28 messages logged, xml disabled,
filtering disabled
Logging Exception size (4096 bytes)
Count and timestamp logging messages: disabled
No active filter modules.
Trap logging: level informational, 83 message lines logged
Log Buffer (4096 bytes):
*Oct 4 13:42:03.210: %SEC_LOGIN-5-LOGIN_SUCCESS: Login Success [user: ] [Source: ] [localport: ] at UTC Mon Oct 4 2010
And the new file after the trimming should be this
*Oct 4 13:42:03.210: %SEC_LOGIN-5-LOGIN_SUCCESS: Login Success [user: ] [Source: ] [localport: ] at UTC Mon Oct 4 2010
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您没有提供足够的信息。但这听起来像是可以从命令行完成的事情。您的解决方案可能如下所示:
更新: 看过您的扩展说明后,您似乎只需要以星号开头的行。
You don't give anywhere near enough information. But it sounds like something that can be done from the command line. Your solution will probably look something like this:
Update: Having seen your expanded explanation, it looks like you only want lines that start with an asterisk.
如果我很了解您的需求,您可以执行以下操作:
我已根据您的新规范进行了更新
此脚本读取文件
foo.txt
并将行写入文件bar.txt 除了
begin_skip
和end_skip
之间的内容。begin_kip
和end_skip
可以是任何正则表达式。If i well understand your needs, you can do something like :
I've updated according to your new specifications
This script reads the file
foo.txt
and write lines into filebar.txt
except those betweenbegin_skip
andend_skip
included.begin_kip
andend_skip
can be any regex.嗯,问题不是很清楚。我猜你想删除文件中的某些字符串。
我猜你所期待的是这里的“$line =~ s/$a.*$b/$a$b/g;”它只替换字符串之间的内容,而不替换实际的字符串。你可以简单地调用 sed 脚本
Hmm, the question is not very clear. I guess you want to delete certain strings in a file.
I guess what you are expecting is in here "$line =~ s/$a.*$b/$a$b/g;" It only replaces the content between the string but not the actual string. You could simply call sed script