英文:
Find and sed together fail in macOS
问题
这个命令会在删除exec
以后的部分中查找文件。
find src -name "*.tsx" -o -name "*.ts" -exec sed -i "" "s/LayerSelector/LayerRenderer/g" {} \;
LayerSelector
确实出现在大多数这些文件中,而-i
应该会就地替换。
然而,什么都没有发生。文件没有变化,也没有消息。
这个简单的命令有什么问题吗?
然而,这个命令可以正常工作:
sed -i "" "s/LayerSelector/LayerRenderer/g" src/**/*.tsx
英文:
This command finds files if I remove from exec
onwards.
find src -name "*.tsx" -o -name "*.ts" -exec sed -i "" "s/LayerSelector/LayerRenderer/g" {} \;
The LayerSelector
is indeed in most of those files, and the -i
should replace in place.
However, nothing happens. No changes in files, no message.
What is wrong with this simple command ?
However, this works fine:
sed -i "" "s/LayerSelector/LayerRenderer/g" src/**/*.tsx
答案1
得分: 3
您缺少-o
子句周围的括号:
find src \( -name "*.tsx" -o -name "*.ts" \) -exec sed -i "" "s/LayerSelector/LayerRenderer/g" {} \;
没有括号,由于操作符优先级,这个命令:
find src -name "*.tsx" -o -name "*.ts" -exec sed -i "" "s/LayerSelector/LayerRenderer/g" {} \;
与这个命令相同(这可能不是您想要的,因为它对*.tsx
文件什么都不做):
find src \( -name "*.tsx" \) -o \( -name "*.ts" -exec sed -i "" "s/LayerSelector/LayerRenderer/g" {} \; \)
另请参阅:
- 在
find
命令中使用多个-name
和-exec
只执行最后一个-name
匹配项 - Unix & Linux Stack Exchange find
命令中的操作符优先级? - Unix & Linux Stack Exchange
英文:
You are missing parentheses around the -o
clause:
find src \( -name "*.tsx" -o -name "*.ts" \) -exec sed -i "" "s/LayerSelector/LayerRenderer/g" {} \;
Without the parentheses, due to operator precedence, this:
find src -name "*.tsx" -o -name "*.ts" -exec sed -i "" "s/LayerSelector/LayerRenderer/g" {} \;
is the same as this (which is probably not what you want, because it does nothing to "*.tsx"
files):
find src \( -name "*.tsx" \) -o \( -name "*.ts" -exec sed -i "" "s/LayerSelector/LayerRenderer/g" {} \; \)
See also:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论