[英]How do I replace the header and trailer of all files in a directory with a new header and trailer string, but only up to a certain character?
I have files in a directory: ex: 我在目录中有文件:例如:
file1
file2
Both files have contents that look like the below (header, body, trailer words just for reference): 这两个文件的内容都如下所示(标题,正文,尾部单词仅供参考):
header: 123xxx xxx value=file1id
body: blah blah blah
trailer: 123zzz zzz value=file1id
I want to run a linux command that will replace the header and trailer of all the files in the directory, up to "file1id", so that they look like the below: 我想运行一个linux命令,该命令将替换目录中所有文件的标题和结尾,直到“ file1id”为止,以便它们看起来如下所示:
header: 321aaa aaa value=file1id
body: blah blah blah
trailer: 321bbb bbb value=file1id
I can easily find how to replace the whole header or trailer in a file on Google, but I need the file1id in my file. 我可以轻松地找到如何替换Google上文件中的整个标头或预告片,但是我的文件中需要file1id。 I need some guidance on how to loop through files in a directory and replace the header and trailer up to a certain character in the header and trailer.
我需要一些有关如何循环浏览目录中的文件以及将标题和结尾替换为标题和结尾中的特定字符的指导。
I have seen a variety of sed and awk expressions that delete and replace the header or trailer. 我已经看到了各种各样的sed和awk表达式,它们删除并替换了标题或结尾。
sed -rne 's/(value=)\s+\w+/\1 yyy/gip'
As I mentioned above, I want to run a linux command that will replace the header and trailer of all the files in the directory, up to "file1id", so that they look like the below: 如上文所述,我想运行一个linux命令,该命令将替换目录中所有文件的标题和结尾,直到“ file1id”为止,以便它们看起来如下所示:
header: 321aaa aaa value=file1id
body: blah blah blah
trailer: 321bbb bbb value=file1id
This sed
example has two replace commands, one for the first line (header) and one for last line trailer (denoted by $ in the second substitution). 这个
sed
示例具有两个替换命令,一个用于第一行(标题),一个用于最后一行的尾部(在第二个替换中由$表示)。 -i
option of sed
edits the file in place. sed
-i
选项可在适当位置编辑文件。
sed -i '1 s/^.*value=/yoursubstitution value=/; $ s/^.*value=/yoursubstitution value=/'
output: 输出:
yoursubstitution value=file1id
body: blah blah blah
yoursubstitution value=file1id
You can match from the beginning of the line ( ^
) to value=
您可以从行首(
^
)匹配为value=
echo 'header: 123xxx xxx value=file1id' | sed 's/^.*value=/header: 321aaa aaa value=/'
header: 321aaa aaa value=file1id
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.