Linux命令替换目录下一组文件的行集

标签 linux unix sed file-handling

我只需要替换选定的250个erlang文件(扩展名为.erl)的前4个标题行,但是目录+子目录中总共有400个erlang文件,我需要避免修改不需要的文件改变。 我有要修改的文件名列表,但不知道如何让我的 linux 命令使用它们。

sed -i '1s#.*#%% This Source Code Form is subject to the terms of the Mozilla Public#' *.erl
sed -i '2s#.*#%% License, v. 2.0. If a copy of the MPL was not distributed with this file,#' *.erl
sed -i '3s#.*#%% You can obtain one at http://mozilla.org/MPL/2.0/.#' *.erl
sed -i '4s#.*##' *.erl

在上面的命令中,我不想传递 *.erl,而是传递那些我需要修改的文件名列表,一个一个地做,我将花费超过 3 天的时间来完成它。

有什么办法吗?

最佳答案

使用 awk 遍历入围文件名并使用 xargs 执行 sed。您可以使用 -e 选项对一个文件执行多个 sed 命令。

awk '{print $1}' your_shortlisted_file_lists  | xargs sed -i -e first_sed -e second_sed $1

xargs$1 变量中从 awk 获取文件名。

关于Linux命令替换目录下一组文件的行集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36564737/

相关文章:

c - linux/windows 上的宽字符字符串函数

c++ - 如何创建 `DOS/Unix` 类似 GUI

sed 替换未知字符串的位置匹配除以用户定义的分隔符

linux - 使用 awk 从文件中读取特定文本

c - 具有等待队列挂起系统的Linux驱动程序代码

linux - 是否可以获得仅包含调用堆栈、线程和局部变量的 Linux 核心转储?

linux - Bash脚本帮助,包含处理文件的子目录

linux - 仅当文件尚不存在时才将行附加到文件中

arrays - C-shell : How to create multiple arrays from a single line of standard input?

linux - 剪切命令基础知识