user3147180
user3147180

Reputation: 943

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

Upvotes: 3

Views: 2155

Answers (2)

Ed Morton
Ed Morton

Reputation: 203615

$ 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

Upvotes: 4

pfnuesel
pfnuesel

Reputation: 15320

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.

Upvotes: 3

Related Questions