【问题标题】:How to find a specific file recursively and delete the last line from all files found in linux?如何递归查找特定文件并从linux中找到的所有文件中删除最后一行?
【发布时间】:2020-11-07 19:43:47
【问题描述】:

我想递归查找特定文件e.g. foo.txt,如果该行包含特定单词(e.g. bar),则删除其中的最后一行。基本结合-

find . -type f -iname "foo.txt"
sed '$d' foo.txt
dir1 -
      |-sub-dir1-
                 |-foo.txt
      |-sub-dir2-
                 |-foo.txt
      |-
      .
      .
      |-
      |-sub-dirn-
                 |-foo.txt

如果最后一行包含bar,则从dir1 下的所有foo.txt 文件中删除最后一行。

原创内容-

foo.txt
1
2
bar

删除后-

foo.txt
1
2

谢谢。

【问题讨论】:

  • SO 上的人不会太客气地要求解决某个问题——我建议您先自己尝试一下,然后再询问您是否还有问题。如果您已经尝试过,请在帖子中添加您的尝试和问题所在。

标签: linux unix sed


【解决方案1】:

使用-exec 选项运行sed 命令。添加 /bar/ 正则表达式以检查最后一行是否也匹配该模式。

find . -type f -iname "foo.txt" -exec sed -i '${/bar/d;}' {} +

【讨论】:

    猜你喜欢
    • 2014-06-21
    • 2010-09-27
    • 1970-01-01
    • 1970-01-01
    • 2020-02-26
    • 2014-04-09
    • 2014-05-20
    • 1970-01-01
    • 2013-12-22
    相关资源
    最近更新 更多