简体   繁体   中英

Replace block of lines in file with Linux/Unix Command

Could you please help for a command that can do on a file:

Input -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
*/

All above lines should delete and replace with

/*
* 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
*/

My opinion is, first delete from '/ ' to ' /' first occurrence or delete till */ from starting of file. And after, replacing with second block of lines at the top of file.

Use regular expressions with sed and awk. Gawk has a function called gsub for substituting text.

Suppose the file is called filename . Save the new text without the first and last lines (ie from "* Copyright..." to "* File Path...") in newcopyright . Then:

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

This will send the new file to standard output (ie your screen). You can redirect it to a new file, or if you want to modify the file in place, you can do this:

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

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