为什么linux重定向截断文件? [英] Why a linux redirect truncates the file?

查看:654
本文介绍了为什么linux重定向截断文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为test.txt的文件:

I have a file called "test.txt" like that:

hello world

它只是一个hello world字符串。如果我使用perl regex:

it is, just a 'hello world' string. If I use a perl regex:

perl -pe "s/hello/bye/g" test.txt

它说:

bye world

但如果我尝试将该文件重定向到自身:

but if I try to redirect that file to itself:

perl -pe "s/hello/bye/g" test.txt > test.txt

生成的文件为空。为什么?如何在文件中过滤正则表达式?

the resulting file is empty. Why? And how can I 'filter' a regex over a file?

推荐答案

perl 接受inplace的参数 -i 。有了这个,你可以用一个perl程序处理一个文件,并立即将其回写。

perl accepts the parameter -i for inplace. With this, you can process a file with a perl program and immediately have it written back.

这篇关于为什么linux重定向截断文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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