Quantcast
Channel: User Dennis Williamson - Unix & Linux Stack Exchange
Viewing all articles
Browse latest Browse all 61

Answer by Dennis Williamson for Increment numeric pattern in file

$
0
0

This prints the whole line by substituting a new value for field 2 and should work regardless of how many fields may appear later in the line.

awk -F 'REQ| ' '$1 == "(REF-" {field1 = $1; $1 = ""; num = $2; for (i = 0; i < 4096; i++) {$2 = sprintf("%sREQ%04d", field1, num + i); line = $0; sub("^ ", "", line); print line}; next} {print}' inputfile

Broken out on multiple lines:

awk -F 'REQ| ' '$1 == "(REF-" {
        field1 = $1; 
        $1 = ""; 
        num = $2; 
        for (i = 0; i < 4096; i++) {
            $2 = sprintf("%sREQ%04d", field1, num + i); 
            line = $0; 
            sub("^ ", "", line); 
            print line
        };
        next
    }
    {
        print
    }' inputfile

Viewing all articles
Browse latest Browse all 61

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>