Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- employee :
- ====================================
- 101*Ajay*ajay@gmail.com*12/08/2017*Bilaspur$
- 102*John*john@gmail.com*21/01/2018*Raipur$
- 103*Sneha*sneha@gmail.com*08/12/2017*Raipur$
- 104*Ramesh*ramesh@gmail.com*12/01/2017*Korba$
- 105*mahesh*mahesh@gmail.com*12/01/2019*Delhi$
- ====================================
- project :
- ====================================
- 201*ProjA*Bilaspur$
- 202*ProjB*Raipur$
- 203*ProjC*Delhi$
- ====================================
- empproject :
- ========================================
- 101*201*01/05/2017$
- 102*202*01/10/2018$
- 103*201*01/05/2017$
- 104*203*01/08/2017$
- 105*203*01/08/2018$
- ========================================
- 1. List of Employees and project details.
- $ awk 'BEGIN{RS="$";FS="*";i=0;j=0;k=0}
- FNR==NR{eid[i]=$1;name[i]=$2;city[i]=$5;i++}
- FNR!=NR{eidp[k]=$1;pid[k]=$2;k++}
- END{ while (i > 0) {if(eid[j]==eidp[j]) {if(name[j] && pid[j]) { print
- eid[j]"*"name[j]"*"city[j]"*"pid[j]"$"}};j++;i--}}
- Linux file:///home/ajay/Desktop/test.html
- 1 of 4 03/04/19, 11:46 pm
- ' employee empproject > empprjdet
- The new file "empprjdet" Contains following data :
- ==================
- 101*Ajay*Bilaspur*201$
- 102*John*Raipur*202$
- 103*Sneha*Raipur*201$
- 104*Ramesh*Korba*203$
- 105*mahesh*Delhi*203$
- ==================
- The below awk command compare employee's project details with project
- details and generate a new file "empprjdetail"
- $ awk 'BEGIN{RS="$";FS="*";i=0;j=0;k=0}
- FNR==NR{eid[i]=$1;name[i]=$2;city[i]=$3;pid[i]=$4;i++}
- FNR!=NR{ppid[k]=$1;prj[k]=$2;pcity[k]=$3;k++}
- END{ while (i> 0){ l=k;m=0;while (l > 0){if(pid[j]==ppid[m]){print
- eid[j]"*"name[j]"*"city[j]"*"pid[j]"*"prj[m]"*"pcity[m]"$"};m++;l--};j++;i--}
- }
- ' empprjdet project | sed '/^$/d' > empprjdetail
- At the end sed command is used to remove blank spaces
- empprjdetail
- ===================
- 101*Ajay*Bilaspur*201*ProjA*Bilaspur$
- 102*John*Raipur*202*ProjB*Raipur$
- 103*Sneha*Raipur*201*ProjA*Bilaspur$
- 104*Ramesh*Korba*203*ProjC*Delhi$
- 105*mahesh*Delhi*203*ProjC*Delhi$
- ===================
- $ awk 'BEGIN{FS="*";RS="$"}{print $1,$2,$3,$4,$5}' empprjdetail | sed
- '/^$/d'
- =========================
- 101 Ajay Bilaspur 201 ProjA
- 102 John Raipur 202 ProjB
- 103 Sneha Raipur 201 ProjA
- 104 Ramesh Korba 203 ProjC
- 105 mahesh Delhi 203 ProjC
- =========================
- Linux file:///home/ajay/Desktop/test.html
- 2 of 4 03/04/19, 11:46 pm
- 2. Find which projects engaged maximum number of employees.
- $ awk 'BEGIN{RS="$";FS="*";i=0;j=0;k=0;m=0;prj[0];empid[0];empp[0];
- ct[0]}
- FNR==NR{if(NF==3){prj[i]=$2;i++}}
- FNR!=NR{if(NF==6){empid[j]=$1;empp[j]=$5;j++}}END{while(j>0)
- {m=i;n=0;while(m >= 0) { if(empp[k]==prj[n]){ct[n]+=1};n++;m--};
- k++;j--}}
- END{ctr=0;print "Project Name\tNumber of Employees";while(ctr <
- 3){print prj[ctr]"\t\t\t"ct[ctr];ctr++}}
- ' project empprjdetail
- Output :
- ================================
- Project Name Number of Employees
- ProjA 2
- ProjB 1
- ProjC 2
- ================================
- Notes : It first take the data from previously created file empprjdetail.
- 4. Find the employee who works in their own city.
- awk 'BEGIN{RS="$";FS="*";name[0];id[0];i=0;j=0}$3==$6{id[i]=$1;
- name[i]=$2;i++}END{while(i>0){print id[j]" "name[j];i--;j++}}' empprjdetail
- | sed '/^$/d'
- 101 Ajay
- 102 John
- 105 mahesh
- 5. Find the employee who does not works in their own city.
- awk 'BEGIN{RS="$";FS="*";name[0];id[0];i=0;j=0}$3!=$6{id[i]=$1;
- name[i]=$2;i++}END{while(i>0){print id[j]" "name[j];i--;j++}}' empprjdetail
- | sed '/^$/d'
- 103 Sneha
- 104 Ramesh
Add Comment
Please, Sign In to add comment