common ids


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting common ids
# 1  
Old 09-05-2012
common ids

I have file1.txt

Quote:
1 2 a
3 4 b
2 3 b
1 1 c
1 1 a
I have file2.txt
Quote:
b
a
and I want to extract all the rows in file1.txt that have the same idsas file2.txt in the 3rd column in the file1.txt. so the output willl be

Quote:
1 1 a
1 2 a
2 3 b
3 4 b
I have tried
Code:
 
sort ${data}/13.txt > ${data}/13
sort -k3,3 ${data}/333.txt > ${data}/333
awk 'NR==FNR{a[$0];next}$3 in a' ${data}/13.txt ${data}/333.txt > ${data}/444

but it gives me

Quote:
1 2 a
3 4 b
1 1 a
Thanks

Moderator's Comments:
Mod Comment Please view this code tag video for how to use code tags when posting code and data. Last warning...

Last edited by vbe; 09-05-2012 at 11:59 AM..
# 2  
Old 09-05-2012
Quote:
Originally Posted by johnkim0806
Code:
 
sort ${data}/13.txt > ${data}/13
sort -k3,3 ${data}/333.txt > ${data}/333
awk 'NR==FNR{a[$0];next}$3 in a' ${data}/13.txt ${data}/333.txt > ${data}/444

What's the point of those sorts if the sorted results are never used?

The only thing you need is that awk statement with the relevant files in the correct order piped into a single sort with the necessary options.

Regards,
Alister
This User Gave Thanks to alister For This Post:
# 3  
Old 09-05-2012
Quote:
Originally Posted by alister
What's the point of those sorts if the sorted results are never used?

The only thing you need is that awk statement with the relevant files in the correct order piped into a single sort with the necessary options.

Regards,
Alister
What do you mean by correct order? So would it be just
awk 'NR==FNR{a[$0];next}$3 in a' file2.txt file3.txt?
# 4  
Old 09-05-2012
1) While still in the first file(the total number of lines and file line-number agree) load into array A.
2) Whenever the third column matches anything in A, print the line.
Code:
$ awk 'NR==FNR { A[$1]++; next } $3 in A' file2.txt file1.txt

1 2 a
3 4 b
2 3 b
1 1 a

$

This User Gave Thanks to Corona688 For This Post:
# 5  
Old 09-05-2012
Quote:
Originally Posted by Corona688
1) While still in the first file(the total number of lines and file line-number agree) load into array A.
2) Whenever the third column matches anything in A, print the line.
Code:
$ awk 'NR==FNR { A[$1]++; next } $3 in A' file2.txt file1.txt
 
1 2 a
3 4 b
2 3 b
1 1 a
 
$

Somthing must be wrong with the file that I have or something.
Thanks! if I used A[$0], it would do the same thing right?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

List of all ids,groups, privilege ids

I wish to pull out a list of all user ids on the system, including the privileged ids, the groups to which they belong to. Sometimes after deleting an id also, its home dir does not get deleted or an entry is left behind in /etc/passwd. Can someone help me with a script to achieve both. (2 Replies)
Discussion started by: ggayathri
2 Replies

2. Shell Programming and Scripting

Match ids

Hello, I have two files File 1 with 10 columns rsid position ........ xx 1:10000 File 2 position 1:10000 2:2000 .... I need to extract the IDs given in file 2(column1) from file 1 (column2) and print all columns from file1. I am trying this command (1 Reply)
Discussion started by: nans
1 Replies

3. Shell Programming and Scripting

Merging files with common IDs without JOIN

Hi, I am trying to merge information across 2 files. The first file is a "master" file, with all IDS. File 2 contains a subset of IDs of those in File 1. I would like to match up individuals in File 1 and File 2, and add information in File 2 to that of File 1 if they appear. However, if an... (3 Replies)
Discussion started by: hubleo
3 Replies

4. UNIX for Dummies Questions & Answers

Merge two files with common IDs but unequal number of rows

Hi, I have two files that I would like to merge and think that there should be a solution using awk. The files look something like this: file 1 IDX1 IDY1 IDX2 IDY2 IDX3 IDY3 file 2 IDY1 dataA data1 IDY2 dataB data2 IDY3 dataC data3 Desired output IDX1 IDY1 dataA data1 IDX2 ... (5 Replies)
Discussion started by: katie8856
5 Replies

5. AIX

Multiple IDs

Hello Guys, We've around 20 machines & I've root access for all of them. Also, we have around 4 different ID's in these machines. I have to change the passwords every month, I was wondering is there a way to change the password for multiple IDs at one shot? Thanks, (1 Reply)
Discussion started by: AbhijithS
1 Replies

6. Cybersecurity

Bayesian IDS

Hi there, I am working on Anomaly based Network IDS... Statistical based technique is simple but not quite effective in real scenario... I understand Bayesian classifier/Network is more effective in the context of anomaly detection, but i have very little idea about Bayesian approach for... (0 Replies)
Discussion started by: Dinakara
0 Replies

7. Shell Programming and Scripting

Migrating IDs

I need a script that will move files and change the ownership from a user's old home directory to a new home directory on multiple NIS+ servers. (0 Replies)
Discussion started by: mackdaddy07
0 Replies

8. UNIX for Dummies Questions & Answers

can't su to any IDs

hello friends, i had this problem couldn't figure out. Every time i tried to su to an id by this command. it gave all killed command after that. Can't use it at all. ( OS Irix ). Any help greatly appreciated. ========= server 23# su - hbl3121 Killed Killed Killed Killed Killed Killed... (2 Replies)
Discussion started by: anphdula
2 Replies

9. UNIX for Dummies Questions & Answers

Ids

What's the relationship beteew IDS and informix ? (1 Reply)
Discussion started by: wuhuatao
1 Replies

10. UNIX for Advanced & Expert Users

Creating IDs on HPUX 11.x

HPUX 11.0. Want to create a set of IDs (about 4 or them) for various teams to use to logon to the server. However, client does not want teams to logon directly to the server (via console, telnet etc) with these IDs. Rather, they want the teams to logon with their own ID then su to the new ID. IDs... (3 Replies)
Discussion started by: google
3 Replies
Login or Register to Ask a Question