-2

file1 contains many numbers like shown below:

345656565465
783329439432
345656565465
783329439432
.......

file2:

345656565465;ram;dfdfdf54532fdf;...............................
783329439432;sam;dfsdgg543546fg;...............................
768737228732;bheem;dfsdg5465464767g;...........................
34565656546575;hema;jsdsds45345455h;..........................

Desired Output: file1 should search the map in file2 and put in file3 (output) like this:

345656565465;ram;dfdfdf54532fdf;...............................
783329439432;sam;dfsdgg543546fg;...............................

So far, I've tried this:

$ awk -F'|' 'NR==FNR{c[$1$2]++;next};c[$1$2] > 0' file2 file1

Also tried using perl but I'm not sure if this is correct or not since it's not giving me my desired output:

perl -e 'open(A, "file2"); while(<A>){/.+?\|[^|]+/ && $k{$&}++};
while(<>){/.+?\|[^|]+/ && do{print if defined($k{$&})}}' file1

The problems I am facing with is that it should grep only that number but in my codes it is taking the big-number which consists the same pattern and it should paste in third file. please help me to achieve this :)

closed as unclear what you're asking by Anthon, slm Aug 7 '16 at 17:42

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    The answer to "Can you suggest me a better code?" can only be "Yes" or "No" Please update your post by indicating what the output of your awk and perl "solutions" and what is wrong with those. Also provide a real question (which has no Yes/No answer) – Anthon Aug 7 '16 at 10:28
  • by using AWK i am getting this output this is different from desired output. 783329439432;sam;dfsdgg543546fg;48384738473847983423433433;jkshsadhasjhdjsad;jhf‌​sdkjhfs;jdhsjd 783329439432;sam;dfsdgg543546fg;48384738473847983423433433;jkshsadhasjhdjsad;jhf‌​sdkjhfs;jdhsjd 783329439432;sam;dfsdgg543546fg;48384738473847983423433433;jkshsadhasjhdjsad;jhf‌​sdkjhfs;jdhsjd – aiswarya Aug 7 '16 at 15:22
  • What part of "update your post" is unclear to you? – Anthon Aug 7 '16 at 15:44
1

you almost make it

awk -F';' 'NR==FNR{c[$1]++;next ;} c[$1] > 0' file1 file2

where

  • -F';' use semi colon as separator (and not pipe)
  • use file1 first to catch number
  • I suppose you want to select on first column.
  • i tried yours also, but i got following output 783329439432;sam;dfsdgg543546fg;48384738473847983423433433;jkshsadhasjhdjsad;jhfsdkjhfs;jdhsjd 783329439432;sam;dfsdgg543546fg;48384738473847983423433433;jkshsadhasjhdjsad;jhfsdkjhfs;jdhsjd 783329439432;sam;dfsdgg543546fg;48384738473847983423433433;jkshsadhasjhdjsad;jhfsdkjhfs;jdhsjd – aiswarya Aug 7 '16 at 15:15
  • yes i want to compare with first column :) – aiswarya Aug 7 '16 at 15:28

Not the answer you're looking for? Browse other questions tagged or ask your own question.