1
#!/bin/gawk
function convertToCamelCase(text)
{
        split(text, words, " "); for (i=1; i<=length(words); i++) { res = res toupper(substr(words[i],1,1))tolower(substr(words[i],2))" ";
        return res;
}
function convertToThreeDigitDecimal(num)
{
        return sprintf("%7.3f",(num/1000000));
}
BEGIN {
....

I am trying to add a function in awk, it is giving me syntax error.

bash-3.2$ ./execute_all_stats.sh.bak file.csv
awk: get_mkt_stats.awk.bak:7: function convertToThreeDigitDecimal(num)
awk: get_mkt_stats.awk.bak:7: ^ syntax error
awk: get_mkt_stats.awk.bak:11: BEGIN {
awk: get_mkt_stats.awk.bak:11: ^ syntax error

The awk version is:

bash-3.2$ awk --version
GNU Awk 3.1.5

I am calling the awk like the following:

gawk -F',' -f script.awk ${file}

1 Answer 1

2

you're missing a closing '}' on your for loop.

function convertToCamelCase(text)
{
    split(text, words, " ");
    for (i=1; i<=length(words); i++) { 
        res = res toupper(substr(words[i],1,1)) tolower(substr(words[i],2))" "
     }
 return res;
}

I've also added a space in front of tolower, just to make that function explicit.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.