[Solved] How to extract single and duplicate lines from file?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers [Solved] How to extract single and duplicate lines from file?
# 1  
Old 10-09-2013
[Solved] How to extract single and duplicate lines from file?

Hi,

I need help!

I have two files, one containing a list of codes and the other a list of codes and their meaning. I need to extract from file 2 all the codes from file 1 into a new file.

These are my files:

File1:
Code:
Metbo
Metbo
Memar
Mth
Metbo

File2:
Code:
Metbo Methanoculleus
Memar Methanobacterium
Mth Methanoregula
CLOS Clostridium
PS Pseudomona

Output file should be like:
Code:
Metbo Methanoculleus
Metbo Methanoculleus
Memar Methanobacterium
Mth Methanoregula
Metbo Methanoculleus

I tried with grep -f file1 file2 > outfile

But in the outfile I only get one hit from Metbo and not 3.

Any suggestions.

Thanks in advanced Smilie

Last edited by vbe; 10-09-2013 at 09:56 AM.. Reason: code tags next time please!
# 2  
Old 10-09-2013
Code:
join <(sort file1) <(sort file2) > outfile

cat outfile
Memar Methanobacterium
Metbo Methanoculleus
Metbo Methanoculleus
Metbo Methanoculleus
Mth Methanoregula

This User Gave Thanks to in2nix4life For This Post:
# 3  
Old 10-09-2013
or if keeping order is important:
Code:
>outfile; while read GG;do grep $GG file2 >>outfile;done<file1

# 4  
Old 10-09-2013
Thanks in2nix4life it worked perfectly Smilie
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

[SOLVED] remove lines that have duplicate values in column two

Hi, I've got a file that I'd like to uniquely sort based on column 2 (values in column 2 begin with "comp"). I tried sort -t -nuk2,3 file.txtBut got: sort: multi-character tab `-nuk2,3' "man sort" did not help me out Any pointers? Input: Output: (5 Replies)
Discussion started by: pathunkathunk
5 Replies

2. Shell Programming and Scripting

[Solved] Need to add Single quotes to particular lines

Hi Guys, I have the following file. DbName=DBNAME DbUser=USERID DbPass=PASSLL SrcLocation=/appl/data/VSTAR SrcFile1=gmb_dly_ind_sls_20120410133424.txt SrcFile2= IpLocation=/appl/data/VSTAR/global_daily/input/GMB IpFile=gmb_dly_ind_sls_20120410133424.txt... (4 Replies)
Discussion started by: mac4rfree
4 Replies

3. Shell Programming and Scripting

Removing duplicate records in a file based on single column explanation

I was reading this thread. It looks like a simpler way to say this is to only keep uniq lines based on field or column 1. https://www.unix.com/shell-programming-scripting/165717-removing-duplicate-records-file-based-single-column.html Can someone explain this command please? How are there no... (5 Replies)
Discussion started by: cokedude
5 Replies

4. Shell Programming and Scripting

Removing duplicate records in a file based on single column

Hi, I want to remove duplicate records including the first line based on column1. For example inputfile(filer.txt): ------------- 1,3000,5000 1,4000,6000 2,4000,600 2,5000,700 3,60000,4000 4,7000,7777 5,999,8888 expected output: ---------------- 3,60000,4000 4,7000,7777... (5 Replies)
Discussion started by: G.K.K
5 Replies

5. UNIX for Advanced & Expert Users

In a huge file, Delete duplicate lines leaving unique lines

Hi All, I have a very huge file (4GB) which has duplicate lines. I want to delete duplicate lines leaving unique lines. Sort, uniq, awk '!x++' are not working as its running out of buffer space. I dont know if this works : I want to read each line of the File in a For Loop, and want to... (16 Replies)
Discussion started by: krishnix
16 Replies

6. Shell Programming and Scripting

Awk: How to merge duplicate lines and print in a single

The input file: >cat module1 200611051053 95 200523457498 35 200617890187 57 200726098123 66 200645676712 71 200744556590 68 >cat module2 200645676712 ... (10 Replies)
Discussion started by: winter9
10 Replies

7. Shell Programming and Scripting

Extract single word from lines

Hi I have a huge line like the following: this word and many other words AA122134 other and other words and AA224466 and other other other other word AA667210 words I only want extract the words AA?????? and put them in a column, how could i do ? thx so much! (7 Replies)
Discussion started by: AdminLew
7 Replies

8. UNIX for Advanced & Expert Users

What is command to extract single file from an archieve?

I just want to extract one sigle file from an .ear archieve instead of extracting whole ear. Can anyone help me on this? (4 Replies)
Discussion started by: harshal_dcx
4 Replies

9. UNIX for Dummies Questions & Answers

tar command help -- extract single file

Hi, I want to view/display the contents of one file in tar file. For example if the tar file is sam.tar.gz and one of the file inside is E1.txt, how do i view the contents of this E1.txt file. Olso if I want to extract the E1.txt file only from sam.tar.gz how can i do that. Thanks in... (7 Replies)
Discussion started by: icefish
7 Replies

10. Shell Programming and Scripting

tar: extract single file to different filepath

Hi, This is my first post here - I'm hoping I can get some help! I have searched these forums and othersand not getting anything that works. I am trying to extract a single file from a tar archive to a diffierent location than it will default to. For example my tar log shows me ... a... (3 Replies)
Discussion started by: littleIdiot
3 Replies
Login or Register to Ask a Question