regex - Need SED or AWK script to do strlen optimization -
i need little cause touch sed or awk. i'm trying replace
string1.append("hello"); // regexp find is: \w*\.append\(".*"\)
with
string1.append("hello", 5); // note has figure out length of "hello"
and need search , replace across hundreds of thousands of files. , "hello anything... including "\n\n\n" should 3 not 6. example:
s.append("\n\n\n"); ---> s.append("\n\n\n", 3);
thanks in advance help... i'm thinking need awk i'm reading tutorial basics of awk right now...
since want run on files containing code, here's example of full functionality:
$ cat file foo() { string1.append("hello"); if (bar) { s.append("\n\n\n"); } else { s.append("\n\\n\n\\\n"); } } $ $ cat tst.awk match($0,/[[:alnum:]_]+\.append\(".*"\)/) { split(substr($0,rstart,rlength), orig, /"/) head = substr($0,1,rstart-1) orig[1] tail = orig[3] substr($0,rstart+rlength) tgt = orig[2] gsub(/[\\][\\]/,"x",tgt) gsub(/[\\]/,"",tgt) $0 = sprintf("%s\"%s\", %d%s", head, orig[2], length(tgt), tail) } { print } $ $ awk -f tst.awk file foo() { string1.append("hello", 5); if (bar) { s.append("\n\n\n", 3); } else { s.append("\n\\n\n\\\n", 6); } }
i replaced "\w" example in original posted question posix equivalent "[[:alnum:]_]" portability. "\w" work gnu awk , other tools, not tools , not awks.
Comments
Post a Comment