用于多个替换的单个 sed 命令? [英] Single sed command for multiple substitutions?
问题描述
我使用 sed 来替换文件中的文本.我想给 sed 一个文件,其中包含要在给定文件中搜索和替换的所有字符串.
I use sed to substitute text in files. I want to give sed a file which contains all the strings to be searched and replaced in a given file.
它遍历 .h 和 .cpp 文件.在每个文件中,它搜索包含在其中的文件名.如果找到,它会用<a.h>"替换例如a.h"(没有引号).
It goes over .h and .cpp files. In each file it searches for file names which are included in it. If found, it substitutes for example "a.h" with "<a.h>" (without the quotes).
脚本是这样的:
For /F %%y in (all.txt) do
for /F %%x in (allFilesWithH.txt) do
sed -i s/\"%%x\"/"\<"%%x"\>"/ %%y
- all.txt - 在其中进行替换的文件列表
- allFilesWithH.txt - 要搜索的所有包含名称
我不想多次运行 sed(作为 input.txt 中文件名的数量.)但我想运行一个 sed 命令并将它的 input.txt 作为输入传递.
I don't want to run sed several times (as the number of files names in input.txt.) but I want to run a single sed command and pass it input.txt as input.
我该怎么做?
P.S 我从 VxWorks Development shell 运行 sed,所以它没有 Linux 版本的所有命令.
P.S I run sed from VxWorks Development shell, so it doesn't have all the commands that the Linux version does.
推荐答案
sed
本身没有能力从文件中读取文件名.我不熟悉 VxWorks shell,我想这与缺乏答案有关......所以这里有一些可以在 bash 中工作的东西 - 也许 VxWorks 会支持其中之一.
sed
itself has no capability to read filenames from a file. I'm not familiar with the VxWorks shell, and I imagine this is something to do with the lack of answers... So here are some things that would work in bash - maybe VxWorks will support one of these things.
sed -i 's/.../...' `cat all.txt`
sed -i 's/.../...' $(cat all.txt)
cat all.txt | xargs sed -i 's/.../...'
实际上,如果它完成工作,多次调用 sed
没什么大不了的:
And really, it's no big deal to invoke sed
several times if it gets the job done:
cat all.txt | while read file; do sed -i 's/.../.../' $file; done
for file in $(cat all.txt); do # or `cat all.txt`
sed -i 's/.../.../' $file
done
这篇关于用于多个替换的单个 sed 命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!