简体   繁体   中英

awk, not clear about snippet in example from gnu-awk site

I'm going through this GNU awk example . This program needs the ord() and chr() library functions .

And that's where I get a little confused. I don't understand what does this code snippet does:

BEGIN    { _ord_init() }

function _ord_init(    low, high, i, t)
{
    low = sprintf("%c", 7) # BEL is ascii 7
    if (low == "\a") {    # regular ascii
        low = 0
        high = 127
    } else if (sprintf("%c", 128 + 7) == "\a") {
        # ascii, mark parity
        low = 128
        high = 255
    } else {        # ebcdic(!)
        low = 0
        high = 255
    }

    for (i = low; i <= high; i++) {
        t = sprintf("%c", i)
        _ord_[t] = i
    }
}

If you remove it ( BEGIN { _ord_init() } ) and run the split.awk example, it works but in a strange way: there's a file called "xa?" that appear and the file "xab" is missing.

This is the input I have:

1 2 3 hello
1 3 4 world
2 4 5 india
4 2 1 china

I run the split program as such:

awk -f split.awk -2 input.txt 

And the content of split.awk is as such: well its the code in the two preivous urls put into one file "split.awk".

So again, what does the function _ord_init do exactly?

It define the character set to use at runtime.

It uses a local response to a specific request ( sprintf("%c", 7) and sprintf("%c", 128 + 7) == "\\a" ) to allow chr() and ord() to answer in the same character set.

3 main family set are treated (other are untreated here) - ascii (most used at development time of awk) - parity ascii (ASCII but with the 8th bit is always set to 1 instead of 0 or parity breaker in transmission [to insure there is always an odd number of 1]) - ebcdic (typically mainframe)

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