Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Depends on the AWK implementation, apparently.

    bash> awk -v v="80.1%" 'BEGIN{print v+0.1}'
    80.2
gawk has `strtonum`. But yes, parsing in awk generally looks like a pain. With plain positive/negative ints though, not so hard:

    echo "123456" | awk '{
        if ($0 ~ /^-?[0-9]+$/) {
            num = 0
            sign = 1
            start = 1
            if (substr($0, 1, 1) == "-") {
                sign = -1
                start = 2
            }
            for (i = start; i <= length($0); i++) {
                digit = substr($0, i, 1)
                num = num * 10 + digit
            }
            num = sign * num
            print "The integer is:", num
        } else {
            print "Invalid input string:", $0
        }
    }'


Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: