首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将行插入多个指定文件中

将行插入多个指定文件中
EN

Stack Overflow用户
提问于 2013-11-12 05:53:04
回答 2查看 60关注 0票数 1

我想在多个指定类型文件的开头插入一行,这些文件位于当前目录或子目录中。

我知道用

代码语言:javascript
运行
复制
find . -name "*.csv"

可以帮助我列出我想要插入的文件。

并使用

代码语言:javascript
运行
复制
sed -i '1icolumn1,column2,column3' test.csv

可用于在文件开头插入一行,

但是现在我不知道如何将文件名从"find“命令传递到"sed”命令。

有人能给我什么建议吗?

或者有什么更好的解决办法来解决这个问题?

顺便说一句,在一行命令中这样做是有效的吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-11-12 05:56:09

尝试使用xargsfind和命令行参数的输出传递给下一个命令,此处为sed

代码语言:javascript
运行
复制
find . -type f -name '*.csv' -print0 | xargs -0 sed -i '1icolumn1,column2,column3'

另一个选项是使用-exec选项的find

代码语言:javascript
运行
复制
find . -type f -name '*.csv' -exec sed -i '1icolumn1,column2,column3' {} \;

注意:已经观察到,xargs是一种更有效的方法,可以使用-P选项处理多个进程。

票数 2
EN

Stack Overflow用户

发布于 2013-11-12 05:54:33

这条路:

代码语言:javascript
运行
复制
find . -type f -name "*.csv" -exec sed -i '1icolumn1,column2,column3' {} +

-exec在这里做了所有的魔术。man find的相关部分:

代码语言:javascript
运行
复制
   -exec command ;
   Execute  command;  true  if  0  status  is returned.  All following arguments
   to find are taken to be arguments to the command until an argument consisting
   of `;' is encountered.  The string `{}' is replaced by the current file name
   being processed everywhere it occurs in the arguments to the command, not just
   in arguments where it is alone, as in some versions  of find.   Both  of  
   these constructions might need to be escaped (with a `\') or quoted to protect
   them from expansion by the shell.  See the EXAMPLES section for examples of
   the use of the -exec option.  The specified command is run once for each
   matched file.  The command is executed in the starting directory.   There
   are unavoidable security  problems  surrounding use of the -exec action;
   you should use the -execdir option instead
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19921915

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档