加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

Linux:如何同时使用文件作为输入和输出?

发布时间:2020-05-23 16:08:36 所属栏目:Linux 来源:互联网
导读:我刚刚在bash中运行以下代码: uniq .bash_history .bash_history 我的历史档案最终完全空了. 我想在写入之前我需要一种方法来读取整个文件. 怎么做的? PS:我显然想过使用临时文件,但我正在寻找更优雅的解决方案. 我建议使用 moreutils的海绵.从联机帮助页

我刚刚在bash中运行以下代码:

uniq .bash_history > .bash_history

我的历史档案最终完全空了.

我想在写入之前我需要一种方法来读取整个文件.
怎么做的?

PS:我显然想过使用临时文件,但我正在寻找更优雅的解决方案.

解决方法

我建议使用 moreutils的海绵.从联机帮助页:
DESCRIPTION
  sponge  reads  standard  input  and writes it out to the specified file. Unlike
  a shell redirect,sponge soaks up all its input before opening the output file.
  This allows for constructing pipelines that read from and write to the same 
  file.

要将此问题应用于您的问题,请尝试:

uniq .bash_history | sponge .bash_history

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读