简体   繁体   中英

Delete multiple lines from files using sed/awk using regex pattern

Eclipse's "Generate Entities from Tables..." JPA code generator insists on creating an @Id field and associated getter/setter methods, despite the fact that these are defined in the superclass I designate in the wizard steps.

To work around this, my idea is to write a script to remove the offending lines and clean up the generated files in other ways. (Yes, I could do this manually using the Find/Replace dialog in Eclipse, but I want to automate the entire set of changes because I'll be doing this code generation/clean up routine multiple times while working on various models.)

The problem is that I cannot find the right regex pattern for matching multiple lines for use in either awk or sed. The regex patterns I create work in Eclipse's and TextWrangler's search dialogs, but not with awk or sed.

Here are the multi-line pieces of text I want to remove (in separate calls to awk/sed, of course):

    @Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(unique=true, nullable=false)
private String id;

and

    public String getId() {
    return this.id;
}

public void setId(String id) {
    this.id = id;
}

Please note that each non-empty line in the cited code above begins with a \\t character, even though it's not present when pasted here.

As an example, to delete the getter/setter lines, I have tried

awk '!/\tpublic String getId\(\) \{\r\t\treturn this\.id;\r\t\}\r\r\tpublic void setId\(String id\) \{\r\t\tthis\.id = id;\r\t\}\r/' $file > temp.txt && mv temp.txt $file

or a shortened version

awk '!/^\tpublic String getId.*\r.*\r.*\r\r.*\r.*\r.*\r\r/' ... 

but neither work.

(Both patterns work in Eclipse or TextWrangler to find the lines to be removed.)

I can only get single line deletes, for example

awk '!/^\tpublic String getId.*\r/' ...

but that's not helpful, since deleting "\\t}\\r" would delete all method-ending and class-ending curly braces.

I'm doing this on a Mac running OS X 10.8.2, if that makes any difference.

Thanks.

By default, awk and sed are line-based tools, so you can't match against more than one line.

I would suggest identifying the range using a regex for the first line and an offset to the last line, like this:

sed -e '/^\tpublic String getId/,+2 d'

This deletes the line public String getId , and the two following lines.

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