Awk: check element in array and it's value

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Awk: check element in array and it's value
# 1  
Old 11-20-2017
Awk: check element in array and it's value

I want to see if element exists in array, if so then, check it's corresponding value.

Column 4 is position and column 1 is the chromosome for it. There are duplicates for one position on one chromosome.

I want to check if same position exists on different chromosome:

Data format:

2       rs267607987     0       47702411        0       T
2       seq-rs587779123 0       47702411        0       I
2       seq-rs587779124 0       47702411        0       D
11      seq-rs730880711 0       47364479        0       I
11      seq-rs863225110 0       47364479        0       I
11      seq-rs863225271 0       47364479        0       I
11      seq-rs397515973 0       47359006        0       D
11      seq-rs727503187 0       47359006        0       D
11      seq-rs730880654 0       47359006        0       D
17      DUP-rs80358150  0       41209068        0       C
17      rs273901754     0       41209068        0       D
17      rs80358150      0       41209068        0       C
17       seq-rs5827779124 0       47702411        0       D

I want to check if position column 4 has duplicates on different values for column 1.
in this case:
2       seq-rs587779124 0       47702411        0       D
17       seq-rs5827779124 0       47702411        0       D

Following code fails:

awk ' {if ($4 in arr) && if (arr[$4]==$1){ print arr[$4],$4} else {arr[$4]=$1} }' testcol.txt

awk:  {if ($4 in arr) && if (arr[$4]==$1){ print arr[$4],$4} else {arr[$4]=$1} }
awk:                  ^ syntax error

Would really appreciate any guidance here.

Moderator's Comments:
Mod Comment Please use CODE (not QUOTE) tags consistently as required by forum rules!

Last edited by RudiC; 11-20-2017 at 02:56 PM.. Reason: Added / changed CODE tags.
# 2  
Old 11-20-2017
Which of the three lines for chromosome 2 should be selected?
# 3  
Old 11-20-2017
It's not important which of the three lines are to be selected from chromosome 2. I'd like to see if same position is present across different chrosomes.
# 4  
Old 11-20-2017

For your code, put the && within the single if's parentheses:
awk ' {if (($4 in arr) && (arr[$4]==$1)) { print arr[$4],$4} else {arr[$4]=$1} }' file

But this won't print your desired result. Try
awk '($4 in LN) && ($1 != CHR[$4]) {print LN[$4]; print} {LN[$4] = $0; CHR[$4] = $1}' file
2       seq-rs587779124 0       47702411        0       D
17       seq-rs5827779124 0       47702411        0       D

# 5  
Old 11-20-2017
 awk ' {if (! $4 in arr){arr[$4]=$1} if ( ($4 in arr) && (arr[$4]!=$1 ) ){print "Isseu "} }' testcol.txt

I got the && working but the condition fails. Can't figure out why. I can copy paste your code, but would like to know bug in mine.
# 6  
Old 11-20-2017
Use parentheses around the first $4 in arr Right now, you check (!$4) in arr.
# 7  
Old 11-20-2017
I can't get your code working if I change the order.

 awk ' {if (! $4 in CHR){ CHR[$4]=$1; LN[$4]=$0 } { if ( ($4 in LN) && (CHR[$4] != $1 ) ){print } } }' testcol.txt

I'm totally lost what's wrong Smilie
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: count unique element of array

Hi, tab-separated input: blabla_1 A,B,C,C blabla_2 A,E,G blabla_3 R,Q,A,B,C,R,Q output: blabla_1 3 blabla_2 3 blabla_3 5 After splitting $2 in an array, I am trying to store the number of unique elements in a variable, but have some difficulties resetting the variable to 0 before... (6 Replies)
Discussion started by: beca123456
6 Replies

2. UNIX for Advanced & Expert Users

Array Element

This question is for someone that's more familiar with Array Element. I need to know if the maximum array element that can be assigned is 1024 and if its so, Is there a workaround solution when the counter exceeded 1024? param_array="$param_nam" counter=$counter+1 #to avoid space... (3 Replies)
Discussion started by: cumeh1624
3 Replies

3. Shell Programming and Scripting

awk Search Array Element Return Index

Can you search AWK array elements and return each index value for that element. For example an array named car would have index make and element engine. I want to return all makes with engine size 1.6. Array woulld look like this: BMW 1.6 BMW 2.0 BMW 2.5 AUDI 1.8 AUDI 1.6 ... (11 Replies)
Discussion started by: u20sr
11 Replies

4. Shell Programming and Scripting

Perl: How to check whether my array contains element x

Hi All, I am new to perl I am stuck in simple problem I need your help I want to define a subroutine. sub check_if_entity_exists(@array_to_be_checked,$entityName) I have array as http-listener-1 http-listener-2 http-listener-3 http-listener-4 If i send http-listener-3 my... (1 Reply)
Discussion started by: javaholics
1 Replies

5. Shell Programming and Scripting

HELP with AWK one-liner. Need to employ an If condition inside AWK to check for array variable ?

Hello experts, I'm stuck with this script for three days now. Here's what i need. I need to split a large delimited (,) file into 2 files based on the value present in the last field. Samp: Something.csv bca,adc,asdf,123,12C bca,adc,asdf,123,13C def,adc,asdf,123,12A I need this split... (6 Replies)
Discussion started by: shell_boy23
6 Replies

6. Shell Programming and Scripting

Multiplying array element

I am trying to take all the elements of an array and multiply them by 2, and then copy them to a new array. Here is what I have i=0 for true in DMGLIST do let DMGSIZES2="${DMGSIZES}"*2 let i++ done unset i echo ${DMGSIZES2} It does the calculation correctly for the first element,... (7 Replies)
Discussion started by: nextyoyoma
7 Replies

7. Shell Programming and Scripting

remove an element from array

I need to remove an element from the below array variable TABLENAME. #!/bin/ksh set -A TABLENAME "mirf roxar keke mirs" echo "the array is ${TABLENAME}" If i need to remove say keke and have the final TABLENAME as below, how this could be achieved. Pls throw some light. echo "Modified... (3 Replies)
Discussion started by: michaelrozar17
3 Replies

8. Shell Programming and Scripting

Help! Yet another check element in array Question

Greetings, DISCLAIMER: My shell scripting is rusty so my question may be borderline stupid. You've been warned. I need to create a script that a) lists the content of zip files in a directory and b) sends out an `exception` report. My ZIP files contain a control file (for load check). I want... (2 Replies)
Discussion started by: alan
2 Replies

9. Shell Programming and Scripting

How to check index of a array element in shell script?

Example - Script to find the index of a month from array MONTHS="Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec" set -A MON $MONTHS A="Sun May 23 09:34:30 GMT 2010" getMonth=`echo $A|cut -c5-7` ##getMonth=May Arrayindex_in_MONTHS_array= ???? # { 0,1,2,3,4 } - at fifth place ... (7 Replies)
Discussion started by: KuldeepSinghTCS
7 Replies

10. Shell Programming and Scripting

acessing awk array element while getline < "file"

I am attempting to write a awk script that reads in a file after awk array elements are assigned and using those elements while reading in the new file. Does this make sense? /pattern/ {tst=$3} (( getline < "file" ) > 0 ) { x=x " "tst } When I print tst in the END statement it... (9 Replies)
Discussion started by: timj123
9 Replies
Login or Register to Ask a Question

Featured Tech Videos