linux 替换目录下文件所有关键字

简介:

for i in *;do sed -ie 's/_test2/_test3/g' $i; sed -ie 's/_type2/_type3/g' $i; done

 

 

539down voteaccepted

1. Replacing all occurrences of one string with another in all files in the current directory:

These are for cases where you know that the directory contains only regular files and that you want to process all non-hidden files. If that is not the case, use the approaches in 2.

All sed solutions in this answer assume GNU sed. If using FreeBSD or OS/X, replace -i with -i ''. Also note that the use of the -i switch with any version of sed has certain filesystem security implications and is inadvisable in any script which you plan to distribute in any way.

  • Non recursive, files in this directory only:

    sed -i -- 's/foo/bar/g' * perl -i -pe 's/foo/bar/g' ./*

    (the perl one will fail for file names ending in | or space)).

  • Recursive, regular files (including hidden ones) in this and all subdirectories

    find . -type f -exec sed -i 's/foo/bar/g' {} +

    If you are using zsh:

    sed -i -- 's/foo/bar/g' **/*(D.)

    (may fail if the list is too big, see zargs to work around).

    Bash can't check directly for regular files, a loop is needed (braces avoid setting the options globally):

    ( shopt -s globstar dotglob; for file in **; do if [[ -f $file ]] && [[ -w $file ]]; then sed -i -- 's/foo/bar/g' "$file" fi done )

    The files are selected when they are actual files (-f) and they are writable (-w).

 

4. Multiple replace operations: replace with different strings

  • You can combine sed commands:

    sed -i 's/foo/bar/g; s/baz/zab/g; s/Alice/Joan/g' file

    Be aware that order matters (sed 's/foo/bar/g; s/bar/baz/g' will substitute foo with baz).

  • or Perl commands

    perl -i -pe 's/foo/bar/g; s/baz/zab/g; s/Alice/Joan/g' file
  • If you have a large number of patterns, it is easier to save your patterns and their replacements in a sed script file:

    #! /usr/bin/sed -f
    s/foo/bar/g s/baz/zab/g
  • Or, if you have too many pattern pairs for the above to be feasible, you can read pattern pairs from a file (two space separated patterns, $pattern and $replacement, per line):

    while read -r pattern replacement; do sed -i "s/$pattern/$replacement/" file done < patterns.txt
  • That will be quite slow for long lists of patterns and large data files so you might want to read the patterns and create a sed script from them instead. The following assumes a <space> delimiter separates a list of MATCH<space>REPLACE pairs occurring one-per-line in the file patterns.txt :

    sed 's| *\([^ ]*\) *\([^ ]*\).*|s/\1/\2/g|' <patterns.txt | sed -f- ./editfile >outfile

    The above format is largely arbitrary and, for example, doesn't allow for a <space> in either ofMATCH or REPLACE. The method is very general though: basically, if you can create an output stream which looks like a sed script, then you can source that stream as a sed script by specifying sed's script file as -stdin.

  • You can combine and concatenate multiple scripts in similar fashion:

    SOME_PIPELINE |
    sed -e'#some expression script' \ -f./script_file -f- \ -e'#more inline expressions' \ ./actual_edit_file >./outfile

    A POSIX sed will concatenate all scripts into one in the order they appear on the command-line. None of these need end in a \newline.

  • grep can work the same way:

    sed -e'#generate a pattern list' <in | grep -f- ./grepped_file
  • When working with fixed-strings as patterns, it is good practice to escape regular expressionmetacharacters. You can do this rather easily:

    sed 's/[]$&^*\./[]/\\&/g
         s| *\([^ ]*\) *\([^ ]*\).*|s/\1/\2/g|
    ' <patterns.txt | sed -f- ./editfile >outfile

5. Multiple replace operations: replace multiple patterns with the same string

  • Replace any of foobar or baz with foobar

    sed -Ei 's/foo|bar|baz/foobar/g' file
  • or

    perl -i -pe 's/foo|bar|baz/foobar/g' file









本文转自张昺华-sky博客园博客,原文链接:http://www.cnblogs.com/bonelee/p/6428101.html ,如需转载请自行联系原作者



相关文章
|
16天前
|
运维 安全 Linux
Linux中传输文件文件夹的10个scp命令
【10月更文挑战第18天】本文详细介绍了10种利用scp命令在Linux系统中进行文件传输的方法,涵盖基础文件传输、使用密钥认证、复制整个目录、从远程主机复制文件、同时传输多个文件和目录、保持文件权限、跨多台远程主机传输、指定端口及显示传输进度等场景,旨在帮助用户在不同情况下高效安全地完成文件传输任务。
112 5
|
16天前
|
Linux Shell 数据库
Linux文件查找新姿势:总有一种你没见过
【10月更文挑战第18天】文件查找是Linux用户提升工作效率的重要技能。本文介绍了几种实用的文件查找方法,包括基础的`find`命令、快速的`locate`和`mlocate`、高效的`fd`工具、以及结合`grep`和`rg`进行内容搜索。此外,还提供了编写Shell脚本和使用图形界面工具的建议,帮助你更灵活地管理文件。
54 3
|
4天前
|
网络协议 Linux
linux系统重要文件目录
本文介绍了Linux系统中的重要目录及其历史背景,包括根目录、/usr、/etc、/var/log和/proc等目录的结构和功能。其中,/etc目录下包含了许多关键配置文件,如网卡配置、DNS解析、主机名设置等。文章还详细解释了各目录和文件的作用,帮助读者更好地理解和管理Linux系统。
18 2
|
3天前
|
缓存 监控 Linux
|
7天前
|
Linux Shell 数据库
文件查找是Linux用户日常工作的重要技能介绍了几种不常见的文件查找方法
文件查找是Linux用户日常工作的重要技能。本文介绍了几种不常见的文件查找方法,包括使用`find`和`column`组合、`locate`和`mlocate`快速查找、编写Shell脚本、使用现代工具`fd`、结合`grep`搜索文件内容,以及图形界面工具如`Gnome Search Tool`和`Albert`。这些方法能显著提升文件查找的效率和准确性。
26 2
|
10天前
|
Linux 数据库
linux 全局搜索文件
在 Linux 系统中,全局搜索文件常用 `find`、`locate` 和 `grep` 命令。`find` 根据文件名、类型、大小、时间戳等条件搜索;`locate` 通过预构建的数据库快速查找文件;`grep` 在文件中搜索特定文本,常与 `find` 结合使用。选择合适的命令取决于具体需求。
|
14天前
|
Linux 开发工具 Perl
Linux命令替换目录下所有文件里有"\n"的字符为""如何操作?
【10月更文挑战第20天】Linux命令替换目录下所有文件里有"\n"的字符为""如何操作?
30 4
|
13天前
|
运维 安全 Linux
Linux文件清空的五种方法总结分享
每种方法各有优势,选择最合适的一种或几种,可以极大提高您的工作效率。更多有关Linux系统管理的技巧与资源,欢迎访问,持续提升您的运维技能。
59 1
|
15天前
|
Linux
Linux系统之expr命令的基本使用
【10月更文挑战第18天】Linux系统之expr命令的基本使用
51 4
|
2天前
|
缓存 监控 Linux