Guest User

copy

a guest
Apr 8th, 2019
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.44 KB | None | 0 0
  1. employee :
  2. ====================================
  3. 101*Ajay*ajay@gmail.com*12/08/2017*Bilaspur$
  4. 102*John*john@gmail.com*21/01/2018*Raipur$
  5. 103*Sneha*sneha@gmail.com*08/12/2017*Raipur$
  6. 104*Ramesh*ramesh@gmail.com*12/01/2017*Korba$
  7. 105*mahesh*mahesh@gmail.com*12/01/2019*Delhi$
  8. ====================================
  9. project :
  10. ====================================
  11. 201*ProjA*Bilaspur$
  12. 202*ProjB*Raipur$
  13. 203*ProjC*Delhi$
  14. ====================================
  15. empproject :
  16. ========================================
  17. 101*201*01/05/2017$
  18. 102*202*01/10/2018$
  19. 103*201*01/05/2017$
  20. 104*203*01/08/2017$
  21. 105*203*01/08/2018$
  22. ========================================
  23.  
  24. 1. List of Employees and project details.
  25. $ awk 'BEGIN{RS="$";FS="*";i=0;j=0;k=0}
  26.  
  27. FNR==NR{eid[i]=$1;name[i]=$2;city[i]=$5;i++}
  28. FNR!=NR{eidp[k]=$1;pid[k]=$2;k++}
  29. END{ while (i > 0) {if(eid[j]==eidp[j]) {if(name[j] && pid[j]) { print
  30. eid[j]"*"name[j]"*"city[j]"*"pid[j]"$"}};j++;i--}}
  31.  
  32. Linux file:///home/ajay/Desktop/test.html
  33.  
  34. 1 of 4 03/04/19, 11:46 pm
  35.  
  36. ' employee empproject > empprjdet
  37. The new file "empprjdet" Contains following data :
  38. ==================
  39. 101*Ajay*Bilaspur*201$
  40. 102*John*Raipur*202$
  41. 103*Sneha*Raipur*201$
  42. 104*Ramesh*Korba*203$
  43. 105*mahesh*Delhi*203$
  44. ==================
  45. The below awk command compare employee's project details with project
  46. details and generate a new file "empprjdetail"
  47. $ awk 'BEGIN{RS="$";FS="*";i=0;j=0;k=0}
  48.  
  49. FNR==NR{eid[i]=$1;name[i]=$2;city[i]=$3;pid[i]=$4;i++}
  50. FNR!=NR{ppid[k]=$1;prj[k]=$2;pcity[k]=$3;k++}
  51. END{ while (i> 0){ l=k;m=0;while (l > 0){if(pid[j]==ppid[m]){print
  52. eid[j]"*"name[j]"*"city[j]"*"pid[j]"*"prj[m]"*"pcity[m]"$"};m++;l--};j++;i--}
  53. }
  54. ' empprjdet project | sed '/^$/d' > empprjdetail
  55. At the end sed command is used to remove blank spaces
  56. empprjdetail
  57. ===================
  58. 101*Ajay*Bilaspur*201*ProjA*Bilaspur$
  59. 102*John*Raipur*202*ProjB*Raipur$
  60. 103*Sneha*Raipur*201*ProjA*Bilaspur$
  61. 104*Ramesh*Korba*203*ProjC*Delhi$
  62. 105*mahesh*Delhi*203*ProjC*Delhi$
  63. ===================
  64. $ awk 'BEGIN{FS="*";RS="$"}{print $1,$2,$3,$4,$5}' empprjdetail | sed
  65. '/^$/d'
  66. =========================
  67. 101 Ajay Bilaspur 201 ProjA
  68. 102 John Raipur 202 ProjB
  69. 103 Sneha Raipur 201 ProjA
  70. 104 Ramesh Korba 203 ProjC
  71. 105 mahesh Delhi 203 ProjC
  72. =========================
  73. Linux file:///home/ajay/Desktop/test.html
  74.  
  75. 2 of 4 03/04/19, 11:46 pm
  76.  
  77. 2. Find which projects engaged maximum number of employees.
  78. $ awk 'BEGIN{RS="$";FS="*";i=0;j=0;k=0;m=0;prj[0];empid[0];empp[0];
  79. ct[0]}
  80. FNR==NR{if(NF==3){prj[i]=$2;i++}}
  81. FNR!=NR{if(NF==6){empid[j]=$1;empp[j]=$5;j++}}END{while(j>0)
  82. {m=i;n=0;while(m >= 0) { if(empp[k]==prj[n]){ct[n]+=1};n++;m--};
  83. k++;j--}}
  84. END{ctr=0;print "Project Name\tNumber of Employees";while(ctr <
  85. 3){print prj[ctr]"\t\t\t"ct[ctr];ctr++}}
  86. ' project empprjdetail
  87. Output :
  88. ================================
  89. Project Name Number of Employees
  90. ProjA 2
  91. ProjB 1
  92. ProjC 2
  93. ================================
  94. Notes : It first take the data from previously created file empprjdetail.
  95.  
  96. 4. Find the employee who works in their own city.
  97. awk 'BEGIN{RS="$";FS="*";name[0];id[0];i=0;j=0}$3==$6{id[i]=$1;
  98. name[i]=$2;i++}END{while(i>0){print id[j]" "name[j];i--;j++}}' empprjdetail
  99. | sed '/^$/d'
  100. 101 Ajay
  101. 102 John
  102. 105 mahesh
  103. 5. Find the employee who does not works in their own city.
  104. awk 'BEGIN{RS="$";FS="*";name[0];id[0];i=0;j=0}$3!=$6{id[i]=$1;
  105. name[i]=$2;i++}END{while(i>0){print id[j]" "name[j];i--;j++}}' empprjdetail
  106. | sed '/^$/d'
  107. 103 Sneha
  108. 104 Ramesh
Add Comment
Please, Sign In to add comment