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

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

nokia1100
iphone678
nokia4200
cherry123

output

nokia1100    123,000
iphone678    123
nokia4200    23,645
cherry123    67,456
$ 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

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

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

cherry123 67,456

Hi,

It's working fine at my end.

[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
1 Like

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.

Explanation:

awk 'NR==FNR{a[$1]=$2;next}
a[$1] {print $1,a[$1]}' item.txt list.txt
'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.

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

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

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

Nokia N70
Nokia 1208
Nokia 1200
Nokia 2600
Nokia 1100
Nokia 1202
Nokia 1600

OUTPUT

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

Something like this,

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

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.

1 Like

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.