gpt4 book ai didi

perl - 使用 grep 和 perl 搜索和替换

转载 作者:行者123 更新时间:2023-12-05 01:15:22 26 4
gpt4 key购买 nike

我需要将下面所有文件中的“vi_chiron”替换为“vi_huracan”。我正在使用以下命令行,并且还更改了对所有基础文件的完全访问权限:

 grep -ri "vi_chiron" ./ | grep -v Header | xargs perl -e "s/vi_chiron/vi_huracan/" -pi

我收到错误: “无法打开 ./build/drivers_file.min:#: 没有这样的文件或目录。“和许多其他类似的错误。知道为什么吗?以下是该文件的权限:

          ll build/drivers_file.min 
-rwxrwxrwx 1 ask vi 5860 Mar 13 12:07 build/drivers_file.min

最佳答案

您可以通过以下方式更改您的命令:

grep -ril "vi_chiron" . | xargs grep -vl Header | xargs perl -e "s/vi_chiron/vi_huracan/" -pi 

find . -type f -exec grep -ril "vi_chiron" | xargs grep -vl Header | xargs perl -e "s/vi_chiron/vi_huracan/" -pi

为了操作所有不包含Header的文件,将vi_chiron修改为vi_hurican

如果我是你,我会简化链条并以相反的方式进行:

grep -rvl Header . | xargs sed -i.bak 's/vi_chiron/vi_hurican/'

如果您有足够的信心将 -i.bak 更改为 -i 以便 sed 不进行任何备份。

另请注意,您的更改和替换不是全局的,如果您想全局使用:sed -i.bak 's/vi_chiron/vi_hurican/g'

'-i[SUFFIX]' '--in-place[=SUFFIX]' This option specifies that files are to be edited in-place. GNU 'sed' does this by creating a temporary file and sending output to this file rather than to the standard output.(1).

 This option implies '-s'.

When the end of the file is reached, the temporary file is renamed
to the output file's original name. The extension, if supplied, is
used to modify the name of the old file before renaming the
temporary file, thereby making a backup copy(2)).

This rule is followed: if the extension doesn't contain a '*', then
it is appended to the end of the current filename as a suffix; if
the extension does contain one or more '*' characters, then _each_
asterisk is replaced with the current filename. This allows you to
add a prefix to the backup file, instead of (or in addition to) a
suffix, or even to place backup copies of the original files into
another directory (provided the directory already exists).

If no extension is supplied, the original file is overwritten
without making a backup.

来源:https://www.gnu.org/software/sed/manual/sed.txt

关于perl - 使用 grep 和 perl 搜索和替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49250467/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com