replace multiple lines identifying end character

后端 未结 3 754
眼角桃花
眼角桃花 2021-01-25 02:03

I have the below code

CREATE TABLE Table1(
        column1 double NOT NULL,
        column2 varchar(60) NULL,
        column3 varchar(60) NULL,
        column4          


        
3条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-25 02:53

    With GNU awk for multi-char RS and assuming you want to get rid of the comma before the "CONSTRAINT":

    $ cat tst.awk
    BEGIN{ RS="^$"; ORS="" }
    {
        gsub(/\/,"\034")
        gsub(/,\s*CONSTRAINT[^\034]+\034/,")")
        gsub(/\034/,"GO")
        print
    }
    $ gawk -f tst.awk file
    CREATE TABLE Table1(
            column1 double NOT NULL,
            column2 varchar(60) NULL,
            column3 varchar(60) NULL,
            column4 double NOT NULL)
    GO
    

    The above works by replacing every stand-alone "GO" with a control char that's unlikely to appear in your input (in this case I used the same value as the default SUBSEP) so we can use that char in a negated character list in the middle gsub() to create a regexp that ends with the first "GO" after "CONSTRAINT". This is one way to do "non-greedy" matching in awk.

    If there is no char that you KNOW cannot appear in your input, you can create one like this:

    $ cat tst.awk
    BEGIN{ RS="^$"; ORS="" }
    {
        gsub(/a/,"aA"); gsub(/b/,"aB"); gsub(/\/,"b")
        gsub(/,\s*CONSTRAINT[^b]+b/,")")
        gsub(/b/,"GO"); gsub(/aB/,"b"); gsub(/aA/,"a")
        print
    }
    $ 
    $ gawk -f tst.awk file
    CREATE TABLE Table1(
            column1 double NOT NULL,
            column2 varchar(60) NULL,
            column3 varchar(60) NULL,
            column4 double NOT NULL)
    GO
    

    The above initially converts all "a"s to "aA" and "b"s to "aB" so that

    1. there are no longer any "b"s in the record, and
    2. since all original "a"s now have an "A" after them, the only occurrences of "aB" represent where "bs" were originally located

    and that means that we can now convert all "GO"s to "b"s just like we converted them to "\034" in the first script above. Then we do the main gsub() and then unroll our initial gsub()s.

    This idea of gsub()ing to create chars that cannot previously exist, using those chars, then unrolling the initial gsub()s is an extremely useful idiom to learn and remember, e.g. see https://stackoverflow.com/a/13062682/1745001 for another application.

    To see it working one step at a time:

    $ cat file                                                                                                   
    foo bar Hello World World able bodies
    
    $ awk '{gsub(/a/,"aA")}1' file                                                                               
    foo baAr Hello World World aAble bodies
    
    $ awk '{gsub(/a/,"aA"); gsub(/b/,"aB")}1' file                                                               
    foo aBaAr Hello World World aAaBle aBodies
    
    $ awk '{gsub(/a/,"aA"); gsub(/b/,"aB"); gsub(/World/,"b")}1' file                                            
    foo aBaAr Hello b b aAaBle aBodies
    
    $ awk '{gsub(/a/,"aA"); gsub(/b/,"aB"); gsub(/World/,"b"); gsub(/Hello[^b]+b/,"We Are The")}1' file                         
    foo aBaAr We Are The b aAaBle aBodies
    
    $ awk '{gsub(/a/,"aA"); gsub(/b/,"aB"); gsub(/World/,"b"); gsub(/Hello[^b]+b/,"We Are The"); gsub(/b/,"World")}1' file 
    foo aBaAr We Are The World aAaBle aBodies
    
    $ awk '{gsub(/a/,"aA"); gsub(/b/,"aB"); gsub(/World/,"b"); gsub(/Hello[^b]+b/,"We Are The"); gsub(/b/,"World"); gsub(/aB/,"b")}1' file
    foo baAr We Are The World aAble bodies
    
    $ awk '{gsub(/a/,"aA"); gsub(/b/,"aB"); gsub(/World/,"b"); gsub(/Hello[^b]+b/,"We Are The"); gsub(/b/,"World"); gsub(/aB/,"b"); ; gsub(/aA/,"a")}1' file
    foo bar We Are The World able bodies
    

提交回复
热议问题