Search code examples
seduppercaselowercase

How to transfer characters between uppercase and lowercase with sed


For example, my src.tex is

SectionType *p = NULL;

and I want it change to

section_type *p = NULL;

what I want to do is just change the SectionType, but remains the NULL unchanged, I used y/[A-Z]/[a-z], but this will change all the line.

My sed do not support \L,\l,\U and \u functions, it version is 3.* and distributed under FSF, not GNU, may be GNU's works well with this functions, but at the time I could not adopt the GNU's because I'm working in msys and there is no GNU package.

Is there any solution to get it?


Solution

  • From here

    Convert CamelCase or camelCase to camel_case:

    sed -e 's/\([A-Z][a-z]\)/_\l\1/g' -e 's/^_\([a-z]\)/\1/g' file.txt

    Input:

    SectionType *p = NULL;
    sectionType *p = NULL;
    ABCSectionType *p = NULL;
    

    Output:

    section_type *p = NULL;
    section_type *p = NULL;
    ABC_section_type *p = NULL;