使用 find 和 sed 将文件名添加到文件开头
使用以下内容,我将文件名添加到每行的前面,并将输出发送到单个文件。
ls | while read file; do sed -e "s/^/$file/g" $file > out; done
我想执行相同的 sed
替换,但使用 find
和 exec
或 xargs
命令 -
find . -type f -exec sed "s/^/{}/g" {} > out +
但我得到一个错误 -
find:-exec ... + 仅支持一个 {} 实例
输入文件如下 -
fileA.txt
A1
A2
fileB.txt
B1
B2
所需输出
fileA.txt A1
fileA.txt A2
fileB.txt B1
fileB.txt B2
我知道如何使用 awk 执行此操作,但我想这样做它与 sed、find 和 exec 或 xargs 一起使用。
using the following I add the file name to the front of each line and send the output to a single file.
ls | while read file; do sed -e "s/^/$file/g" $file > out; done
I want to perform the same sed
replacement but using a find
and exec
or xargs
command -
find . -type f -exec sed "s/^/{}/g" {} > out +
but I get an error -
find: Only one instance of {} is supported with -exec ... +
Input files are like this -
fileA.txt
A1
A2
fileB.txt
B1
B2
desired output
fileA.txt A1
fileA.txt A2
fileB.txt B1
fileB.txt B2
I know how to do this with awk, but I'd like to do it with sed, find and exec or xargs.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
当我回答这个问题时,你的“no awk”行还没有出现。无论如何,看看下面我更新的答案:
根据评论更新,
所以你想使用 find、exec/xargs 和 sed 来完成它。我的脚本需要 GNU Sed,我希望你有它。
先看一行:(
> out
被省略了,你可以将它添加到行尾。)现在我们来测试一下,如下:
结果是你期望的吗?
简短解释
find ....|xargs -i echo {}
没什么可解释的,只需打印每行文件名(以
"./"
开头)sed -r 's#(.\/)(.*)# MAGIC
#ge'
\1: "./"
和\2 "a.txt"(filename)
e
,所以 MAGIC 部分将是作为 shell 命令执行。(需要 GNU sed)
cat &\|sed "s:^:file \2 :g
cat & 只是输出文件内容,并通过管道传输到另一个 sed。 最后进行replace(
s:..:..:g
)外部 sed。
关键是 Gnu sed 的“e”。
as I answered this, your "no awk" line not yet there. anyway, take a look my updated answer below:
updated based on comment
so you want to use find, exec/xargs, and sed to do it. My script needs GNU Sed, i hope you have it.
see the one liner first: (well,
> out
is omitted. You could add it to the end of the line. )now let's take a test, see below:
is the result your expectation?
Short explanation
find ....|xargs -i echo {}
nothing to explain, just print thefilename per line (with leading
"./"
)sed -r 's#(.\/)(.*)# MAGIC
#ge'
\1: "./"
and\2 "a.txt"(filename)
e
at the end of sed line, the MAGIC part would beexecuted as shell command.(GNU sed needed)
cat &\|sed "s:^:file \2 :g
cat & is just output the filecontent, and pipe to another sed. do the replace (
s:..:..:g
)the outer sed.
the key is the 'e' of Gnu sed.
未经测试,尝试使用 xargs
untested, try using xargs
为什么不简单地用这样的
find
替换第一行中的ls
呢?您只能将
s
的分隔符从/
替换为文件名中未包含的其他内容。我选择了|
作为示例。Why don't you simply replace the
ls
in your first line with thefind
like this?You must only exchange the delimiter for
s
from/
to something else not contained in your filenames. I have chosen|
as an example.这个对我来说效果很好,而且比肯特的答案更容易使用
注意:比为该路径插入完整路径名
使用此路径来仅保留裸文件名部分
,然后如果您对标准输出结果感到满意,您可以将 -i 开关添加到 sed 命令以覆盖文件
this one works fine for me and it is simpler to use than Kent's answer
NOTE: than the full pathname is inserted for that one
use this one instead to keep only the bare filename part
then if your are happy with stdout results you might add -i switch to the sed command to overwrite the files
怎么样:
How about: