LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-18-2013, 02:46 AM   #1
ProAm500
LQ Newbie
 
Registered: Apr 2013
Posts: 11

Rep: Reputation: Disabled
Need help with the cut command..or should I be using the grep command?


Complete newb to linux and I'm working on some simple tasks using commands and so forth. I've been banging my head against the wall for a day a half with this last problem and hoping I someone can provide some help.I'm working with 2 files each containing some lines of text.
File A contains:
Code:
cat ClassA
Smith, Joe     SR CIS
Smith, Karen   JR CS
Clark, Sue     SR CIS
Brown, Steve   JR CIS
Duck, Donald   SR CIS
Mouse, Mickey  SR CIS
Hall, Maureen  JR CS
Simon, Sam     JR CIS
Wells, John    JR CS
File B contains:
Code:
cat ClassB
Jones, Susan   JR CS
Smith, Joe     SR CIS
Smith, Karen   JR CS
Clark, Sue     SR CIS
Brown, Steve   JR CIS
Duck, Donald   SR CIS
Johnson, Bob   SR CS
Hall, Maureen  JR CS
Simon, Sam     JR CIS
The task is to in one command line "Print just the names of the Junior CIS majors that are enrolled in both A and B. (They must be enrolled in both classes to be on the list. Only print their name once)"

I'm not exactly sure how to basically match the field that would just contain JR AND CIS but not display duplicate names. I think I'm supposed to use the cut command or grep then pipe into cut...I'm not exactly sure where to start. Any help that can at least get me going in the right direction would be great. Thanks.

Last edited by ProAm500; 04-18-2013 at 02:58 AM.
 
Old 04-18-2013, 03:06 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389
I would use grep (have a look at the -f option) to get all the similar lines and pipe it through awk to get the JR CIS parts and print their name.
 
Old 04-18-2013, 03:28 AM   #3
Zzipo
LQ Newbie
 
Registered: Mar 2013
Posts: 28

Rep: Reputation: Disabled
Another option, without using awk.

Is like in three steps.

For each file:

First: (grep) create a new file (file1new and file2new) based on the deletion of lines that don't contain "JR CIS" -> O(n)

Second: (sort) the lines based on the name (first column) -> O(nlog(n))

Third: (comm -1 -2 file1new file2new) print just the common lines. -> O(n)


So, O(nlog(n)) finally.
 
Old 04-18-2013, 05:40 AM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
How about grepping both files for "JR CS" and using uniq (with the magic --repeated option)?

In essence that works but needs a little refinement. The most elegant I got was three commands in a pipeline with no temporary files.
 
Old 04-18-2013, 06:10 AM   #5
Zzipo
LQ Newbie
 
Registered: Mar 2013
Posts: 28

Rep: Reputation: Disabled
I didn't know about uniq.

But, I don't know how can you do it without sorting before.

I reached this:
Code:
grep "JR CIS" -h test1file.txt test2file.txt | sort | uniq -d
But (uniq -d) without doing first (sort) doesn't work.

Ok, this sequence is also in 3 steps and also O(nlog(n)).

Is there any way to do it in just two steps?
Because you said "grep and uniq".

Ah, when you said "JR CS" I think it was a typo by "JR CIS".

Last edited by Zzipo; 04-18-2013 at 06:12 AM.
 
Old 04-18-2013, 06:15 AM   #6
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
Do you want something like this?
Code:
~$ grep 'CIS' fileA | grep 'JR' | cut -d" " -f1,2
 
Old 04-18-2013, 06:25 AM   #7
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389
Well, I thought this looked like homework and provided a hint in my previous post.

Now that I see multiple full examples, have a look at this:
Code:
grep -f fileA fileB | awk '/JR CIS/ { print $1, $2 }'
 
Old 04-18-2013, 12:52 PM   #8
ProAm500
LQ Newbie
 
Registered: Apr 2013
Posts: 11

Original Poster
Rep: Reputation: Disabled
thanks guys I'll give them all a shot. And yes it is a question as past of an assignment, but like i said this is the last problem on it, and i've been agonizing over it for almost 2 days lol..I'm still a complete beginner to linux shell commands, but I'm getting it, and have some fun with it too.
 
Old 04-18-2013, 07:37 PM   #9
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,362

Rep: Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377
Here are some good links to bookmark & read
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/
 
Old 04-19-2013, 05:23 AM   #10
eklavya
Member
 
Registered: Mar 2013
Posts: 619

Rep: Reputation: 136Reputation: 136
I know you got your solution but it seems an interesting problem, that's why I tried it.
You said you are trying with grep & cut so I tried using that.
Code:
grep -f ClassA ClassB | grep "JR.* CIS" | cut -d' ' -f1,2
 
Old 04-20-2013, 04:48 PM   #11
ProAm500
LQ Newbie
 
Registered: Apr 2013
Posts: 11

Original Poster
Rep: Reputation: Disabled
Thanks guys. I tried pretty much all your guys solutions and different variations of them and for most them I kept getting the same answer in some form
Code:
Brown, Steve
Simon, Sam
I believe the answer should be something like or at least in some form......
Code:
Smith, Joe     SR CIS
Clark, Sue     SR CIS
Brown, Steve   JR CIS
Duck, Donald   SR CIS
Simon, Sam     JR CIS
My instructor handed it back and told me to give it another shot. Her hint was "so start with sorting the files, you need two grep commands to pull in only CIS and JR, you should only count the records once (the unique command) and the use the cut command to list only the names". I'm going to keep playing around with it and see what I get.

Last edited by ProAm500; 04-20-2013 at 04:52 PM.
 
Old 04-20-2013, 11:17 PM   #12
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
For searching unique JR CIS, try this:
Code:
~$ grep 'JR' classA classB | grep 'CIS' | cut -d':' -f2 | sort -u
OR
~$ grep 'JR CIS' classA classB | cut -d':' -f2 | sort -u
Although same could be done easily using other commands like awk, but as per your instructor, you should try this.

Last edited by shivaa; 04-20-2013 at 11:19 PM.
 
Old 04-30-2013, 12:54 PM   #13
ProAm500
LQ Newbie
 
Registered: Apr 2013
Posts: 11

Original Poster
Rep: Reputation: Disabled
Just to wrap this up and put a bow on this topic. I used the following command and am still not 100% sure its right, but I got full credit so it works for me for now, lol. I'm working on the second part of the assignment and will have some questions so be prepared..thanks for the help guys!!

Code:
grep 'JR CIS' ClassA ClassB| cut -d':' -f2 | sort -u
 
Old 04-30-2013, 04:35 PM   #14
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,559

Rep: Reputation: 474Reputation: 474Reputation: 474Reputation: 474Reputation: 474
This awk ...
Code:
awk 'NR==FNR{if ($0~"JR CIS") a[$1$2];next}
   $1$2 in a{print $1,$2}' $InFile1 $InFile2 >$OutFile
... produces this result ...
Code:
Brown, Steve
Simon, Sam
... and this awk ...
Code:
awk 'NR==FNR{if ($0~"JR CIS") a[$1$2];next}
   $1$2 in a{print}' $InFile1 $InFile2 >$OutFile
... produces this result ...
Code:
Brown, Steve   JR CIS
Simon, Sam     JR CIS
Daniel B. Martin
 
Old 04-30-2013, 05:42 PM   #15
ProAm500
LQ Newbie
 
Registered: Apr 2013
Posts: 11

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by danielbmartin View Post
This awk ...
Code:
awk 'NR==FNR{if ($0~"JR CIS") a[$1$2];next}
   $1$2 in a{print $1,$2}' $InFile1 $InFile2 >$OutFile
... produces this result ...
Code:
Brown, Steve
Simon, Sam


... and this awk ...
Code:
awk 'NR==FNR{if ($0~"JR CIS") a[$1$2];next}
   $1$2 in a{print}' $InFile1 $InFile2 >$OutFile
... produces this result ...
Code:
Brown, Steve   JR CIS
Simon, Sam     JR CIS
Daniel B. Martin
thats what my command got

Last edited by ProAm500; 04-30-2013 at 05:43 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] How to grep, cut, cat in a single command? dwarf007 Programming 6 04-27-2010 02:36 AM
help with cut command using find. Cut last 8 characters leaving the rest ncsuapex Programming 4 09-16-2009 08:55 PM
How to use command grep,cut,awk to cut a data from a file? hocheetiong Linux - Newbie 7 09-11-2008 07:16 PM
grep and cut command tanveer Linux - General 3 09-05-2008 02:17 AM


All times are GMT -5. The time now is 06:16 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration