[英]How to add multiple lines to the beginning of many files in linux?
我有一个文件beginning.txt,其中包含以下行:
Disclaimer Here
This is a Notice
I have something liere
如何执行linux命令将starts.txt中的所有行添加到与扩展名“.customfile”匹配的每个文件的顶部(注意.customfile只是一个文本文件,但这些文件可能在我当前的子目录中)我想要更新的文件夹)?
我有许多带有.customfile后缀的文件,我想要追加,所以寻找一种以编程方式执行此操作的方法。 我看到sed命令的示例仅限于一行。
sed -i '1s/^/<added text> /' file
用bash:
for file in $(find . -name "*.customfile"); do
echo Processing $file
cat beginning.txt $file > $file.modified
mv $file.modified $file
done
sed
命令r
在附加某个位置的文件内容时非常有用,除非该位置是第0行。
在第1行之前插入文本就像在第0行之后添加一样。如果允许使用sed '0r /path/to/beginning.txt *.customfile
,那将是解决方案。
您可以将进程替换与sed
一起使用。 进程替换将生成一个sed
命令,可以针对您搜索的任何文件运行该命令。
sed -i -f - file < <(sed 's/^/1i/' /path/to/beginning.txt)
进程sed 's/^/1i/' /path/to/beginning.txt
to sed 's/^/1i/' /path/to/beginning.txt
将生成sed
命令以插入在/path/to/beginning.txt
中找到的文本:
1iDisclaimer Here
1iThis is a Notice
1iI have something liere
sed -f -
将从文件中读取sed命令, -
表示该文件是stdin
。
如果需要在多个文件上运行它,可以使用globs或find
和xargs
。
使用多个文件的示例:
sed -i -f - /path/to/*.customfile < <(sed 's/^/1i/' /path/to/beginning.txt)
使用find和xargs的示例:
find . -type f -name \*.customfile | xargs -I{} bash -c "sed -f - {} < <(sed 's/^/1i/' /tmp/beginning.txt)"
或者,您可以通过将输出分配给变量并使用此字符串传递给sed
命令来避免为每个文件重新运行sed 's/^/1i/' /path/to/beginning.txt
。
sedcommand=$(sed 's/^/1i/' /path/to/beginning.txt)
sed -i -f - /path/to/*.customfile <<< "$sedcommand"
或者从beginning.txt
创建一个command.sed
文件,并使用它将文本插入自定义文件。
sed 's/^/1i/' /path/to/beginning.txt > /path/to/command.sed
sed -i -f /path/to/command.sed /path/to/*.customfile
这是让你入门的东西,你只需要根据自己的喜好进行调整:
for i in *.customfile; do sed -i '1s/^/<your_text> \n/' $i; done
我想你可以试试这样的东西
find . -name "*.customfile" -type f | while read i; do cat beginning.txt $i | tee $i 1>/dev/null ; done
也许不是最优雅的使用tee命令,但它完成了工作:)
你有两个基本问题:
*.customfile
) 使用find
可以轻松解决#1问题。 通过将行添加到临时文件,然后移动文件,#2可能是最容易做到的。
这是一个单行shell命令,它将执行它,假设您要在名为beginning.txt
的文件中添加行:
find . -name "*.customfile" -print | while read fn ; do echo fixing $fn ; cat beginning.txt "$fn" > "$fn".new && mv "$fn" "$fn".old && mv "$fn".new "$fn" ; done
虽然cat
是适合这项工作的shell工具(参见:Robert的答案,我建议改为),如果你确实想用多行替换折磨自己,或者在大量的开头插入免责声明文本文件,你可以使用sed
的地址前缀来定位一行的第一个开头,然后使用sed
替换第一行的开头(例如文件的开头),使用带有免责声明文本的正则表达式 '^'
替换。
具体来说,你将使用sed
地址的'0,addr2'
形式,如果addr2
( 正则表达式 )匹配第一行输入它导致0,addr2
形式在其范围的末尾并替换(插入) )替换文本一次并退出。
例如,在每个文件的开头添加免责声明files
(无论您使用通配符或find
与过程替代选择需要的文件)将采取以下形式:
## set disclaimer text in `dc` however you like
dc='Disclaimer Here\nThis is a Notice\nI have something liere\n'
## use 0,addr2 Address form to insert at beginning of each file.
sed -i "0,/^/s/^/${dc}/" files
示例文件
$ cat dat/basefile.txt
========
The quick brown fox
jumps over the lazy dog.
示例使用/输出(以stdout为例)
$ dc='Disclaimer Here\nThis is a Notice\nI have something liere\n' \
sed -e "0,/^/s/^/${dc}/" dat/basefile.txt
Disclaimer Here
This is a Notice
I have something liere
========
The quick brown fox
jumps over the lazy dog.
而不是折磨自己,而不是使用cat
...
这是另一个没有tmp-File的小解决方案(只有初始文本在一个文件中)
for i in ``ls *.customfile``; do echo -e "``cat prepend.txt``$(cat $i)" > $i; done
请用正常的双反推替换,我不知道如何在这里显示正常的反推。
如果您不想要文件prepend.txt ,则可以在文本后面用带有“\\ n”的行替换cat-command。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.