awk command with a loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk command with a loop
# 8  
Old 08-11-2017
I'm not yet sure I entirely grasp it, but that "grinding itself down to 0 lines" confirms my gut feeling about the processing. Unfortunately the few sample limes don't allow for a thorough testing. How about
Code:
awk '
$1 in X  || $2 in X     {next
                        }

$3 >  $4 ||
$3 == $4 && $5 < $6     {TMP = $2
                        }

$3 <  $4 ||
$3 == $4 && $5 > $6     {TMP = $1
                        }

                        {X[TMP]
                         print TMP
                        }
' file
ID1
ID4
ID2


Last edited by RudiC; 08-11-2017 at 03:13 PM..
This User Gave Thanks to RudiC For This Post:
# 9  
Old 08-11-2017
I think that's worked.. thank you!!
# 10  
Old 08-11-2017
If that logic works, you might want to try
Code:
awk '
!($1 in X  || $2 in X)  {TMP = 1 + ($3 >  $4 || $3 == $4 && $5 < $6)
                         X[$TMP]
                         print $TMP
                        }
' file
ID1
ID4
ID2

You may want to post a bit larger sample so the test can be built on a larger basis.

What if $3 == $4 && $5 == $6?
# 11  
Old 08-11-2017
That's even more elegant and gives the same output. I'm struggling to understand how you managed to condense my four criteria into :
Code:
($3 >  $4 || $3 == $4 && $5 < $6)

Re: if
Code:
$3==$4 && $5==$6

, there is one occurrence of this in the whole file, but one of the IDs has already been 'eliminated' by the time we get to that row (hence that row no longer exists), rendering it not a problem..
# 12  
Old 08-11-2017
In fact, what I applied is your logic from post#1, deploying the fact that the two expressions are mutually exclusive except for the $5 == $6 case. The result of a boolean expression is either 1 (TRUE), or 0 (FALSE), and by adding 1 the target field (1 or 2) is calculated (In a strict language like e.g. PASCAL you can't do that (arithmetics with logical values), but C or awk allow for it).
If you can make very sure the $5 == $6 case doesn't exist (or disappears in the process), you can leave its consideration out, but good programming style would require it to be covered, and be it with an error condition.
This User Gave Thanks to RudiC For This Post:
# 13  
Old 08-11-2017
Thank you for the explanation - v. helpful.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Loop awk command on files in a folder

Hi, I'd like to loop an action over all files with given extension within a folder. The "main" action is: awk -F "\t" 'BEGIN{OFS="\t"}{if ($10=="S") print$0; }' input.txt > output.txt The input.txt should be every file in the folder with *.subVCF extension; and the output should be a file... (3 Replies)
Discussion started by: dovah
3 Replies

2. Shell Programming and Scripting

How to use a loop for multiple files in a folder to run awk command?

Dear folks I have two data set which there names are "final.map" and "1.geno" and look like this structures: final.map: gi|358485511|ref|NC_006088.3| 2044 gi|358485511|ref|NC_006088.3| 2048 gi|358485511|ref|NC_006088.3| 2187 gi|358485511|ref|NC_006088.3| 17654 ... (2 Replies)
Discussion started by: sajmar
2 Replies

3. Shell Programming and Scripting

awk programming -Passing variable to awk for loop

Hi All, I am new to AWK programming. I have the following for loop in my awk program. cat printhtml.awk: BEGIN -------- <some code here> END{ ----------<some code here> for(N=0; N<H; N++) { for(M=5; M<D; M++) print "\t" D ""; } ----- } ... (2 Replies)
Discussion started by: ctrld
2 Replies

4. Shell Programming and Scripting

awk loop using array:wish to store array values from loop for use outside loop

Here's my code: awk -F '' 'NR==FNR { if (/time/ && $5>10) A=$2" "$3":"$4":"($5-01) else if (/time/ && $5<01) A=$2" "$3":"$4-01":"(59-$5) else if (/time/ && $5<=10) A=$2" "$3":"$4":0"($5-01) else if (/close/) { B=0 n1=n2; ... (2 Replies)
Discussion started by: klane
2 Replies

5. Shell Programming and Scripting

If else condition inside for loop of awk command in UNIX shell scripting

Hi , Please excuse me for opening a new thread i am unable to find out the syntax error in my if else condition inside for loop in awk command , my actual aim is to print formatted html td tag when if condition (True) having string as "failed", could anyone please advise what is the right... (2 Replies)
Discussion started by: karthikram
2 Replies

6. Shell Programming and Scripting

For loop, awk command issue

limit.csv data -------------- 5600050 38Nhava 400077 27Bomay rate.txt data ------------- 38NhaVA 27BomaY 27Bomay below is my script: for i in `cat limit.csv` do b=`awk '{print $1}' $i` (4 Replies)
Discussion started by: p_satyambabu
4 Replies

7. Shell Programming and Scripting

Problem Using If & For loop in AWK Command

I am parsing file for the fields using awk command, first i check 26th field for two characters using substr function if it matches then using for loop on array i search 184th field for 4 chars if it matches then i print the required fields but on execution i get the error, please help...... (5 Replies)
Discussion started by: siramitsharma
5 Replies

8. Shell Programming and Scripting

awk command in script gives error while same awk command at prompt runs fine: Why?

Hello all, Here is what my bash script does: sums number columns, saves the tot in new column, outputs if tot >= threshold val: > cat getnon0file.sh #!/bin/bash this="getnon0file.sh" USAGE=$this" InFile="xyz.38" Min="0.05" # awk '{sum=0; for(n=2; n<=NF; n++){sum+=$n};... (4 Replies)
Discussion started by: catalys
4 Replies

9. Shell Programming and Scripting

Comparison and editing of files using awk.(And also a possible bug in awk for loop?)

I have two files which I would like to compare and then manipulate in a way. File1: pictures.txt 1.1 1.3 dance.txt 1.2 1.4 treehouse.txt 1.3 1.5 File2: pictures.txt 1.5 ref2313 1.4 ref2345 1.3 ref5432 1.2 ref4244 dance.txt 1.6 ref2342 1.5 ref2352 1.4 ref0695 1.3 ref5738 1.2... (1 Reply)
Discussion started by: linuxkid
1 Replies

10. UNIX for Dummies Questions & Answers

Awk command in while loop

Hello! I've got a loop in which I am processing a list of values gotten through a file with read command. It seems that instead of processing the lines (values) one by one, I process them all together. the input file is: 20 20 20 80 70 70 20 The code is: (2 Replies)
Discussion started by: haaru
2 Replies
Login or Register to Ask a Question