Couple of questions wth grep/sort


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Couple of questions wth grep/sort
# 1  
Old 11-10-2012
Couple of questions wth grep/sort

I have different things that I was trying to do but am kind of struggling with this since I'm a Linux noob. The backround is that I have two files with student names in the same directory, and each file lists the student name, their major and their grade level. What is the most efficient way to get a total count of students that are only enrolled in one class (listed only once) from these two files?

Initially I tried to get a count and pipe it out, but was at a loss as to how to get a count of the students only enrolled in one class, but this is where I am at now.

Code:
grep -c "Hist" HIS200 ART250 | sort

The second problem is I need to print the names of the senior history majors that are enrolled in both of these classes only printing their name once. I'm stuck on this one too, so I'm not sure if there is a better command I should be using? Here's where I'm at on that one:
Code:
egrep -i "HIST|SR" HIS200 ART200 | sort

. When I do this is just seems to return a list of all students in both classes so is there a way to egrep two different strings? Thanks in advance for any help or guidance you can provide!

JC

Last edited by Scrutinizer; 11-10-2012 at 05:40 PM.. Reason: code tags
# 2  
Old 11-10-2012
You could try something like:
Code:
grep "val1" file1 file2 | grep -c "val2" | ...

It would be good if you could post sample data...
# 3  
Old 11-11-2012
Hey, good point....forgot to add that info. Here's the contents of the two files I'm working with. One is named HIS200 and one is named ART250.

Contents of HIS200:
Code:
Jackson, Joe SR HIST
Sorenson, Kara JR ART
Clopes, Suzie SR HIST
Brown, Steve JR HIST
Driver, Don SR HIST
Mantle, Mick SR HIST
Hill, Mary JR ART
Greene, Seth JR HIST
Wells, Wesley JR SOC

Contents of ART250
Code:
Jones, Susan JR ART
Jackson, Joe SR HIST
Sorenson, Kara JR ART
Clopes, Suzie SR HIST
Brown, Steve JR HIST
Driver, Don SR HIST
Johnson, Bob SR SOC
Hill, Mary JR ART
Greene, Seth JR HIST


Last edited by Scott; 11-11-2012 at 05:23 AM.. Reason: Code tags
# 4  
Old 11-11-2012
Try these commands:

List of students enrolled in one class:-
Code:
sort HIS200 ART250 | uniq -u

Senior History majors enrolled in both classes:-
Code:
sort HIS200 ART250 | grep "SR HIST" | uniq -d


Last edited by Yoda; 11-11-2012 at 01:38 AM..
# 5  
Old 11-11-2012
Hi

Students enrolled in one class:

Code:
$ awk '{a[$0]++;}END{for (i in a)if(a[i]==1)x++;print x}' HIS200 ART250
4

Senior mgr list:
Code:
$ awk '/SR HIST/{a[$0]++;}END{for (i in a)if(a[i]==2)print i;}' HIS200 ART250
Jackson, Joe SR HIST
Driver, Don SR HIST
Clopes, Suzie SR HIST

Guru.
# 6  
Old 11-11-2012
Thanks for the help everyone, I appreciate it. bipinajith, on yours the second one is really close, I get a list of names if I don't use the -d flag for uniq, but when I use the -d flag for uniq I don't get any output.

@ Guru on the first bit of code I get an error of bash: syntax error near unexpected token 'i' and on the second code I don't get any output. Is it possible that there are other plugins I need to install? I'm running Ubuntu 12.10 on Cisco Virtualbox since I didn't have a spare system to install this on. Also, does anyone have the man pages in pdf format...was just thinking it would be nice to have a local copy to search when looking up commands. Thanks again all!
# 7  
Old 11-18-2012
diff

You could also use diff in this case:

Code:
$ diff ~/Desktop/HIS200.txt ~/Desktop/ART250.txt 
0a1
> Jones, Susan JR ART
6c7
< Mantle, Mick SR HIST
---
> Johnson, Bob SR SOC
9d9
< Wells, Wesley JR SOC

This will return the names that only appear in one file. The "greater than" and "lesser than" symbols indicate which file the names appear in, with '<' indicating the first file, (HIS200.txt in this case).

Last edited by sudon't; 11-18-2012 at 05:39 PM.. Reason: a little more info
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Homework & Coursework Questions

awk questions using sort and grep

1. The problem statement, all variables and given/known data: So i'll probably get told off for this but I have a few problems and rather than clog up the whole forum I'll post them here. Please bare in mind I am a complete novice when it comes to all this and so if you help please treat me like a... (4 Replies)
Discussion started by: jamesb18
4 Replies

2. Shell Programming and Scripting

Couple of easy questions for experts on awk/sed

Hello Experts.. I have 3-4 C codes with Oracle SQL statements embedded. All the SQL statements starts with EXEC SQL keyword and ends with ;. I want to extract all the SQL statements out of these codes. I did awk '/^EXEC SQL/,/\;/' inputFile (I use this on all of the codes individually). That... (2 Replies)
Discussion started by: juzz4fun
2 Replies

3. Shell Programming and Scripting

Grep couple of consecutive lines if each lines contains certain string

Hello, I want to extract from a file like : 20120530025502914 | REQUEST | whatever 20120530025502968 | RESPONSE | whatever 20120530025502985 | RESPONSE | whatever 20120530025502996 | REQUEST | whatever 20120530025503013 | REQUEST | whatever 20120530025503045 | RESPONSE | whatever I want... (14 Replies)
Discussion started by: black_fender
14 Replies

4. UNIX for Dummies Questions & Answers

Couple of newbie Unix questions

I entered a command at the prompt and it's interactive (not background). It gathers some stats and writes them to a file. I want to see this job running and what it's doing - I/O especially and maybe CPU and stuff. What can do to see this (say using a second session?) I know it's a long job but... (3 Replies)
Discussion started by: ido1957
3 Replies

5. Solaris

Couple logadm questions

My logadm.conf is below. Is there a way to match a log file that appends the time/date stamp after the log file? Also, a 0 is being appended onto the files I'm compressing and having rotated. Is there a way to fix that? /var/apache/tomcat55/logs/catalina.out -C 30 -P 'Fri Jun 18 16:48:55... (5 Replies)
Discussion started by: LittleLebowski
5 Replies

6. IP Networking

Couple of quick IP Routing questions

Hi 1) Is the MIB II table that can be accessed through the function m2IpRouteTblEntryGet() the same as the IP Routing table that the IP layer uses to do its routing work? 2) Is there a set of API functions, other than m2IpRouteTblEntryGet(), that are available to access entries in the IP Routing... (2 Replies)
Discussion started by: nzeidat
2 Replies

7. HP-UX

Couple of questions.

Right now I am a Microfocus COBOL programmer, working on a HP-UX system. I want to now get Certified as a HP Certified Systems Administrator. I ordered a book from amazon and will start with it. I also will be working at my job with someone who applies the patches and things as we do NOT have a... (3 Replies)
Discussion started by: nixie21
3 Replies

8. Debian

A couple of grub questions.

1) After I install a new kernel in Debian Sarge, it updates my menu.lst file for grub, but incorrectly. It assumes it should boot from partition hd0,0, but this is incorrect. How do I change this faulty assumption? 2) If grub fails to find a kernel, grub allows me to enter a path to... (1 Reply)
Discussion started by: akbar
1 Replies

9. Linux

Attn! A couple quick questions about Linux [Help]

Hi guys!! I was wondering if you can help me with a couple quick questions in order for me to understand it better... Any help would be appreciated and i would like to say thanks!! In advanced... Ok here goes... (I think these are pretty basic, but i just want to clarify) 1. What would be the... (2 Replies)
Discussion started by: kyoist
2 Replies
Login or Register to Ask a Question