« first day (232 days earlier)      last day (2185 days later) » 

11:30 AM
Hi @terdon, do you use awk?
 
11:47 AM
yes
 
I have a small question to ask you
I have a one line code which I don't understand why it isn't working
I have a file with 18 columns. If lines of column 8 start with either G or C and lines of column 9 start with either A or T, and if lines of column 17 start with either G or C and lines of lines of column 18 start with either A or T, then I would like to print two new columns saying "@" and "@", as columns 19 and 20.
I have written this code:
  awk '{if (($8~/^G:/ && $9~/^A:/ || $8~/^G:/ && $9~/^T:/ || $8~/^C:/ && $9~/^A:/ || $8~/^C:/ && $9~/^T:/) && ($17~/^G:/ && $18~/^A:/ || $17~/^G:/ && $18~/^T:/ || $17~/^C:/ && $18~/^A:/ || $17~/^C:/ && $18~/^T:/)) {print $0,"M1=@","M2=@"}'
It gives no syntax error but doesn't seem to work
A curly bracket in the end is missing but it still the same
 
12:07 PM
@MaharshiChakravortee Sorry, but that's just too hard to parse. Do you mean this?
awk '{ if($8~/^[GC]/ && $9~/^[AT]/ && $17~/^[GC]/ && $18~/^[AT]/){print $0,"M1=@","M2=@"}}'
You can't just group so many conditions with && and || like that. I mean you can, but figuring out what it does is very hard. For instance, does if ((A && B || C && D )) mean "if (A and B) or (C and D)"? Or does it mean "if(A and (( B or C) and D)"`?
It comes down to operator precedence and I don't know how awk will do this off the top of my head. So just avoid such ambiguity and either group things with parentheses: if(( A || B) && (C || D))
Or use my version which uses character classes instead of all those ||
 
Awk puts && before ||
(I just googled, since I was curious)
Though whether an individual version actually adheres to that is an open question
 
@DevonRyan Yeah. I hate this sort of thing. I tend to always group explicitly just to avoid bugs. This type of error can be a real pain to track down.
 
@terdon I'm with you, a few extra characters for the sake of sanity never hurt anyone
 
12:26 PM
I learned that lesson here:
6
Q: Perl's "not" operator not working as expected with the defined() function

terdonThe following snippet is not working as expected: $k{"foo"}=1; $k{"bar"}=2; if(not defined($k{"foo"}) && not defined($k{"bar"})){ print "Not defined\n"; } else{ print "Defined" } Since both $k{"foo"} and $k{"bar"} are defined, the expected output is "Defined". Running the code, howeve...

 
Once bitten twice shy, as the saying goes
 
aye
 
1:09 PM
Aah I see
Thanks this helps!
Can I ask questions like this anytime or is this chat room restricted to some boundaries?
 
@MaharshiChakravortee It would be much better to ask on the site. This room isn't very active so you're not likely to get an answer here and, more importantly, if you ask here then you don't help anyone. If you ask on the site instead, other people can benefit from the answers you get (and the site benefits from having more useful content).
 
Understood! Thanks!
 

« first day (232 days earlier)      last day (2185 days later) »