AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / ubuntu / 问题 / 989142
Accepted
elanozturk
elanozturk
Asked: 2017-12-25 03:26:11 +0800 CST2017-12-25 03:26:11 +0800 CST 2017-12-25 03:26:11 +0800 CST

多个文件文本替换为 sed

  • 772

我尝试用sed但不知道如何用多个文件替换文本文件中的一些文本。
我用:

sed -i -- 's/SOME_TEXT/SOME_TEXT_TO_REPLACE/g /path/to/file/target_text_file

在处理多个文件之前,我使用以下命令在文本文件中打印了目标文本文件的路径:

find /path/to/files/ -name "target_text_file" > /home/user/Desktop/target_files_list.txt

现在我想sed按照target_files_list.txt.

bash
  • 3 3 个回答
  • 9066 Views

3 个回答

  • Voted
  1. Best Answer
    Arkadiusz Drabczyk
    2017-12-25T03:42:01+08:002017-12-25T03:42:01+08:00

    您可以使用循环遍历文件while ... do:

    $ while read i; do printf "Current line: %s\n" "$i"; done < target_files_list.txt
    

    在你的情况下,你应该printf ...用sed你想要的命令替换。

    $ while read i; do sed -i -- 's/SOME_TEXT/SOME_TEXT_TO_REPLACE/g' "$i"; done < target_files_list.txt
    

    但是,请注意,您只能使用以下方法实现您想要的find:

    $ find /path/to/files/ -name "target_text_file" -exec sed -i -- 's/SOME_TEXT/SOME_TEXT_TO_REPLACE/g' {} \;
    

    您可以通过运行阅读更多关于-exec选项的信息man find | less '+/-exec ':

       -exec command ;
    
              Execute command; true if 0 status is returned.  All
              following arguments to find are taken to be arguments to
              the command until an argument consisting of `;' is
              encountered.  The string `{}' is replaced by the current
              file name being processed everywhere it occurs in the
              arguments to the command, not just in arguments where it
              is alone, as in some versions of find.  Both of these
              constructions might need to be escaped (with a `\') or
              quoted to protect them from expansion by the shell.  See
              the EXAMPLES section for examples of the use of the
              -exec option.  The specified command is run once for
              each matched file.  The command is executed in the
              starting directory.  There are unavoidable security
              problems surrounding use of the -exec action; you should
              use the -execdir option instead.
    

    编辑:

    正如用户 terdon和甜点在评论中正确指出的那样,必须使用-rwith ,read因为它可以正确处理反斜杠。它也被报道shellcheck:

    $ cat << EOF >> do.sh
    #!/usr/bin/env sh
    while read i; do printf "$i\n"; done < target_files_list.txt
    EOF
    $ ~/.cabal/bin/shellcheck do.sh
    
    In do.sh line 2:
    while read i; do printf "\n"; done < target_files_list.txt
          ^-- SC2162: read without -r will mangle backslashes.
    

    所以应该是:

    $ while read -r i; do sed -i -- 's/SOME_TEXT/SOME_TEXT_TO_REPLACE/g' "$i"; done < target_files_list.txt
    
    • 7
  2. steeldriver
    2017-12-25T05:40:15+08:002017-12-25T05:40:15+08:00

    一种方法是使用xargs:

    xargs -a target_files_list.txt -d '\n' sed -i -- 's/SOME_TEXT/TEXT_TO_REPLACE/g'
    

    来自man xargs:

       -a file, --arg-file=file
              Read items from file instead of standard input.  
    
       --delimiter=delim, -d delim
              Input  items  are  terminated  by  the specified character.  The
              specified delimiter may be a single character, a C-style charac‐
              ter  escape  such as \n, or an octal or hexadecimal escape code.
    
    • 3
  3. SilverWolf
    2017-12-25T08:05:22+08:002017-12-25T08:05:22+08:00

    只需使用一个for循环。

    IFS=$'\n' # Very important! Splits files on newline instead of space.
    
    for file in $(cat files.txt); do
        sed ...
    done
    

    请注意,如果您遇到名称中带有换行符 (!) 的任何文件,您将遇到问题。(:

    • 2

相关问题

  • 同时复制到两个位置

  • 如何在 shell 脚本中创建选择菜单?

  • 从 bash 迁移到 zsh [关闭]

  • bashrc 还是 bash_profile?

  • 备份 bash 脚本未压缩其 tarball

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何运行 .sh 脚本?

    • 16 个回答
  • Marko Smith

    如何安装 .tar.gz(或 .tar.bz2)文件?

    • 14 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Martin Hope
    Flimm 如何在没有 sudo 的情况下使用 docker? 2014-06-07 00:17:43 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve