If both condition satisfied


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting If both condition satisfied
# 1  
Old 02-27-2014
If both condition satisfied

Code:
pattern
tin loop 3 
tin xarg 1
tin jim  5
tin icon 2 
tin tour 4

patn.out
tin loop 3
tin jim  5
tin icon 1 
tin tour 2

while read one two three
do
awk '$2 ~ /'"$two"'/' patn.out || while read four five six
do
awk -v n1=$three -v n2=$six '{BEGIN (n2<n1)?1:0}'
done<patn.out
done<pattern

my first check is to search a pattern in column 2 of file patn.out and if first is true then check
whether variable $three is less than $six, if it is less than i want to write those lines to a flat file
Code:
Output needed for above input:
tin xarg 1            		 ---> xarg i failed in first awk so written in output file
tin icon 2 
tin tour 4

From the above code, I can able to test whether it is less than or not but as we know it is displaying the output
which satisfied in first awk

Do we have single awk line to solve this? if so can you plz explain the command
# 2  
Old 02-27-2014
Hi, you could try:
Code:
awk 'NR==FNR{A[$2]=$3; next} !($2 in A) || $3>A[$2]' patn.out pattern

# 3  
Old 02-27-2014
Thanks it works, can you explain how it works?
# 4  
Old 02-27-2014
Hi, it is the same thing as what you were trying to accomplish using shell and awk to do, but entirely in awk, using associative array elements...

Code:
awk '
  NR==FNR{                       # When the first file is being read (only then are FNR and NR equal)
    A[$2]=$3                     # create an (associative) element in array A with the second field as the index and the 3rd field as value
    next                         # start reading the next record (line)
  } 
  !($2 in A) || $3>A[$2]         # while reading the second file, if field 2 is not present in array A, OR if it is present, but $3 is more than A[$2] (the previously recorded $3 from path.out for that particular $2) then print the record (line) 
' patn.out pattern               # first read patn.out as the first file and then 'pattern' as the second file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

If condition return 0 even when it fails to satisfy te condition

HI My doubt may be basic one but I need to get it clarified.. When i use "if" condition that checks for many AND, OR logical conditions like if ]; then return 0 fi Even the if condition fails it returns as zero.. Any clue.. But if i add else condition like if ]; ... (2 Replies)
Discussion started by: Priya Amaresh
2 Replies

2. Shell Programming and Scripting

if condition

if chr1:109457160 1 109457160 99.1735537190083 + chr1:109457233 1 109457233 99.1735537190083 - chr1:109457614 1 109457614 99.1735537190083 + chr1:109457618 1 109457618 100 + chr1:109457943 1 109457943 100 - chr1:109458224 1 109458224 99.1735537190083 - file1.txt If 6th column in... (3 Replies)
Discussion started by: johnkim0806
3 Replies

3. Shell Programming and Scripting

redirect stdout echo command in condition A run in condition B

hi, I have some problems in my simple script about the redirect echo stdout command inside a condition. Why is the echo command inside the elif still execute in the else command Here are my simple script After check on the two diff output the echo stdout redirect is present in two diff... (3 Replies)
Discussion started by: jao_madn
3 Replies

4. HP-UX

Difference between [condition] and [[condition]] and ((condition)) when used with if condition

Executed the following if conditions .. and got different results . only (( )) gave correct o/p with all scenarios . Can anybody please let me know what is the difference between and ] and ((condition)) when used with if condition. And why each condition gave different result. 1.... (2 Replies)
Discussion started by: soumyabubun
2 Replies

5. Shell Programming and Scripting

or in a IF condition

Hi I have to evaluate multiple conditions with an 'or'. Here is an example: if when i use the above i get a error message ' Please help me to know if i am missing something in the syntax. how do i achieve multiple "or" in the same if condition. Thanks. (11 Replies)
Discussion started by: sunrexstar
11 Replies

6. UNIX for Dummies Questions & Answers

until loop : running even if condition is satisfied

I am checking one until loop with the following logic until || |||| do sleep 30 done before running this i am exporting above variables to SUCCESS.But the loop is running in infinite loop.My requirement is to make the loop run until all the 3 variables are SUCCESS or any of the 3... (2 Replies)
Discussion started by: dr46014
2 Replies

7. UNIX for Dummies Questions & Answers

Changing old file name to new one if the old file name satisfied certain condition

Hi guys, I desperately need your help on my problem:- I got a folder in my directory that has many files in it. Most of the file's name is starting with certain prefix. Eg: ABC, CDE, EFG, etc. I only want to change file name that is starting from certain prefix and the rest I want them to... (1 Reply)
Discussion started by: balzzz
1 Replies

8. Shell Programming and Scripting

how to create folder wen the condition satisfied

hi i hav files ha1j ha2m ha3n ha4q ha5s ...like tat im having some 20 files ..and i want to create a folder as the same amount of files which im having wen the condition if loop is satisfied .. thank you (5 Replies)
Discussion started by: maximas
5 Replies

9. Shell Programming and Scripting

print remaining file after a condition is satisfied

Hi , could any one suggest me that how to determine if the first field is numeric and if it is greater than another number then from that point everything else should be printed using awk. I have tried this : awk -v xxxx=$xxxxx ' BEGIN { enable=0 } { print $1 if ( ( $1 !~ "^*$" ... (5 Replies)
Discussion started by: hitmansilentass
5 Replies

10. Shell Programming and Scripting

if condition ...

i have following if condition if above statement is case sensitive.....what is syntax if i have to make above comparision case insensetive (4 Replies)
Discussion started by: mahabunta
4 Replies
Login or Register to Ask a Question