Array V-Lookup using UNIX bash


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Array V-Lookup using UNIX bash
# 8  
Old 04-30-2018
RudyC,


Would the following modification to your script be adequate if the input file IN1 was like this instead:

Code:
awk 'NR == FNR {REP[$1] = $4; next} $1 in REP && FNR > 1 { $4 = REP[$1]; CNT++} 1; END {print CNT}' OFS="\t"

Code:
rsid    chromosome    position    alleles
rs4471    1    82154    TT
rs313    1    752721    GG
rs125    1    768448    GG
rs112    1    798959    GG
rs668    1    800007    CC
rs497    1    838555    AA
rs447    1    846808    TC
rs753    1    854250    AG
rs1330    1    861808    GG
rs111    1    873558    TG
rs171    1    887162    TT

And the desired output OUT needed to be :

Code:
rsid    chromosome    position    alleles
rs4471    1    82154    AA
rs313    1    752721    GG
rs125    1    768448    AG
rs112    1    798959    GG
rs668    1    800007    CC
rs497    1    838555    CC
rs447    1    846808    TC
rs753    1    854250    AG
rs1330    1    861808    AA
rs111    1    873558    TG
rs171    1    887162    TT


Last edited by Geneanalyst; 04-30-2018 at 12:37 AM..
# 9  
Old 04-30-2018
Hello Geneanalyst,

If your Input_file is same as shown samples then following may help you on same.
Code:
awk 'FNR==NR&&FNR>1{a[$1]=$NF;next} FNR>1{if(a[$1]){$NF=$(NF-1)=""} else {$(NF-1)=OFS $(NF-1) $NF;$NF=""};print $0 a[$1]}' OFS="\t" in2 in1

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 10  
Old 04-30-2018
What's the result you get when running your adapted code?
# 11  
Old 05-01-2018
Quote:
Originally Posted by RudiC
What's the result you get when running your adapted code?
Looks like it works.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash lookup matching digits for secong file

In the bash below the user selects the file to be used. The digits of each file are unique and used to automatically locate the next file to be used in the process. The problem I can not seem to fix is that the full path needs to be referenced in the second portion and it is not currently. Is... (7 Replies)
Discussion started by: cmccabe
7 Replies

2. Shell Programming and Scripting

Bash arrays: rebin/interpolate smaller array to large array

hello, i need a bit of help on how to do this effectively in bash without a lot of extra looping or massive switch/case i have a long array of M elements and a short array of N elements, so M > N always. M is not a multiple of N. for case 1, I want to stretch N to fit M arrayHuge H = (... (2 Replies)
Discussion started by: f77hack
2 Replies

3. Shell Programming and Scripting

IP Address LookUp Bash Script

I am new to bash scripting. I want write a script that reads from the first argument file and run nslookup, then prints out each nslookup. Something like below: File name = ip 8.8.8.8 8.8.4.4 Bash shell script: nslookup.sh #!/bin/bash for i in $1 do nslookup $i done I... (7 Replies)
Discussion started by: boldnbeautiful
7 Replies

4. Shell Programming and Scripting

Bash 3.2 - Array / Regex - IF 3rd member in array ends in 5 digits then do somthing...

Trying to do some control flow parsing based on the index postion of an array member. Here is the pseudo code I am trying to write in (preferably in pure bash) where possible. I am thinking regex with do the trick, but need a little help. pesudo code if == ENDSINFIVEINTS ]]; then do... (4 Replies)
Discussion started by: briandanielz
4 Replies

5. UNIX for Dummies Questions & Answers

Lookup in Unix

Hi, I have an input file which contain below records: a,1 b,2 c,3 a,10 b,34 i have a reference file which contains below records: a,AA b,BB c,CC My required output is : (3 Replies)
Discussion started by: pandeesh
3 Replies

6. Shell Programming and Scripting

Problem with lookup values on AWK associative array

I'm at wits end with this issue and my troubleshooting leads me to believe it is a problem with the file formatting of the array referenced by my script: awk -F, '{if (NR==FNR) {a=$4","$3","$2}\ else {print a "," $0}}' WBTSassignments1.txt RNCalarms.tmp On the WBTSassignments1.txt file... (2 Replies)
Discussion started by: JasonHamm
2 Replies

7. UNIX for Advanced & Expert Users

Clueless about how to lookup and reverse lookup IP addresses under a file!!.pls help

Write a quick shell snippet to find all of the IPV4 IP addresses in any and all of the files under /var/lib/output/*, ignoring whatever else may be in those files. Perform a reverse lookup on each, and format the output neatly, like "IP=192.168.0.1, ... (0 Replies)
Discussion started by: choco4202002
0 Replies

8. UNIX for Dummies Questions & Answers

lookup in unix

i am having one file which is of CSV format with two fields client_id and client_nbr.The sample data is lke below ABC,1250 CDE,1520 EFG,1000 PQR,1800 The client nbr for these clients change frequently.So i want to create one lookup file every week for the changed client and run a script... (3 Replies)
Discussion started by: dr46014
3 Replies

9. Shell Programming and Scripting

lookup in unix

Hi All I have got a fixed length file of 80bytes long.The first 4bytes of each record represents a client_number.I need to modify the client number based on another lookup file. The lookup file contains 2 fields and a comma delimited file.The first line of the lookup file contains the header... (5 Replies)
Discussion started by: dr46014
5 Replies

10. UNIX for Dummies Questions & Answers

Unix 8.2 and reverse Lookup

We have Unix configured as our external DNS, forward DNS is working properly, however Reverse lookup is not working. Any idea what the problem is? I have checked the named.boot and .rev file and everything seems to be correctly. However it appears that the reverse zone file in the named.boot... (2 Replies)
Discussion started by: cassy
2 Replies
Login or Register to Ask a Question