How to count a string occurrence,search in between a pattern for specific string

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP












1















I am working on a ksh script whose scenario is as follows:



I have a text file for students' reports which contains details of students
like:



Student_1
Name: ABC
Class:X
Head Teacher:SITA
Status: Pass

Student_1
Name: ABCE
Class:X
Head Teacher:SITA
Status: Pass

Student_2
Name:ABCD
Class:XI
Head Teacher:RYAN
Status: Fail

Student_50:
Name:MIKE
Class:X
Head Teacher:RYAN
Status:Fail


What I need to do is to



  1. Find the number of students by counting the lines that start with Student_N;


  2. Count how many students passed and how many students failed using the Status: line.


  3. 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.txt
sed '/^Student_i<< Status/,/)/Status/$/!d/Status/s/^Student_i<< Status (///Status/s/);$//' students_details.txt >> report_card.txt
sed '/^Student_i<< Status/,/)Status$/!d;s/^Student_i<< Status (//;s/);$//' students_details.txt >> report_card.txt
sed '/^pass/,/);$/!d;s/^pass (//;s/);$//' students_details.txt>> report_card.txt


My desired output files are:




  • For point 1:



    Student_1 : 2
    Student_2 : 1
    Student_50: 1



  • For Point 2:



    Pass: 2
    Fail: 2



  • For Point 3:



    Count of Students whose name starts with "A" : 3










share|improve this question
























  • What is it you are looking for? You mention you want to learn. However on this site you are likely to receive pre-chewed answers that may not advance your learning that much. One tip; you seem to believe that "i" has some special meaning. In the samples posted "i" is nothing more than the letter "i".

    – Bram
    Oct 22 '15 at 7:43











  • Edited the question. Hope u will understand. At least give some pointers to resolve the issue, instead of ignoring it.

    – A.K. Singh
    Oct 22 '15 at 8:46











  • Since your data appears to consist of structured data records, I'd look at using awk or perl in 'paragraph mode' rather than sed. FWIW I don't see the relevance of the ksh tag here since you seem to be looking for a solution using external text-processing tools rather than the shell itself.

    – steeldriver
    Oct 22 '15 at 10:17











  • @steeldriver yes i am also working on different options like i had used grep -n -w filename.txt but still i am not fully satisfied.

    – A.K. Singh
    Oct 22 '15 at 10:21











  • OK so here's a freebie to get you started: awk 'BEGINRS="" ENDprint NR'

    – steeldriver
    Oct 22 '15 at 10:30















1















I am working on a ksh script whose scenario is as follows:



I have a text file for students' reports which contains details of students
like:



Student_1
Name: ABC
Class:X
Head Teacher:SITA
Status: Pass

Student_1
Name: ABCE
Class:X
Head Teacher:SITA
Status: Pass

Student_2
Name:ABCD
Class:XI
Head Teacher:RYAN
Status: Fail

Student_50:
Name:MIKE
Class:X
Head Teacher:RYAN
Status:Fail


What I need to do is to



  1. Find the number of students by counting the lines that start with Student_N;


  2. Count how many students passed and how many students failed using the Status: line.


  3. 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.txt
sed '/^Student_i<< Status/,/)/Status/$/!d/Status/s/^Student_i<< Status (///Status/s/);$//' students_details.txt >> report_card.txt
sed '/^Student_i<< Status/,/)Status$/!d;s/^Student_i<< Status (//;s/);$//' students_details.txt >> report_card.txt
sed '/^pass/,/);$/!d;s/^pass (//;s/);$//' students_details.txt>> report_card.txt


My desired output files are:




  • For point 1:



    Student_1 : 2
    Student_2 : 1
    Student_50: 1



  • For Point 2:



    Pass: 2
    Fail: 2



  • For Point 3:



    Count of Students whose name starts with "A" : 3










share|improve this question
























  • What is it you are looking for? You mention you want to learn. However on this site you are likely to receive pre-chewed answers that may not advance your learning that much. One tip; you seem to believe that "i" has some special meaning. In the samples posted "i" is nothing more than the letter "i".

    – Bram
    Oct 22 '15 at 7:43











  • Edited the question. Hope u will understand. At least give some pointers to resolve the issue, instead of ignoring it.

    – A.K. Singh
    Oct 22 '15 at 8:46











  • Since your data appears to consist of structured data records, I'd look at using awk or perl in 'paragraph mode' rather than sed. FWIW I don't see the relevance of the ksh tag here since you seem to be looking for a solution using external text-processing tools rather than the shell itself.

    – steeldriver
    Oct 22 '15 at 10:17











  • @steeldriver yes i am also working on different options like i had used grep -n -w filename.txt but still i am not fully satisfied.

    – A.K. Singh
    Oct 22 '15 at 10:21











  • OK so here's a freebie to get you started: awk 'BEGINRS="" ENDprint NR'

    – steeldriver
    Oct 22 '15 at 10:30













1












1








1








I am working on a ksh script whose scenario is as follows:



I have a text file for students' reports which contains details of students
like:



Student_1
Name: ABC
Class:X
Head Teacher:SITA
Status: Pass

Student_1
Name: ABCE
Class:X
Head Teacher:SITA
Status: Pass

Student_2
Name:ABCD
Class:XI
Head Teacher:RYAN
Status: Fail

Student_50:
Name:MIKE
Class:X
Head Teacher:RYAN
Status:Fail


What I need to do is to



  1. Find the number of students by counting the lines that start with Student_N;


  2. Count how many students passed and how many students failed using the Status: line.


  3. 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.txt
sed '/^Student_i<< Status/,/)/Status/$/!d/Status/s/^Student_i<< Status (///Status/s/);$//' students_details.txt >> report_card.txt
sed '/^Student_i<< Status/,/)Status$/!d;s/^Student_i<< Status (//;s/);$//' students_details.txt >> report_card.txt
sed '/^pass/,/);$/!d;s/^pass (//;s/);$//' students_details.txt>> report_card.txt


My desired output files are:




  • For point 1:



    Student_1 : 2
    Student_2 : 1
    Student_50: 1



  • For Point 2:



    Pass: 2
    Fail: 2



  • For Point 3:



    Count of Students whose name starts with "A" : 3










share|improve this question
















I am working on a ksh script whose scenario is as follows:



I have a text file for students' reports which contains details of students
like:



Student_1
Name: ABC
Class:X
Head Teacher:SITA
Status: Pass

Student_1
Name: ABCE
Class:X
Head Teacher:SITA
Status: Pass

Student_2
Name:ABCD
Class:XI
Head Teacher:RYAN
Status: Fail

Student_50:
Name:MIKE
Class:X
Head Teacher:RYAN
Status:Fail


What I need to do is to



  1. Find the number of students by counting the lines that start with Student_N;


  2. Count how many students passed and how many students failed using the Status: line.


  3. 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.txt
sed '/^Student_i<< Status/,/)/Status/$/!d/Status/s/^Student_i<< Status (///Status/s/);$//' students_details.txt >> report_card.txt
sed '/^Student_i<< Status/,/)Status$/!d;s/^Student_i<< Status (//;s/);$//' students_details.txt >> report_card.txt
sed '/^pass/,/);$/!d;s/^pass (//;s/);$//' students_details.txt>> report_card.txt


My desired output files are:




  • For point 1:



    Student_1 : 2
    Student_2 : 1
    Student_50: 1



  • For Point 2:



    Pass: 2
    Fail: 2



  • For Point 3:



    Count of Students whose name starts with "A" : 3







shell-script ksh






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 4 at 23:53









terdon

133k33267446




133k33267446










asked Oct 22 '15 at 7:08









A.K. SinghA.K. Singh

115




115












  • What is it you are looking for? You mention you want to learn. However on this site you are likely to receive pre-chewed answers that may not advance your learning that much. One tip; you seem to believe that "i" has some special meaning. In the samples posted "i" is nothing more than the letter "i".

    – Bram
    Oct 22 '15 at 7:43











  • Edited the question. Hope u will understand. At least give some pointers to resolve the issue, instead of ignoring it.

    – A.K. Singh
    Oct 22 '15 at 8:46











  • Since your data appears to consist of structured data records, I'd look at using awk or perl in 'paragraph mode' rather than sed. FWIW I don't see the relevance of the ksh tag here since you seem to be looking for a solution using external text-processing tools rather than the shell itself.

    – steeldriver
    Oct 22 '15 at 10:17











  • @steeldriver yes i am also working on different options like i had used grep -n -w filename.txt but still i am not fully satisfied.

    – A.K. Singh
    Oct 22 '15 at 10:21











  • OK so here's a freebie to get you started: awk 'BEGINRS="" ENDprint NR'

    – steeldriver
    Oct 22 '15 at 10:30

















  • What is it you are looking for? You mention you want to learn. However on this site you are likely to receive pre-chewed answers that may not advance your learning that much. One tip; you seem to believe that "i" has some special meaning. In the samples posted "i" is nothing more than the letter "i".

    – Bram
    Oct 22 '15 at 7:43











  • Edited the question. Hope u will understand. At least give some pointers to resolve the issue, instead of ignoring it.

    – A.K. Singh
    Oct 22 '15 at 8:46











  • Since your data appears to consist of structured data records, I'd look at using awk or perl in 'paragraph mode' rather than sed. FWIW I don't see the relevance of the ksh tag here since you seem to be looking for a solution using external text-processing tools rather than the shell itself.

    – steeldriver
    Oct 22 '15 at 10:17











  • @steeldriver yes i am also working on different options like i had used grep -n -w filename.txt but still i am not fully satisfied.

    – A.K. Singh
    Oct 22 '15 at 10:21











  • OK so here's a freebie to get you started: awk 'BEGINRS="" ENDprint NR'

    – steeldriver
    Oct 22 '15 at 10:30
















What is it you are looking for? You mention you want to learn. However on this site you are likely to receive pre-chewed answers that may not advance your learning that much. One tip; you seem to believe that "i" has some special meaning. In the samples posted "i" is nothing more than the letter "i".

– Bram
Oct 22 '15 at 7:43





What is it you are looking for? You mention you want to learn. However on this site you are likely to receive pre-chewed answers that may not advance your learning that much. One tip; you seem to believe that "i" has some special meaning. In the samples posted "i" is nothing more than the letter "i".

– Bram
Oct 22 '15 at 7:43













Edited the question. Hope u will understand. At least give some pointers to resolve the issue, instead of ignoring it.

– A.K. Singh
Oct 22 '15 at 8:46





Edited the question. Hope u will understand. At least give some pointers to resolve the issue, instead of ignoring it.

– A.K. Singh
Oct 22 '15 at 8:46













Since your data appears to consist of structured data records, I'd look at using awk or perl in 'paragraph mode' rather than sed. FWIW I don't see the relevance of the ksh tag here since you seem to be looking for a solution using external text-processing tools rather than the shell itself.

– steeldriver
Oct 22 '15 at 10:17





Since your data appears to consist of structured data records, I'd look at using awk or perl in 'paragraph mode' rather than sed. FWIW I don't see the relevance of the ksh tag here since you seem to be looking for a solution using external text-processing tools rather than the shell itself.

– steeldriver
Oct 22 '15 at 10:17













@steeldriver yes i am also working on different options like i had used grep -n -w filename.txt but still i am not fully satisfied.

– A.K. Singh
Oct 22 '15 at 10:21





@steeldriver yes i am also working on different options like i had used grep -n -w filename.txt but still i am not fully satisfied.

– A.K. Singh
Oct 22 '15 at 10:21













OK so here's a freebie to get you started: awk 'BEGINRS="" ENDprint NR'

– steeldriver
Oct 22 '15 at 10:30





OK so here's a freebie to get you started: awk 'BEGINRS="" ENDprint NR'

– steeldriver
Oct 22 '15 at 10:30










1 Answer
1






active

oldest

votes


















2














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: $passnFail:$failn";
print "Student names starting with A: $Asn"
' file


Student_2 : 1
Student_1 : 1
Student_50 : 1
Pass: 2
Fail:2
Student names starting with A: 2


If you insist on separate commands per operation, you could use:



$ awk '/^Student_/a[$0]++ ENDfor(s in a)print s,a[s]' file 


Student_1 1
Student_2 1
Student_50: 1




$ perl -ne '$pass++ if /:s*Pass/; $fail++ if /:s*Fail/;
ENDprint "Pass: $passnFail: $failn"' file


Pass: 2
Fail: 2




$ echo "Student names starting with A: $(grep -c "^Name:s*A" file )"


Student names starting with A: 2





share|improve this answer

























  • Nice answer using Perl. Since the original file had white-spaces in the second block, I'd add a s* between /^ and (Student_d+).

    – Moreaki
    Mar 4 at 23:44






  • 1





    @Moreaki good point, thanks. I'm pretty sure that was just a copy/paste error though. Note how there are other places in the file that aren't uniform (some have Name: foo and others Name:foo). I am guessing the actual file is uniform and produced by some software tool.

    – terdon
    Mar 4 at 23:55











Your Answer








StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f237835%2fhow-to-count-a-string-occurrence-search-in-between-a-pattern-for-specific-string%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














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: $passnFail:$failn";
print "Student names starting with A: $Asn"
' file


Student_2 : 1
Student_1 : 1
Student_50 : 1
Pass: 2
Fail:2
Student names starting with A: 2


If you insist on separate commands per operation, you could use:



$ awk '/^Student_/a[$0]++ ENDfor(s in a)print s,a[s]' file 


Student_1 1
Student_2 1
Student_50: 1




$ perl -ne '$pass++ if /:s*Pass/; $fail++ if /:s*Fail/;
ENDprint "Pass: $passnFail: $failn"' file


Pass: 2
Fail: 2




$ echo "Student names starting with A: $(grep -c "^Name:s*A" file )"


Student names starting with A: 2





share|improve this answer

























  • Nice answer using Perl. Since the original file had white-spaces in the second block, I'd add a s* between /^ and (Student_d+).

    – Moreaki
    Mar 4 at 23:44






  • 1





    @Moreaki good point, thanks. I'm pretty sure that was just a copy/paste error though. Note how there are other places in the file that aren't uniform (some have Name: foo and others Name:foo). I am guessing the actual file is uniform and produced by some software tool.

    – terdon
    Mar 4 at 23:55















2














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: $passnFail:$failn";
print "Student names starting with A: $Asn"
' file


Student_2 : 1
Student_1 : 1
Student_50 : 1
Pass: 2
Fail:2
Student names starting with A: 2


If you insist on separate commands per operation, you could use:



$ awk '/^Student_/a[$0]++ ENDfor(s in a)print s,a[s]' file 


Student_1 1
Student_2 1
Student_50: 1




$ perl -ne '$pass++ if /:s*Pass/; $fail++ if /:s*Fail/;
ENDprint "Pass: $passnFail: $failn"' file


Pass: 2
Fail: 2




$ echo "Student names starting with A: $(grep -c "^Name:s*A" file )"


Student names starting with A: 2





share|improve this answer

























  • Nice answer using Perl. Since the original file had white-spaces in the second block, I'd add a s* between /^ and (Student_d+).

    – Moreaki
    Mar 4 at 23:44






  • 1





    @Moreaki good point, thanks. I'm pretty sure that was just a copy/paste error though. Note how there are other places in the file that aren't uniform (some have Name: foo and others Name:foo). I am guessing the actual file is uniform and produced by some software tool.

    – terdon
    Mar 4 at 23:55













2












2








2







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: $passnFail:$failn";
print "Student names starting with A: $Asn"
' file


Student_2 : 1
Student_1 : 1
Student_50 : 1
Pass: 2
Fail:2
Student names starting with A: 2


If you insist on separate commands per operation, you could use:



$ awk '/^Student_/a[$0]++ ENDfor(s in a)print s,a[s]' file 


Student_1 1
Student_2 1
Student_50: 1




$ perl -ne '$pass++ if /:s*Pass/; $fail++ if /:s*Fail/;
ENDprint "Pass: $passnFail: $failn"' file


Pass: 2
Fail: 2




$ echo "Student names starting with A: $(grep -c "^Name:s*A" file )"


Student names starting with A: 2





share|improve this answer















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: $passnFail:$failn";
print "Student names starting with A: $Asn"
' file


Student_2 : 1
Student_1 : 1
Student_50 : 1
Pass: 2
Fail:2
Student names starting with A: 2


If you insist on separate commands per operation, you could use:



$ awk '/^Student_/a[$0]++ ENDfor(s in a)print s,a[s]' file 


Student_1 1
Student_2 1
Student_50: 1




$ perl -ne '$pass++ if /:s*Pass/; $fail++ if /:s*Fail/;
ENDprint "Pass: $passnFail: $failn"' file


Pass: 2
Fail: 2




$ echo "Student names starting with A: $(grep -c "^Name:s*A" file )"


Student names starting with A: 2






share|improve this answer














share|improve this answer



share|improve this answer








edited Oct 23 '15 at 6:03









Henk Langeveld

587414




587414










answered Oct 22 '15 at 13:23









terdonterdon

133k33267446




133k33267446












  • Nice answer using Perl. Since the original file had white-spaces in the second block, I'd add a s* between /^ and (Student_d+).

    – Moreaki
    Mar 4 at 23:44






  • 1





    @Moreaki good point, thanks. I'm pretty sure that was just a copy/paste error though. Note how there are other places in the file that aren't uniform (some have Name: foo and others Name:foo). I am guessing the actual file is uniform and produced by some software tool.

    – terdon
    Mar 4 at 23:55

















  • Nice answer using Perl. Since the original file had white-spaces in the second block, I'd add a s* between /^ and (Student_d+).

    – Moreaki
    Mar 4 at 23:44






  • 1





    @Moreaki good point, thanks. I'm pretty sure that was just a copy/paste error though. Note how there are other places in the file that aren't uniform (some have Name: foo and others Name:foo). I am guessing the actual file is uniform and produced by some software tool.

    – terdon
    Mar 4 at 23:55
















Nice answer using Perl. Since the original file had white-spaces in the second block, I'd add a s* between /^ and (Student_d+).

– Moreaki
Mar 4 at 23:44





Nice answer using Perl. Since the original file had white-spaces in the second block, I'd add a s* between /^ and (Student_d+).

– Moreaki
Mar 4 at 23:44




1




1





@Moreaki good point, thanks. I'm pretty sure that was just a copy/paste error though. Note how there are other places in the file that aren't uniform (some have Name: foo and others Name:foo). I am guessing the actual file is uniform and produced by some software tool.

– terdon
Mar 4 at 23:55





@Moreaki good point, thanks. I'm pretty sure that was just a copy/paste error though. Note how there are other places in the file that aren't uniform (some have Name: foo and others Name:foo). I am guessing the actual file is uniform and produced by some software tool.

– terdon
Mar 4 at 23:55

















draft saved

draft discarded
















































Thanks for contributing an answer to Unix & Linux Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f237835%2fhow-to-count-a-string-occurrence-search-in-between-a-pattern-for-specific-string%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown






Popular posts from this blog

How to check contact read email or not when send email to Individual?

Displaying single band from multi-band raster using QGIS

How many registers does an x86_64 CPU actually have?