Awk/sed script to convert a file from camelCase to underscores

后端 未结 4 730
灰色年华
灰色年华 2020-12-30 02:49

I want to convert several files in a project from camelCase to underscore_case.

I would like to have a onliner that only needs the filename

4条回答
  •  攒了一身酷
    2020-12-30 03:45

    This might be what you want:

    $ cat tst.awk
    {
        head = ""
        tail = $0
        while ( match(tail,/[[:upper:]]/) ) {
            tgt = substr(tail,RSTART,1)
            if ( substr(tail,RSTART-1,1) ~ /[[:lower:]]/ ) {
                tgt = "_" tolower(tgt)
            }
            head = head substr(tail,1,RSTART-1) tgt
            tail = substr(tail,RSTART+1)
        }
        print head tail
    }
    
    $ cat file
    nowIs theWinterOfOur disContent
    From ThePlay About RichardIII
    
    $ awk -f tst.awk file
    now_is the_winter_of_our dis_content
    From The_play About Richard_iII
    

    but without your sample input and expected output it's just a guess.

提交回复
热议问题