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
# 1  
Old 08-17-2010
Extracting data based on the list file

Hi there,

Can you help. I need to extract data based on the list file(list.txt) from item.txt as shown below. Please note the actual files are enormous in size. Thank you.

item.txt
Code:
nokia1100    123,000
nokia2100    66,000
samsung123    11,000
samsung456    23,000
iphone432    234,000
iphone678    123
cherry679    11,000
cherry123    67,456
nokia660    345,222
nokia_series    123,321
nokia4200    23,645
samsung_series    23,978

list.txt
Code:
nokia1100
iphone678
nokia4200
cherry123

output
Code:
nokia1100    123,000
iphone678    123
nokia4200    23,645
cherry123    67,456

# 2  
Old 08-17-2010
Code:
$ awk '
    FNR==NR{arr[$1]=$1;next}
    $1 in arr{print arr[$1],$2}
' list.txt file.txt
nokia1100 123,000
iphone678 123
cherry123 67,456
nokia4200 23,645

And a different solution:
$ while read modelnum; do awk -v M="$modelnum" '$1==M {print}' file.txt; done < list.txt
nokia1100    123,000
iphone678    123
nokia4200    23,645
cherry123    67,456

# 3  
Old 08-17-2010
Code:
awk 'NR==FNR{a[$1]=$2;next}
a[$1] {print $1,a[$1]}' item.txt list.txt

# 4  
Old 08-17-2010
Code:
grep -f list.txt item.txt

# 5  
Old 08-17-2010
Hi pravin27,

i tried your script, but the output is only cherry123 67,456.

Can you check please. Thanks.

---------- Post updated at 02:27 PM ---------- Previous update was at 02:25 PM ----------

Hi jaduks,

I tried your 1st recomendation but the output is only one line
Code:
cherry123 67,456

# 6  
Old 08-17-2010
Hi,

It's working fine at my end.


Code:
[root@powerbroker pravin]# cat f3
nokia1100    123,000
nokia2100    66,000
samsung123    11,000
samsung456    23,000
iphone432    234,000
iphone678    123
cherry679    11,000
cherry123    67,456
nokia660    345,222
nokia_series    123,321
nokia4200    23,645
samsung_series    23,978

[root@powerbroker pravin]# cat f4
nokia1100
iphone678
nokia4200
cherry123
test


[root@powerbroker pravin]# awk 'NR==FNR{a[$1]=$2;next}
a[$1] {print $1,a[$1]}' f3 f4
nokia1100 123,000
iphone678 123
nokia4200 23,645
cherry123 67,456

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

Thanks, it is working indeed. Appreciate if you explain what this awk line does?


regards
Sherwin

---------- Post updated at 03:17 PM ---------- Previous update was at 02:59 PM ----------

Hi Jaduks,

Yes your script is working as well, fedora 10 has problem running this so i switch to RHEL instead, and now it working.

Can you shed light as well at what does your script actually does? Thank you.
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

Featured Tech Videos