Extracting data based on the list file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extracting data based on the list file
# 8  
Old 08-17-2010
Explanation:

Code:
awk 'NR==FNR{a[$1]=$2;next}
a[$1] {print $1,a[$1]}' item.txt list.txt

Code:
'NR==FNR{a[$1]=$2;next}

When we read the 1st file we fill an array a and use $1 as index and $2 as the value.

Code:
a[$1] {print $1,a[$1]}

The 2nd line affects for file2. If the first fields of file 2 exist as an index in array a, then print
# 9  
Old 08-17-2010
Hi Pravin27,

I tried your script again, with this ff data but it fails. Can you check this again see below. The objective is to extract the data in nokia.txt that is listed in the nokia_list.txt.

nokia.txt
Code:
Nokia 3210 +639061386552
Nokia 3595 +639161943423
Nokia 2600 +639156758624
Nokia 2600 +639156758625
Nokia 1110i +639058719931
Nokia 3210 +639173368075
Nokia 2610 +639054097758
Nokia 1108b +639051729842
Nokia 8250 +639261171277
Nokia 1112 +639265985233
Nokia 1200 +639261703605
Nokia 1208 +639161389093
Nokia 1208 +639066964450
Nokia 1209 +639057568016
Nokia 3315 +639061668207
Nokia 1650 +639059230575
Nokia 2650 +639164681369
Nokia 1110i +639161155060
Nokia 8250 +639067906311
Nokia 1100 +639261285985
Nokia 1100 +639261285986
Nokia 1100 +639261285987
Nokia 1200 +639067631317
Nokia 1209 +639066777134
Nokia 1202 +639061222194
Nokia 1202 +639061222195
Nokia 1202 +639061222196
Nokia 3315 +639161214625
Nokia 3310 +639157979655
Nokia 1110i +639061290758
Nokia 1112 +639261681121
Nokia 1100 +639261607761
Nokia 1600 +639052317292
Nokia 1600 +639052317292
Nokia 1600 +639052317292
Nokia 1600 +639052317292
Nokia 1110i +639057106433
Nokia 3100 +639066554084
Nokia 3310 +639156816378
Nokia 1600 +639176186724
Nokia 1112 +639169593390
Nokia N70 +639067663152
Nokia 1112 +639061923830
Nokia 3595 +639057612141
Nokia 3330 +639277879791
Nokia 1110i +639263933069

nokia_list.txt
Code:
Nokia N70
Nokia 1208
Nokia 1200
Nokia 2600
Nokia 1100
Nokia 1202
Nokia 1600

OUTPUT
Code:
Nokia N70 +639067663152
Nokia 1208 +639161389093
Nokia 1208 +639066964450
Nokia 1200 +639261703605
Nokia 2600 +639156758624
Nokia 2600 +639156758625
Nokia 1100 +639261607761
Nokia 1202 +639061222194
Nokia 1202 +639061222195
Nokia 1202 +639061222196
Nokia 1600 +639052317292
Nokia 1600 +639052317292
Nokia 1600 +639052317292
Nokia 1600 +639052317292

# 10  
Old 08-17-2010
Something like this,

Code:
awk 'NR==FNR{a[$1$2]=$0;next} a[$1$2] {print $0}' nokia_list.txt nokia.txt

This User Gave Thanks to pravin27 For This Post:
# 11  
Old 08-17-2010
Hi Pravin27,

Great, it is working. Can you tell me how this one works?
It really good to know.

Thank you!
# 12  
Old 08-17-2010
Quote:
Originally Posted by shtobias
Hi Pravin27,

Great, it is working. Can you tell me how this one works?
It really good to know.

Thank you!
Do you try my command, in your case, grep -f will be more simple.
This User Gave Thanks to rdcwayx For This Post:
# 13  
Old 08-17-2010
Hi,

Yes, i tried now the grep -f and it is working as well. Thank you.

the list file should comes first before the actual data.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

In PErl script: need to read the data one file and generate multiple files based on the data

We have the data looks like below in a log file. I want to generat files based on the string between two hash(#) symbol like below Source: #ext1#test1.tale2 drop #ext1#test11.tale21 drop #ext1#test123.tale21 drop #ext2#test1.tale21 drop #ext2#test12.tale21 drop #ext3#test11.tale21 drop... (5 Replies)
Discussion started by: Sanjeev G
5 Replies

2. UNIX for Dummies Questions & Answers

Extracting data from one file, based on another file (splitting)

Dear All, I have two files but want to extract data from one based on another... can you please help me file 1 David Tom Ellen and file 2 David|0010|testnamez|resultsz David|0004|testnamex|resultsx Tom|0010|testnamez|resultsz Tom|0004|testnamex|resultsx Ellen|0010|testnamez|resultsz... (12 Replies)
Discussion started by: A-V
12 Replies

3. Shell Programming and Scripting

Script for extracting data from csv file based on column values.

Hi all, I am new to shell script.I need your help to write a shell script. I need to write a shell script to extract data from a .csv file where columns are ',' separated. The file has 5 columns having values say column 1,column 2.....column 5 as below along with their valuesm.... (3 Replies)
Discussion started by: Vivekit82
3 Replies

4. Shell Programming and Scripting

Generate tabular data based on a column value from an existing data file

Hi, I have a data file with : 01/28/2012,1,1,98995 01/28/2012,1,2,7195 01/29/2012,1,1,98995 01/29/2012,1,2,7195 01/30/2012,1,1,98896 01/30/2012,1,2,7083 01/31/2012,1,1,98896 01/31/2012,1,2,7083 02/01/2012,1,1,98896 02/01/2012,1,2,7083 02/02/2012,1,1,98899 02/02/2012,1,2,7083 I... (1 Reply)
Discussion started by: himanish
1 Replies

5. Shell Programming and Scripting

extracting lines based on condition and copy to another file

hi i have an input file that contains some thing like this aaa acc aa abc1 1232 aaa abc2.... poo awq aa abc1 aaa aaa abc2 bbb bcc bb abc1 3214 bbb abc3.... bab bbc bz abc1 3214 bbb abc3.... vvv ssa as abc1 o09 aaa abc4.... azx aaq aa abc1 900 aqq abc19.... aaa aa aaaa abc1 899 aa... (8 Replies)
Discussion started by: anurupa777
8 Replies

6. Shell Programming and Scripting

Extracting specific lines of data from a file and related lines of data based on a grep value range?

Hi, I have one file, say file 1, that has data like below where 19900107 is the date, 19900107 12 144 129 0.7380047 19900108 12 168 129 0.3149017 19900109 12 192 129 3.2766666E-02 ... (3 Replies)
Discussion started by: Wynner
3 Replies

7. Shell Programming and Scripting

Extracting data between tags based on search string from unix file

Input file is on Linux box and the input file has data in just one line with 1699741696 characters. Sample Input: <xxx><document coll="uspatfull" version="0"><CMSdoc>xxxantivirus</CMSdoc><tag1>1</tag1></document><document coll="uspatfull"... (5 Replies)
Discussion started by: gaya
5 Replies

8. Shell Programming and Scripting

Extract data based on match against one column data from a long list data

My input file: data_5 Ali 422 2.00E-45 102/253 140/253 24 data_3 Abu 202 60.00E-45 12/23 140/23 28 data_1 Ahmad 256 7.00E-45 120/235 140/235 22 data_4 Aman 365 8.00E-45 15/65 140/65 20 data_10 Jones 869 9.00E-45 65/253 140/253 18... (12 Replies)
Discussion started by: patrick87
12 Replies

9. Shell Programming and Scripting

Extracting lines in file based on time

Hi, anyone has any ideas on how do we extract lines from a file with format similiar to this: (based on current time) Jun 18 00:16:50 .......... ............. ............ Jun 18 00:17:59 .......... ............. ............ Jun 18 01:17:20 .......... ............. ............ Jun 18... (5 Replies)
Discussion started by: faelric
5 Replies

10. Shell Programming and Scripting

Extracting data from text file based on configuration set in config file

Hi , a:) i have configuration file with pattren <Range start no>,<Range end no>,<type of records to be extracted from the data file>,<name of the file to store output> eg: myfile.confg 9899000000,9899999999,DATA,b.dat 9899000000,9899999999,SMS,a.dat b:) Stucture of my data file is... (3 Replies)
Discussion started by: suparnbector
3 Replies
Login or Register to Ask a Question