简体   繁体   中英

How to replace multiline comments with sed in files

I have all sort of files php html xml where the copyright notice is included either in top or after 2-3 lines from the top like this

/**
 * Copyright (c)  2014
 * All rights reserved.
 * bla bla many lines like this
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

Now i want to remove all that text from all the files.

How can i remove with sed.

I just know basic sed but i don't know how to deal with multilines

$ cat file
1
2
/**
 * Copyright (c)  2014
 * All rights reserved.
 * bla bla many lines like this
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
3
4
/* and here is another comment
 * you presumably want to keep
 */
5
6

$ awk '/^\/\*/{c++} c!=1; /^ \*\//{c++}' file
1
2
3
4
/* and here is another comment
 * you presumably want to keep
 */
5
6

In you can delete a range starting with /* and ending with */ like this:

sed '/\/\*/,/\*\//d inputFile

tends to be a bit hard to read sometimes, so I'll give a more clear example: If you want to delete everything in a range starting with abc and ending with xyz , you'd do

sed '/abc/,/def/d' inputFile

The case with /* and */ is the same, but / and * have to be escaped.

A similar approach should work for the and files.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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