I am working on a ksh script whose scenario is as follows:I have a text file for students' reports which contains details of studentslike:Student_1Name: ABCClass:XHead Teacher:SITAStatus: Pass  Student_1 Name: ABCE Class:X Head Teacher:SITA Status: Pass Student_2Name:ABCDClass:XIHead Teacher:RYANStatus: FailStudent_50:Name:MIKEClass:XHead Teacher:RYANStatus:FailWhat I need to do is to Find the number of students by counting the lines that start with Student_N;Count how many students passed and how many students failed using the Status: line.Find the name of students whose name starts with A.I tried numerous things including:sed -n '/Student_i<< Status/,/Status/p' students_details.txt >> report_card.txtsed '/^Student_i<< Status/,/)/Status/$/!d/Status/s/^Student_i<< Status (///Status/s/);$//' students_details.txt >> report_card.txtsed '/^Student_i<< Status/,/)Status$/!d;s/^Student_i<< Status (//;s/);$//' students_details.txt >> report_card.txtsed '/^pass/,/);$/!d;s/^pass (//;s/);$//' students_details.txt>> report_card.txtMy desired output files are:For point 1:Student_1 : 2Student_2 : 1Student_50: 1For Point 2:Pass: 2Fail: 2For Point 3:Count of Students whose name starts with A : 3
How to count a string occurrence,search in between a pattern for specific string
shell script;ksh
Personally, I would do the whole thing in Perl:$ perl -00ne '/^(Student_\d+)/ && $count{$1}++;               /Name:\sA/ && $As++;               /Status:\s*Pass/ ? $pass++ : $fail++;              END{                print $_ : $count{$_}\n for keys(%count);                 print Pass: $pass\nFail:$fail\n;                 print Student names starting with A: $As\n            }' file Student_2 : 1Student_1 : 1Student_50 : 1Pass: 2Fail:2Student names starting with A: 2If you insist on separate commands per operation, you could use:$ awk '/^Student_/{a[$0]++} END{for(s in a){print s,a[s]}}' file Student_1 1Student_2 1Student_50: 1$ perl -ne '$pass++ if /:\s*Pass/; $fail++ if /:\s*Fail/;     END{print Pass: $pass\nFail: $fail\n}' file Pass: 2Fail: 2$ echo Student names starting with A: $(grep -c ^Name:\s*A file )Student names starting with A: 2