簡體   English   中英

在目錄和子目錄中查找並替換為sed

[英]Find and replace with sed in directory and sub directories

我運行此命令以在我的站點根目錄中的所有文件中查找所有出現的“apple”並將其替換為“orange”:

find ./ -exec sed -i 's/apple/orange/g' {} \;

但它不是 go 通過子目錄。

這個命令有什么問題?

以下是 find./ 的find./的一些行:

./index.php
./header.php
./fpd
./fpd/font
./fpd/font/desktop.ini
./fpd/font/courier.php
./fpd/font/symbol.php

您的find應該看起來像這樣,以避免將目錄名稱發送到sed

find ./ -type f -exec sed -i -e 's/apple/orange/g' {} \;

對於較大的 s&r 任務,使用 grep 和 xargs 會更好更快,例如;

grep -rl 'apples' /dir_to_search_under | xargs sed -i 's/apples/oranges/g'

這對我有用:

find ./ -type f -exec sed -i '' 's#NEEDLE#REPLACEMENT#' *.php {} \;

由於也有 macOS 人在閱讀這個(就像我一樣),所以下面的代碼對我有用(在 10.14 上)

egrep -rl '<pattern>' <dir> | xargs -I@ sed -i '' 's/<arg1>/<arg2>/g' @

使用-i-e的所有其他答案都不適用於 macOS。

資源

grep -e apple your_site_root/**/*.* -s -l | xargs sed -i "" "s|apple|orage|"

我認為我們可以用一行簡單的命令來做到這一點

for i in `grep -rl eth0 . 2> /dev/null`; do sed -i ‘s/eth0/eth1/’ $i; done

請參閱此頁面

在 Linux 操作系統中:

sed -i 's/textSerch/textReplace/g' namefile

如果“sed”不起作用,請嘗試:

perl -i -pe 's/textSerch/textReplace/g' namefile

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM