Add another condition to bash for when not met


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Add another condition to bash for when not met
# 1  
Old 10-20-2016
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 Smilie.

Code:
if(low[i] <= $2 && $2 <= high[i]) {
            # Match found, add field and break out of loop.
            $(NF + 1) = "low"
            Else
[I]if(low[i] != $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:

Code:
line1     Not low
line2     Not low
line3     Low
line4     Not low

# 2  
Old 10-20-2016
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?
# 3  
Old 10-20-2016
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, firstif is not met. Thank you Smilie.
# 4  
Old 10-20-2016
Quote:
Originally Posted by cmccabe
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, firstif is not met. Thank you Smilie.
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.
This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 10-21-2016
I will post back tomorrow morning when I get back in. Thank you Smilie.

---------- 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 Smilie.


file
Code:
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
Code:
chr2    50573818    50574097    NRXN1
chr7    148106400    148106550    CNTNAP2

desired output (updated file)
Code:
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

Code:
#!/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[i] <= $2 && $2 <= high[i]) {
            # Match found, add field and break out of loop.
            $(NF + 1) = "low"
            Else
            if(low[i] = $2 && $2 = high[i]) {
            # 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

# 6  
Old 10-21-2016
I have't tested the following update, but it should come close to what you want. Try changing:
Code:
    # 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[i] <= $2 && $2 <= high[i]) {
            # Match found, add field and break out of loop.
            $(NF + 1) = "low"
            Else
            if(low[i] = $2 && $2 = high[i]) {
            # No match found, add field and break out of loop.
            $(NF+1) = "no low"
            fi
            break
        }

to:
Code:
    # 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[i] <= $2 && $2 <= high[i]) {
            # Match found, update the added field and break out of loop.
            $NF = "low"
            break
        }

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk - print when condition is met

I have a file.txt containing the following: Query= HWI-ST863:386:C5Y8UACXX:3:2302:16454:89688 1:N:0:ACACGAAT Length=100 Score E Sequences producing significant alignments: (Bits) Value ... (2 Replies)
Discussion started by: tons92
2 Replies

2. Shell Programming and Scripting

Need help on how to append on the filename when condition met.

Hi All, Seeking for your assistance on how to append the specific string when $3 condion met. ex. file1.txt ar0050046b16,5,888,0,0,0,0.00,0.00,0.00,0.00,25689.55 ar0050046b16,5,0,0,0,0,0.00,0.00,0.00,0.00,25689.55 ar0050046b16,5,0,0,0,0,0.00,0.00,0.00,0.00,25689.55 expected output:... (5 Replies)
Discussion started by: znesotomayor
5 Replies

3. Shell Programming and Scripting

Getting the records once condition met

Hi All, Seeking for your assistance to get the records once the $2 met the condition. Ex. file 1.txt 123455,10-Aug-2020 07:33:37 AM,2335235,1323534,12343 123232,11-Aug-2015 08:33:37 PM,4234324,1321432,34364 Output: 123455,10-Aug-2020 07:33:37 AM,2335235,1323534,12343 What i did... (5 Replies)
Discussion started by: znesotomayor
5 Replies

4. Shell Programming and Scripting

Print specific field when condition met

Hi All, Seeking for your assistance to print all the specific field when the condition met. Ex: file1.txt 1|203|3|31243|5341|6452|623|22|00|01 3|45345|123214|6534|3423|6565|643|343|232|10 if field 1 = 1 and field 3 = 3 and field 5 = 5341 and field 6 = 6452 it will print from $1 to $10.... (2 Replies)
Discussion started by: znesotomayor
2 Replies

5. Shell Programming and Scripting

Awk. Abort script if condition was met.

I want to abort script if input variable matched first field in any line of a file. #!/bin/sh read INPUTVAR1 awk "{if(\$INPUTVAR1 == $1) x = 1} END {if(x==1) print \"I want to abort script here\"; else print \"OK\"}" /etc/some.conf I tried "exit" and system("exit") but no luck. (1 Reply)
Discussion started by: urello
1 Replies

6. Shell Programming and Scripting

Delete if condition met in a column

i have a table like this: id, senderNumber, blacklist ----------------------------- 1 0835636326 Y 2 0373562343 Y 3 0273646833 Y and I want to delete automatically if a new inserted row on another table consist anything on senderNumber column above using a BASH Script I... (9 Replies)
Discussion started by: jazzyzha
9 Replies

7. Shell Programming and Scripting

perl -Calling the Subroutine Only if the condition is met

Hello All, I am in the process of learning perl.I have a perl script and based on the arguments passed it would the appropriate subroutine that is defined in the script. Now, I need to check a value that is defined in the Environment variables and should call the subroutine only if the... (1 Reply)
Discussion started by: filter
1 Replies

8. UNIX for Advanced & Expert Users

While loop only if a condition is met

All, I wrote the following section of code (which logically in PHP would of worked): tmpPATH=${1} tmpTAG=${2} if then while read tmpTAG tmpPATH do fi echo $tmpTAG echo $tmpPATH if then done < ./config.cfg fi (4 Replies)
Discussion started by: Cranie
4 Replies

9. Shell Programming and Scripting

do nothing if condition is not met but not exit

Hello all, I created the below script....and it seemed to be working fine. My problem is i want the script to ignore rest of the things if my condition is not met but do not exit.... #!/bin/ksh ########################### ########################### # Set name of the listener, this... (2 Replies)
Discussion started by: abdul.irfan2
2 Replies

10. Shell Programming and Scripting

How to break a loop if condition is met

I am having trouble figuring this code I want to grep a text from a file and if it match certain text it break out of the loop or it should continue searching for the text Here is what I have written but it isn't working while true f=`grep 'END OF STATUS REPORT' filename` do if ... (9 Replies)
Discussion started by: Issemael
9 Replies
Login or Register to Ask a Question