简体   繁体   中英

How to extract values from a “table-like” text file with awk

all
I have two input files like this :
file1 :

#W  #S  #this line dosen't exit
110 170 Bias
110 200 Bias
110 215 Bias
110 320 Bias
125 170 Bias
125 200 Bias
125 215 Bias
125 320 Bias
135 170 Bias
135 200 Bias
135 215 Bias
135 320 Bias
140 170 Bias
140 200 Bias
140 215 Bias
140 320 Bias

file2 :

FUNCTION BIAS ( W, S )
Bias = 0
IF AND ( W >= 0, W < 120 ) THEN
    IF ( S >= 0 ) THEN Bias = -1
    IF ( S >= 180 ) THEN Bias = -2
    IF ( S >= 190 ) THEN Bias = -3
    IF ( S >= 200 ) THEN Bias = -4
    IF ( S >= 210 ) THEN Bias = -5
    IF ( S >= 220 ) THEN Bias = -6
    IF ( S >= 240 ) THEN Bias = -7
ENDIF

IF AND ( W >= 120, W < 130 ) THEN
    IF ( S >= 0 ) THEN Bias = -11
    IF ( S >= 180 ) THEN Bias = -12
    IF ( S >= 190 ) THEN Bias = -13
    IF ( S >= 200 ) THEN Bias = -14
    IF ( S >= 210 ) THEN Bias = -15
    IF ( S >= 220 ) THEN Bias = -16
    IF ( S >= 240 ) THEN Bias = -17
ENDIF

IF AND ( W >= 130, W < 140 ) THEN
    IF ( S >= 0 ) THEN Bias = 1
    IF ( S >= 180 ) THEN Bias = 2
    IF ( S >= 190 ) THEN Bias = 3
    IF ( S >= 200 ) THEN Bias = 4
    IF ( S >= 210 ) THEN Bias = 5
    IF ( S >= 220 ) THEN Bias = 6
    IF ( S >= 240 ) THEN Bias = 7
ENDIF

IF ( W >= 140 ) THEN
    IF ( S >= 0 ) THEN Bias = 11
    IF ( S >= 180 ) THEN Bias = 12
    IF ( S >= 190 ) THEN Bias = 13
    IF ( S >= 200 ) THEN Bias = 14
    IF ( S >= 210 ) THEN Bias = 15
    IF ( S >= 220 ) THEN Bias = 16
    IF ( S >= 240 ) THEN Bias = 17
ENDIF

RETURN (Bias)

What I wanna do is to find out the corresponding value of a math function : "BIAS(W,S)" with the input (W,S) pair from file1

for example : W/S=135/195, "W" satisfy

IF AND ( W >= 130, W < 140 )  

so we will go to check "S"

    IF ( S >= 0 ) THEN Bias = 1
    IF ( S >= 180 ) THEN Bias = 2
    IF ( S >= 190 ) THEN Bias = 3
    IF ( S >= 200 ) THEN Bias = 4
    IF ( S >= 210 ) THEN Bias = 5
    IF ( S >= 220 ) THEN Bias = 6
    IF ( S >= 240 ) THEN Bias = 7

then finally we can find out S=195 is in between 190 and 200, the value of BIAS(W,S) is 3

what I want for the output is like this :

110 170 Bias -1
110 200 Bias -4
110 215 Bias -5
110 320 Bias -7
125 170 Bias -11
125 200 Bias -14
125 215 Bias -15
125 320 Bias -17
135 170 Bias 1
135 200 Bias 4
135 215 Bias 5
135 320 Bias 7
140 170 Bias 11
140 200 Bias 14
140 215 Bias 15
140 320 Bias 17 

It's very easy to check by human eyes but as you can see, file2 is basically a text file instead of a regular 2D-array numerical file, How can I extract the corresponding value? Any hint?

I just translated your logic into awk:

script.awk:

{
    w=$1;
    s=$2;

    if (w >= 0 && w < 120) {
        if ( s >= 0) { bias= -1 }
        if ( s >= 180 ) { bias= -2 }
        if ( s >= 190 ) { bias= -3 }
        if ( s >= 200 ) { bias= -4 }
        if ( s >= 210 ) { bias= -5 }
        if ( s >= 220 ) { bias= -6 }
        if ( s >= 240 ) { bias= -7 }
    }

    if (w >= 120 && w < 130) {
        if ( s >= 0) { bias= -11 }
        if ( s >= 180 ) { bias= -12 }
        if ( s >= 190 ) { bias= -13 }
        if ( s >= 200 ) { bias= -14 }
        if ( s >= 210 ) { bias= -15 }
        if ( s >= 220 ) { bias= -16 }
        if ( s >= 240 ) { bias= -17 }
    }

    if (w >= 130 && w < 140) {
        if ( s >= 0) { bias= 1 }
        if ( s >= 180 ) { bias= 2 }
        if ( s >= 190 ) { bias= 3 }
        if ( s >= 200 ) { bias= 4 }
        if ( s >= 210 ) { bias= 5 }
        if ( s >= 220 ) { bias= 6 }
        if ( s >= 240 ) { bias= 7 }
    }

    if (w >= 140 ) {
        if ( s >= 0) { bias= 11 }
        if ( s >= 180 ) { bias= 12 }
        if ( s >= 190 ) { bias= 13 }
        if ( s >= 200 ) { bias= 14 }
        if ( s >= 210 ) { bias= 15 }
        if ( s >= 220 ) { bias= 16 }
        if ( s >= 240 ) { bias= 17 }
    }

    print $0" "bias; 

}

Execution:

awk -f script.awk  file1
110 170 Bias -1
110 200 Bias -4
110 215 Bias -5
110 320 Bias -7
125 170 Bias -11
125 200 Bias -14
125 215 Bias -15
125 320 Bias -17
135 170 Bias 1
135 200 Bias 4
135 215 Bias 5
135 320 Bias 7
140 170 Bias 11
140 200 Bias 14
140 215 Bias 15
140 320 Bias 17

Run the tst.awk script below on "file2" to convert the script in whatever language that is to awk and save it's output to a new file named "getbias.awk", then run:

awk -f getbias.awk '<your script>' file1

where <your script> parses file1 and calls the generated getbias() function below to get the bias values for each line.

$ cat tst.awk
{
    sub(/BIAS/,"getbias")
    sub(/ENDIF/,"}")
    sub(/ THEN/,"")
    $0 = tolower($0)
}
/^function/ { sub(/\)/,",\tbias )"); $0 = $0 " {" }
/^return/   { $0 = $0 ORS "}" }
/^if/       { sub(/ and/,""); sub(/,/," \\&\\&"); $0 = $0 " {" }
{ print }

.

$ awk -f tst.awk file2
function getbias ( w, s ,       bias ) {
bias = 0
if ( w >= 0 && w < 120 ) {
    if ( s >= 0 ) bias = -1
    if ( s >= 180 ) bias = -2
    if ( s >= 190 ) bias = -3
    if ( s >= 200 ) bias = -4
    if ( s >= 210 ) bias = -5
    if ( s >= 220 ) bias = -6
    if ( s >= 240 ) bias = -7
}

if ( w >= 120 && w < 130 ) {
    if ( s >= 0 ) bias = -11
    if ( s >= 180 ) bias = -12
    if ( s >= 190 ) bias = -13
    if ( s >= 200 ) bias = -14
    if ( s >= 210 ) bias = -15
    if ( s >= 220 ) bias = -16
    if ( s >= 240 ) bias = -17
}

if ( w >= 130 && w < 140 ) {
    if ( s >= 0 ) bias = 1
    if ( s >= 180 ) bias = 2
    if ( s >= 190 ) bias = 3
    if ( s >= 200 ) bias = 4
    if ( s >= 210 ) bias = 5
    if ( s >= 220 ) bias = 6
    if ( s >= 240 ) bias = 7
}

if ( w >= 140 ) {
    if ( s >= 0 ) bias = 11
    if ( s >= 180 ) bias = 12
    if ( s >= 190 ) bias = 13
    if ( s >= 200 ) bias = 14
    if ( s >= 210 ) bias = 15
    if ( s >= 220 ) bias = 16
    if ( s >= 240 ) bias = 17
}

return (bias)
}

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