繁体   English   中英

用Linux / Unix命令替换文件中的行

[英]Replace block of lines in file with Linux/Unix Command

您能否为可以在文件上执行的命令提供帮助:

输入-1:

/*
* Copyright (c) 1992-2013 Some comp, Inc and/or its affiliates. All rights reserved.
* Some comp PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
* Author: aaaa
* DateCreated:   aaaa
* Last Modified By: aaaa
* Modified Time:  aaaa
* File Version: aaaa
* File Path: aaaa
*/

以上所有行应删除并替换为

/*
* Copyright (c) 1992-2013 some comp, and/or its affiliates. All rights reserved.
* some comp PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
* Author: bbbb
* DateCreated:   bbbb
* Last Modified By:  bbbb
* Modified Time:  bbbb
* File Version:  bbbb
* File Path:  bbbb
*/

我的意见是,首先从'/ '删除到' /'第一次出现,或者从文件开始删除到* /。 然后,替换为文件顶部的第二行。

对sed和awk使用正则表达式。 Gawk具有一个称为gsub的函数来替代文本。

假设文件名为filename newcopyright保存不包含第一行和最后一行的新文本(即,从“ *版权...”到“ *文件路径...”)。 然后:

sed -e '/Path/ r newcopyright' -e '/Copyright/,/Path/d' filename

这会将新文件发送到标准输出(即屏幕)。 您可以将其重定向到新文件,或者如果要在适当位置修改文件,可以执行以下操作:

sed -i '.save' -e '/Path/ r newcopyright' -e '/Copyright/,/Path/d' filename

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM