简体   繁体   English

如何用新的标头和尾部字符串替换目录中所有文件的标头和尾部,但最多只能确定一个字符?

[英]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.

相关问题 Linux:如何将实际数据与标题和尾部分开 - Linux: How to strip actual data apart from header and trailer Linux-根据带有头和尾的键位置对文件排序 - Linux - Sort a File based on key position with header and trailer 如何将整个嵌套目录中的头文件仅复制到另一个目录,在复制到新文件夹后保持相同的层次结构 - How can i copy only header files in an entire nested directory to another directory keeping the same hierarchy after copying to new folder 仅在某些字符串中替换字符 - Replace character only in certain string 如何在.csv文件的前两行之后使用awk覆盖,而不删除它们,并在预告片之前停止覆盖? - How do I use awk to overwrite after the first two lines of the .csv file, without deleting them, and stop overwriting before the trailer? 查找并替换特定目录中所有文件和文件夹中的字符串 - Find and replace string in all files and folders in a certain directory 如何在 Linux 上的指定目录中查找使用特定标头的所有文件? - How to find all the files that use a particular header in a specified directory on Linux? 如何使用grep在当前目录中搜索具有字符串“hello”但仅显示.h和.cc文件的所有文件? - How do I use grep to search the current directory for all files having the a string “hello” yet display only .h and .cc files? 如何使用字符串和随机生成的数字替换文件中的标头值? - How do I replace the header value in a file with a string and a randomly generated number? 除了某些文件夹中的文件外,我如何将其所有文件包含在文件夹或目录中? - How do I tar a folder or directory with all of it's files except leave out files from certain folders?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM