Extract records from list


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract records from list
# 1  
Old 10-25-2012
Extract records from list

Hi Gents,

I have a file 1 like this
Code:
1 1000 20
2 2000 30
3 1000 40
5 1000 50

And I have other file 1 like
Code:
2 1

I would like to get from the file 1 the complete line which are in file 2, the key to compare is the column 2 then output should be.

Code:
2 2000 30.

I was trying to get it usin the command join but i can'tSmilie
Code:
join file1 file2 -a 1.....

Thanks in advance for your help/

---------- Post updated at 03:14 AM ---------- Previous update was at 03:13 AM ----------

sorry the key to compare the files is the colunm 1 Smilie


Moderator's Comments:
Mod Comment Please use code tags for code and data

Last edited by Scrutinizer; 10-25-2012 at 05:50 AM.. Reason: code tags
# 2  
Old 10-25-2012
Code:
awk 'FNR==NR{a[$1]=$0;next}{print a[$1]}' file1 file2

This User Gave Thanks to pamu For This Post:
# 3  
Old 10-25-2012
Thanks a lot for your help,, the code works perfect
# 4  
Old 10-25-2012
@Pamu,can you explain this one
# 5  
Old 10-25-2012
Quote:
Originally Posted by bmk
@Pamu,can you explain this one
Code:
awk 'FNR==NR{a[$1]=$0;next}   # Here we read first file and stores $0 into an array a with index $1

{print a[$1]}' file1 file2   # Here we print array a using $1 of file 2. I assumed that file2 contains all the records from file1. 

So no need to check the presence of a[$1].

Hope this helps you..Smilie
# 6  
Old 10-25-2012
Thank lot @pamu. Now it'sclear
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extract Matched Records from XML

Hi All, I have a requirement to extract para in XML file on the basis of another list file having specific parameters. I will extract these para from XML and import in one scheduler tool. file2 <FOLDER DATACENTER="ControlMserver" VERSION="800" PLATFORM="UNIX" FOLDER_NAME="SH_AP_INT_B01"... (3 Replies)
Discussion started by: looney
3 Replies

2. Shell Programming and Scripting

Extract UNIque records from File

Hi, I have a file with 20GB Pipe Delimited file where i have too many duplicate records. I need an awk script to extract the unique records from the file and put it into another file. Kindly help. Thanks, Arun (1 Reply)
Discussion started by: Arun Mishra
1 Replies

3. Shell Programming and Scripting

ksh coding to extract records from file

Hello, I have a file with various records in it (from length 30 - 195) and I want to run a script to read each line and copy only the recl=80 files to an output file. Any help much appreciated (4 Replies)
Discussion started by: Grueben
4 Replies

4. Shell Programming and Scripting

Compare Records between to files and extract it

I am not an expert in awk, SED, etc... but I really hope there is a way to do this, because I don't want to have to right a program. I am using C shell. FILE 1 FILE 2 H0000000 H0000000 MA1 MA1 CA1DDDDDD CA1AAAAAA MA2 ... (2 Replies)
Discussion started by: jclanc8
2 Replies

5. Shell Programming and Scripting

Extract data from records that match pattern

Hi Guys, I have a file as follows: a b c 1 2 3 4 pp gg gh hh 1 2 fm 3 4 g h i j k l m 1 2 3 4 d e f g h j i k l 1 2 3 f 3 4 r t y u i o p d p re 1 2 3 f 4 t y w e q w r a s p a 1 2 3 4 I am trying to extract all the 2's from each row. 2 is just an example... (6 Replies)
Discussion started by: npatwardhan
6 Replies

6. Shell Programming and Scripting

Extract CSV records using NAWK?

Example CSV: $ cat myfile HDR COL_A,COL_B,COL_C X,Y,Z Z,Y,X ... X,W,Z In this example, I know that column names are on the second line. I also know that I would like to print lines where COL_A="X" and COL_C="Z". In this simple example, I know that COL_A = $1 and COL_C = $3, and hence... (6 Replies)
Discussion started by: cs03dmj
6 Replies

7. UNIX for Dummies Questions & Answers

Extract records by column value - file non-delimited

the data in my file is has no delimiters. it looks like this: H52082320024740010PH333200612290000930 0.0020080131 D5208232002474000120070306200703060580T1502 TT 1.00 H52082320029180003PH333200702150001 30 100.0020080205 D5208232002918000120070726200707260580T1502 ... (3 Replies)
Discussion started by: jclanc8
3 Replies

8. Shell Programming and Scripting

Need to Extract Data From 94000 records

i have a input file which does not have a delimiter All i Need to do is to identify a line and extract the data from it and run the loop again and need to ensure that it was not extracted earlier Input file ------------ abcd 12345 egfhijk ip 192.168.0.1 CNN.com abcd 12345 egfhijk ip... (12 Replies)
Discussion started by: vasimm
12 Replies

9. Shell Programming and Scripting

extract set of matching records

i have a pipe delimited file with records spread in many lines. i need to extract those records 1)having X in beginning of that record 2)and having at least one Y in beginning before other record begins eg: X|Rec1| A|Rec1| Y|Rec1| X|Rec2| Y|Rec2| Z|Rec3| X|Rec4| M|Rec4| ... (4 Replies)
Discussion started by: finder255
4 Replies

10. UNIX for Dummies Questions & Answers

How to extract duplicate records with associated header record

All, I have a task to search through several hundred files and extract duplicate detail records and keep them grouped with their header record. If no duplicate detail record exists, don't pull the header. For example, an input file could look like this: input.txt HA D1 D2 D2 D3 D4 D4... (17 Replies)
Discussion started by: run_eim
17 Replies
Login or Register to Ask a Question