简体   繁体   English

需要帮助在Linux终端中使用sed编辑多个文件

[英]Need help editing multiple files using sed in linux terminal

I am trying to do a simple operation here. 我试图在这里做一个简单的操作。 Which is to cut a few characters from one file (style.css) do a find a replace on another file (client_custom.css) for more then 100 directories with different names 从一个文件(style.css)切几个字符,然后在另一个文件(client_custom.css)上找到一个替换,以查找超过100个名称不同的目录

When I use the following command 当我使用以下命令时

for d in */; do sed -n 73p ~/assets/*/style.css | cut -c  29-35 | xargs -I :hex:  sed -i 's/!BGCOLOR!/:hex:/' ~/assets/*/client_custom.css $d; done

It keeps giving me the following error for all the directories 它一直给我所有目录以下错误

sed: couldn't edit dirname/: not a regular file

I am confused on why its giving me that error message explicitly gave the full path to the file. 我对为什么它给我该错误消息显式给出了文件的完整路径感到困惑。 It works perfectly fine without a for loop . 没有for loop它工作得很好。

Can anyone please help me out with this issue? 谁能帮我解决这个问题?

sed doesn't support folders as input. sed不支持文件夹作为输入。

for d in */;

puts folders into $d . 将文件夹放入$d If you write sed ... $d , then BASH will put the folder name into the arguments of sed and the poor tool will be confused. 如果您编写sed ... $d ,则BASH会将文件夹名称放入sed的参数中,并且可怜的工具将被混淆。

Also ~/assets/*/client_custom.css since this will expand to all the files which match this pattern. ~/assets/*/client_custom.css因为它将扩展到所有与此模式匹配的文件。 So sed will be called once with all file names. 因此sed将使用所有文件名被调用一次。 You probably want to invoke sed once per file name. 您可能希望每个文件名调用一次sed

Try 尝试

for f in ~/assets/*/client_custom.css; do
    ... | sed -i 's/!BGCOLOR!/:hex:/' $f
done

or, even better: 或者,甚至更好:

for f in ~/assets/*/client_custom.css; do
    ... | sed 's/!BGCOLOR!/:hex:/' "${f}.in" > "${f}"
done

(which doesn't overwrite the output file). (不会覆盖输出文件)。 This way, you can keep the "*.in" files, edit them with the patterns and then use sed to "expand" all the variables. 这样,您可以保留“ * .in”文件,使用模式对其进行编辑,然后使用sed “扩展”所有变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM