本文介绍了使用find和sed将文件名添加到文件的开头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 使用以下我将文件名称添加到每行的前面,并将输出发送到单个文件。 ls |同时读取文件;做sed -es / ^ / $ file / g$ file>出;完成 我想执行相同的 sed 替换,但使用 find 和 exec 或 xargs 命令 - find。 -type f -exec seds / ^ / {} / 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。 解决方案 find。 -type f | xargs awk'$ 0 = FILENAME $ 0'>出 当我回答这个问题时,你的不awk行还没有。无论如何,请看下面我更新的答案: 根据评论更新 所以你想用find,exec / xargs和sed来做。我的脚本需要 GNU Sed ,我希望你能拥有它。 首先看一个班轮:(好,> ; out 被省略了,你可以把它添加到行尾。) find。 -type f | xargs -i echo {} | sed -r's#(。\ /)(.*)# cat& \ | seds:^:file \ 2:g#ge' 现在让我们进行一个测试,见下: kent $ head * .txt ==> a.txt< == A1 A2 ==> b.txt< == B1 B2 kent $ find。 -type f | xargs -i echo {} | sed -r's#(。\ /)(.*)# cat& \ | seds:^:file \ 2:g#ge'文件b.txt B1 文件b.txt B2 文件a.txt A1 文件a.txt A2 是你期望的结果吗? 简短的解释 find .... | xargs -i echo {} 没有什么需要解释的,只需要在每行打印文件名(前导./ code>) 然后将文件名传递给一个sed行,如 sed -r's#(。\ / /((*)) #ge' 请记住,在上面的行中,我们有两个组 \ 1:./ 和 \ 2a.txt(文件名) 在$ sed行结尾的 e , MAGIC 部分将作为shell命令执行(需要GNU sed) > MAGIC : cat& \ | seds:^:file \ 2:g cat& amp ;只是输出文件内容,并管道到另一个sed。做替换( s:..:..:g ) 最后,MAGIC的执行结果将会是的外部替代。 关键是Gnu sed的e / p> 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; doneI 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.txtA1A2fileB.txtB1B2desired output fileA.txt A1fileA.txt A2fileB.txt B1fileB.txt B2I know how to do this with awk, but I'd like to do it with sed, find and exec or xargs. 解决方案 find . -type f |xargs awk '$0=FILENAME$0' > outas I answered this, your "no awk" line not yet there. anyway, take a look my updated answer below:updated based on commentso 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. )find . -type f | xargs -i echo {}|sed -r 's#(.\/)(.*)#cat &\|sed "s:^:file \2 :g"#ge'now let's take a test, see below:kent$ head *.txt==> a.txt <==A1A2==> b.txt <==B1B2kent$ find . -type f | xargs -i echo {}|sed -r 's#(.\/)(.*)#cat &\|sed "s:^:file \2 :g"#ge'file b.txt B1file b.txt B2file a.txt A1file a.txt A2is the result your expectation?Short explanationfind ....|xargs -i echo {} nothing to explain, just print thefilename per line (with leading "./")then pass the filename to a sed line like sed -r 's#(.\/)(.*)# MAGIC#ge'remember that in the above line, we have two groups \1: "./" and\2 "a.txt"(filename)since we have e at the end of sed line, the MAGIC part would beexecuted as shell command.(GNU sed needed)MAGIC: cat &\|sed "s:^:file \2 :g cat & is just output the filecontent, and pipe to another sed. do the replace (s:..:..:g)finally, the execution result of MAGIC would be the Replacement ofthe outer sed.the key is the 'e' of Gnu sed. 这篇关于使用find和sed将文件名添加到文件的开头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-13 07:29