Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 to work.

like image 835
Corentin Peuvrel Avatar asked Mar 01 '15 15:03

Corentin Peuvrel


4 Answers

You could use sed also.

$ echo 'fooBar' | sed -r 's/([a-z0-9])([A-Z])/\1_\L\2/g'
foo_bar
$ echo 'fooBar' | sed 's/\([a-z0-9]\)\([A-Z]\)/\1_\L\2/g'
foo_bar
like image 94
Avinash Raj Avatar answered Oct 10 '22 18:10

Avinash Raj


The proposed sed answer has some issues:

$ echo 'FooBarFB' | sed -r 's/([a-z0-9])([A-Z])/\1_\L\2/g'
Foo_bar_fB

I sugesst the following

$ echo 'FooBarFB' | sed -r 's/([A-Z])/_\L\1/g' | sed 's/^_//'
foo_bar_f_b
like image 43
pachopepe Avatar answered Oct 10 '22 19:10

pachopepe


After a few unsuccessful tries, I got this (I wrote it on several lines for readability, but we can remove the newlines to have a onliner) :

awk -i inplace '{
  while ( match($0, /(.*)([a-z0-9])([A-Z])(.*)/, cap)) 
    $0 = cap[1] cap[2] "_" tolower(cap[3]) cap[4];

  print
}' FILE

For the sake of completeness, we can adapt it to do the contrary (underscore to CamelCase) :

awk -i inplace '{
  while ( match($0, /(.*)([a-z0-9])_([a-z])(.*)/, cap))
    $0 = cap[1] cap[2] toupper(cap[3]) cap[4];

  print
}' FILE

If you're wondering, the -i inplace is a flag only available with awk >=4.1.0, and it modify the file inplace (as with sed -i). If you're awk version is older, you have to do something like :

awk '{...}' FILE > FILE.tmp && mv FILE.tmp FILE

Hope it could help someone !

like image 45
Corentin Peuvrel Avatar answered Oct 10 '22 17:10

Corentin Peuvrel


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.

like image 27
Ed Morton Avatar answered Oct 10 '22 17:10

Ed Morton