【发布时间】:2018-12-30 20:59:49
【问题描述】:
我有一个版权条款,我想将其作为注释附加到目录中一堆文件的顶部 (C#)。它看起来像这样:
/*COPYRIGHTCOPYRIGHTCOPYRIGHTCOPYRIGHTCOPYRIGHTCOPYRIGHTCOPYRIGHT*/
/*C Use, duplication, or disclosure of this software and T*/
/*C related documentation is blah blah blah blah T*/
/*C Copyright 2004 - 2018 COMPANY T*/
/*C All rights reserved. T*/
/*COPYRIGHTCOPYRIGHTCOPYRIGHTCOPYRIGHTCOPYRIGHTCOPYRIGHTCOPYRIGHT*/
一些文件已经包含一个子句,但是对于 2017 年或 2016 年的日期,我想确保子句中的日期设置为 2018 年。如果没有任何子句,我想插入一个。
到目前为止我已经实现find只修改目录中我需要的文件:
find . -type d \( -name ThirdParty -o -name 3rdParty -o -name 3rd_party \) -prune -o -type f \( -name "*.java" -o -name "*.cs" -o -name "*.cpp" -o -name "*.cxx" -o -name "*.cc" -o -name "*.c" -o -name "*.h" -o -name "*.scala" -o -name "*.css" -o -name "*.js" \) -print0
因为这个子句是每个文件顶部的注释,所以它以/*...*/开始和结束。我尝试使用sed,但这很棘手,因为斜线和星号用于sed 中的其他内容。
这是我的find 和sed 的组合。这会将 2017 的所有实例替换为 2018 并附加该子句,但即使它已经存在,它也会附加它。如果它不存在,我只需要附加它。
find ....... -print0 | xargs -0 sed -i 's/2004 - 2017/2004 - 2018/g; 1s/^/\/*COPYRIGHTCOPYRIGHTCOPYRIGHTCOPYRIGHTCOPYRIGHTCOPYRIGHTCOPYRIGHT*\/\n\/*C Use, duplication, or disclosure of this software and.....etc'
在 grep 或 sed 中有更好的方法吗?谢谢 编辑:我正在使用 CYGWIN
【问题讨论】: