Prev: Eight tasks, eight CPUs ... this shouldn't be hard
Next: konqueror: jumps to google, when trying to sign in to website
From: kwan on 24 Apr 2010 02:10 Hello Guru, I wrote Perl to scan through a file that contain information about a particular student. The file contains the following infor: [1] john2001 = student ID [2] CS201 = Course ID [3] DEPT200 = Department ID [4] PROFESSOR2001 = Professor ID [5] 20101 = In 2010 and spring semester (1) is spring, (2) is summer, (3) is fall [6] STUDENT20 = Total Student is 20 [7] TExtBook2001322 = Textbook use john2001:CS201:DEPT200:PROFESSOR2001:20101:STUDENT20:TextBook20013222 bob2001:CS201:DEPT200:PROFESSOR2001:20101:STUDENT20:Textbook200122222 jonny2001:MATH201:DEPAT2001:PROFESSOR2002:20101:STUDENT21:Textbook2003232 Now, how can I find out that the how many student who take computer science 201 in spring of 2010 semester. I wrote the following in Perl script, but I don't know how can I used it in grep or egrep.
From: Chris F.A. Johnson on 24 Apr 2010 03:23 On 2010-04-24, kwan wrote: > Hello Guru, > > I wrote Perl to scan through a file that contain information about a > particular student. > The file contains the following infor: > > [1] john2001 = student ID > [2] CS201 = Course ID > [3] DEPT200 = Department ID > [4] PROFESSOR2001 = Professor ID > [5] 20101 = In 2010 and spring semester > (1) is spring, (2) is summer, (3) is fall > [6] STUDENT20 = Total Student is 20 > [7] TExtBook2001322 = Textbook use > > john2001:CS201:DEPT200:PROFESSOR2001:20101:STUDENT20:TextBook20013222 > bob2001:CS201:DEPT200:PROFESSOR2001:20101:STUDENT20:Textbook200122222 > jonny2001:MATH201:DEPAT2001:PROFESSOR2002:20101:STUDENT21:Textbook2003232 > > Now, how can I find out that the how many student who take computer > science 201 in spring of 2010 semester. > > I wrote the following in Perl script, but I don't know how can I used > it in grep or egrep. Use awk: awk -F: '$2 == "CS201" && $5 == "20101" { ++x } END { print x }' "$file" -- Chris F.A. Johnson, <http://cfajohnson.com> Author: Pro Bash Programming: Scripting the GNU/Linux Shell (2009, Apress) Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
From: Rui Maciel on 24 Apr 2010 06:03 kwan wrote: > Hello Guru, > > I wrote Perl to scan through a file that contain information about a > particular student. > The file contains the following infor: > > [1] john2001 = student ID > [2] CS201 = Course ID > [3] DEPT200 = Department ID > [4] PROFESSOR2001 = Professor ID > [5] 20101 = In 2010 and spring semester > (1) is spring, (2) is summer, (3) is fall > [6] STUDENT20 = Total Student is 20 > [7] TExtBook2001322 = Textbook use > > john2001:CS201:DEPT200:PROFESSOR2001:20101:STUDENT20:TextBook20013222 > bob2001:CS201:DEPT200:PROFESSOR2001:20101:STUDENT20:Textbook200122222 > jonny2001:MATH201:DEPAT2001:PROFESSOR2002:20101:STUDENT21:Textbook2003232 > > Now, how can I find out that the how many student who take computer > science 201 in spring of 2010 semester. > > I wrote the following in Perl script, but I don't know how can I used > it in grep or egrep. You can always parse that document and build a data structure from the information contained in it. By the looks of it, it would amount to nothing more than a simple list of instances of a class which stores the information on each student. As you said that you already wrote a scanner for that document format then it would be trivial to use it to build that list and then iterate through it to extract whatever info you wish to extract, including how many students are taking science 201 in a particular semester. Rui Maciel
From: Bit Twister on 24 Apr 2010 08:08 On Fri, 23 Apr 2010 23:10:35 -0700 (PDT), kwan wrote: > > john2001:CS201:DEPT200:PROFESSOR2001:20101:STUDENT20:TextBook20013222 > bob2001:CS201:DEPT200:PROFESSOR2001:20101:STUDENT20:Textbook200122222 > jonny2001:MATH201:DEPAT2001:PROFESSOR2002:20101:STUDENT21:Textbook2003232 > > Now, how can I find out that the how many student who take computer > science 201 in spring of 2010 semester. > > I wrote the following in Perl script, but I don't know how can I used > it in grep or egrep. Just using grep with above data in data_file :-D $ grep :CS201: data_file | grep -c :20101: 2
From: Frank Stutzman on 24 Apr 2010 08:59
Bit Twister <BitTwister(a)mouse-potato.com> wrote: > > Just using grep with above data in data_file :-D > > $ grep :CS201: data_file | grep -c :20101: > 2 Or maybe slightly better : egrep -c ":CS201:.*:20101:" data_file Might not be perfect, especially if you allow colons to be embeded within your fields. -- Frank Stutzman |