简体   繁体   中英

How To Increment Matching Pattern Of Number In File With Awk/Sed?

I currently have a file that's separated by tabs and I'm trying to increment the third column by 0.05.

Example:

2999    comp  .20

2993    EE    .10

2949    CS    .17

2799    CE    .30

And I would expect a result of the following:

2999    comp  .25

2993    EE    .15

2949    CS    .22

2799    CE    .35

So far I tried to use awk '{sub(/[[:digit:]]+$/,$NF+.05)}1' filename.txt which returned the following:

2999    comp  .0.3

2993    EE    .0.2

2949    CS    .0.27

2799    CE    .0.4

What's the correct way to retain the formatting while incrementing the number by a decimal?

If you want to remain TAB between columns, enter following code:

awk -v OFS='\t' '$3+=0.05' [Your_File]

Output is:

2999    comp  .25
2993    EE    .15
2949    CS    .22
2799    CE    .35

I think parsing the strings is the simplest approach.

    $ cat r.sh
    # w, s: regexps matching "word" or "space" (delimiter)
    # nxt(): sets T (type: "W" or "S") and P (pattern), and "eats" $0
    # pp[], tt[]: parallel arrays with patterns and types
    awk '
    function transform(t, cnt, x) {
        return (t == "W" && cnt == 3 ) ? x + 0.05 : x
    }
    BEGIN {
        w = "^[^ \t]+"
        s = "^[ \t]+"
    }
    {
        for (n = 0; nxt(); n++) {
            pp[n] = P; tt[n] = T
        }

        for (iw = i = 0; i < n; i++) {
           if (tt[i] == "W") iw++
           printf "%s", transform(tt[i], iw, pp[i])
        }
        printf "\n"
    }
    function nxt() {
        if      (match($0, w)) {T = "W"; P = nxt0(); return 1 }
        else if (match($0, s)) {T = "S"; P = nxt0(); return 1 }
        else return 0
    }
    function nxt0(   p) {
        p =  substr($0, 1, RLENGTH)
        $0 = substr($0, RLENGTH + 1)
        return p
    }
    ' "$@"

Usage:

$ sh r.sh file
2999    comp  0.25

2993    EE    0.15

2949    CS    0.22

2799    CE    0.35

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM