Add another condition to bash for when not met

In the below I can not seem to add a line that will add Not low if the statement in bold is not true or meet. I guess when the first if statement is true/meet then print low , otherwise print Not low in $(NF + 1) . I am not sure how to correctly add this. Thank you :).

if(low <= $2 && $2 <= high) {
            # Match found, add field and break out of loop.
            $(NF + 1) = "low"
            Else
if(low != $2 && $2 != high) {
            # No match found, add field and break out of loop.
            $(NF+1) = "no low"
            fi
            break

The portion in italics was an attempt to do this, but only the first if seems to work.

The desired output would be tab-delimited , but that is already part of the complete code:

line1     Not low
line2     Not low
line3     Low
line4     Not low

Hi cmccabe, is this actual code or are you trying to demonstrate a principle?
This seems to be a mix somehow of awk and bash (the "fi" statement) ? The capital E in "Else" is puzzling and so is the opening curly braces without the closing ones and the array reference "high[i]" as well as the variable "high" and what is "[i]if" supposed to mean?

This is a part of a real ksh written by @Don Cragun that I have added to. It runs flawlessly but I can not seem to add a line that adds Not low when the Low condition, first if is not met. Thank you :).

I'm glad that my code ran flawlessly for you, but what you have shown us in post #1 in this thread is a strange mix of sh and awk code with some pieces to hook them together (such as the Else Scrutinizer mentioned) that isn't valid in awk , bash , or ksh .

Please give us a clear description of what you are trying to do with this script, show us the input you are processing, and show us the output you hope to produce.

1 Like

I will post back tomorrow morning when I get back in. Thank you :).

---------- Post updated 10-21-16 at 10:10 AM ---------- Previous update was 10-20-16 at 06:07 PM ----------

In the code below (except for the portion in bold), $2 from file , in a range search using $2 and $3 in file1 . If the search key from file is found in file1 , then the word low is printed in the last field of that line in the updated file . Only the last section of file, Missing in IDP but found in Reference: and only the lines in this section are searched .

The portion in bold was an attempt to add a line that will add Not low if the statement in bold is not true or meet. I guess when the first if statement is true/meet then print low , otherwise print Not low in $(NF + 1) . I am not sure how to correctly add this. Thank you :).

file

Match:
chr15    68521889    C    T    exonic    CLN6    GOOD    50    het    4
chr7    147183143    A    G    intronic    CNTNAP2    GOOD    382    het    22
Missing in Reference but found in IDP:
chr2    51666313    T    C    intergenic    NRXN1,NONE    GOOD    108    het    7
chr2    166903445    T    C    exonic    SCN1A    GOOD    400    het    28
Missing in IDP but found in Reference:
2    166210776    C    T    exonic    SCN2A    c.[2994C>T]+[=]    3095    23.1    24.56
7    148106478    -    GT    intronic    CNTNAP2    c.3716-5_3716-4insGT    4168    28.6    51.01

file1

chr2    50573818    50574097    NRXN1
chr7    148106400    148106550    CNTNAP2

desired output (updated file)

Match:
chr15    68521889    C    T    exonic    CLN6    GOOD    50    het    4
chr7    147183143    A    G    intronic    CNTNAP2    GOOD    382    het    22
Missing in Reference but found in IDP:
chr2    51666313    T    C    intergenic    NRXN1,NONE    GOOD    108    het    7
chr2    166903445    T    C    exonic    SCN1A    GOOD    400    het    28
Missing in IDP but found in Reference:
2    166210776    C    T    exonic    SCN2A    c.[2994C>T]+[=]    3095    23.1    24.56     Not Low
7    148106478    -    GT    intronic    CNTNAP2    c.3716-5_3716-4insGT    4168    28.6    51.01     Low
#!/bin/bash

for file in /home/cmccabe/Desktop/concordance/comparison/update/*.txt ; do
    file1=${file##*/}    # Strip off directory
    getprefix=${file1%%_*.txt}
    file1=$(printf '%s\n' "/home/cmccabe/Desktop/concordance/low/${file1%%_*.txt}_"*.txt) # look for matching file
    if [[ -f "$file1" ]]
    then
TmpFile=${0##*/}.$$
awk '
BEGIN {    # Set input and output field separators...
    OFS = "\t"
}
NR == FNR {
    # Grab low and high ends of ranges from the 1st input file...
    low[++c] = $2
    high[c] = $3
    next
}
sect == 3 {
    # We are in the 3rd section of the 2nd input file (after the section
    # header line)...
    # Look for a range of vaues from a line in the 1st file that includes
    # the 2nd field in this file...
    for(i = 1; i <= c; i++)
        if(low <= $2 && $2 <= high) {
            # Match found, add field and break out of loop.
            $(NF + 1) = "low"
            Else
            if(low = $2 && $2 = high) {
            # No match found, add field and break out of loop.
            $(NF+1) = "no low"
            fi
            break
        }
}
/:$/ {    # Increment the 2nd input file section number when we find a colon at
    # the end of a line...
    sect++
}
1    # print the current contents of the 2nd file input line.
' $file1 $file > "$TmpFile" &&    # End awk script, specifying input files and
                # redirect the output to a temp file...
    cp "$TmpFile" $file &&     # If the awk script was successful, copy the
                    # temp file back to the 2nd input file...
    rm "$TmpFile"        # and, if that was also successful, remove the
                    # temp file.
fi
done

I have't tested the following update, but it should come close to what you want. Try changing:

    # Look for a range of vaues from a line in the 1st file that includes
    # the 2nd field in this file...
    for(i = 1; i <= c; i++)
        if(low <= $2 && $2 <= high) {
            # Match found, add field and break out of loop.
            $(NF + 1) = "low"
            Else
            if(low = $2 && $2 = high) {
            # No match found, add field and break out of loop.
            $(NF+1) = "no low"
            fi
            break
        }

to:

    # Add new field with default value.
    $(NF + 1) = "Not low"
    # Look for a range of vaues from a line in the 1st file that includes
    # the 2nd field in this file...
    for(i = 1; i <= c; i++)
        if(low <= $2 && $2 <= high) {
            # Match found, update the added field and break out of loop.
            $NF = "low"
            break
        }