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 Code:
cat ClassB 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. |
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.
|
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. |
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. |
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 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". |
Do you want something like this?
Code:
~$ grep 'CIS' fileA | grep 'JR' | cut -d" " -f1,2 |
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 }' |
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.
|
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/ |
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 |
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 Code:
Smith, Joe SR CIS |
For searching unique JR CIS, try this:
Code:
~$ grep 'JR' classA classB | grep 'CIS' | cut -d':' -f2 | sort -u |
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 |
This awk ...
Code:
awk 'NR==FNR{if ($0~"JR CIS") a[$1$2];next} Code:
Brown, Steve Code:
awk 'NR==FNR{if ($0~"JR CIS") a[$1$2];next} Code:
Brown, Steve JR CIS |
Quote:
|
All times are GMT -5. The time now is 01:07 PM. |