Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
03-08-2006, 06:25 AM
|
#1
|
Member
Registered: Oct 2003
Posts: 354
Rep:
|
Question About Cat And Grep
Hello everyone,
Could I use cat and grep commands to do the following task? If it is possible, could anyone help to show the sample please?
Task:
Given a file, calculate the number of lines (in this file) which is not started with '#'.
thanks in advance,
George
|
|
|
03-08-2006, 06:31 AM
|
#2
|
Senior Member
Registered: Jan 2005
Location: Manalapan, NJ
Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android
Posts: 4,593
|
grep -v "^#" somefile | wc -l
PS- Try to do your own homework - this isn;t going to help you pass any tests.
|
|
|
03-08-2006, 06:46 AM
|
#3
|
Member
Registered: Nov 2005
Location: Ontario, Canada
Distribution: Linux Mint
Posts: 116
Rep:
|
Hi George
Try this:
$ grep -c "^[^#]" foo
jer
|
|
|
03-08-2006, 08:12 AM
|
#4
|
Senior Member
Registered: May 2004
Location: Australia
Distribution: Gentoo
Posts: 3,545
Rep:
|
So you identified to yourself that the question was for a school-related task and you still answered it? Forgive my ignorance, but why..?
|
|
|
03-08-2006, 08:42 AM
|
#5
|
Senior Member
Registered: Jan 2005
Location: Manalapan, NJ
Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android
Posts: 4,593
|
I've been watching the forums, and while I initially avoided answering those that appeared to be obvious school work I noted that someone always answers, and that occassionally incorrect responses will be posted.
I know that for me, "unlearning" is very hard. At least if one of the responses is correct, then the forum response becomes a multiple choice quiz - the OP will probably have to test each to see which is correct.
For example, there are some side effects to Jerril's response... will the OP discover them? Will the OP figure out how to simplify my response by combining it with Jerril's response? By providing a working (but perhaps not optimal) response, it becomes a basis for learning.
We can't stop people from answering these questions, but at least we can nag the OP into doing the right thing and provide guidance.
When I started in computer science in the 70's, I received a lot of help from others. Later I tutored other students. Part of providing the answer (to me) is to point the questioner on the path to improving on the answer. If the OP discovers the better, simpler way, then they've really learned. If they just copy what they get, they probably won't be continuing in this field anyway.
|
|
|
03-08-2006, 09:38 AM
|
#6
|
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809
|
OP does not fit the pattern of student asking us to do homework. Most of them make one post (thread) and are never seen again.
I don't mind the short questions--it's when they cut and paste the entire assignment (test) into a post that I get annoyed.
|
|
|
03-09-2006, 02:43 AM
|
#7
|
Member
Registered: Oct 2003
Posts: 354
Original Poster
Rep:
|
cs-cam,
Quote:
Originally Posted by cs-cam
So you identified to yourself that the question was for a school-related task and you still answered it? Forgive my ignorance, but why..?
|
It is not. It is my personal interest to this topic.
regards,
George
|
|
|
03-09-2006, 02:45 AM
|
#8
|
Member
Registered: Oct 2003
Posts: 354
Original Poster
Rep:
|
pixellany,
Quote:
Originally Posted by pixellany
OP does not fit the pattern of student asking us to do homework. Most of them make one post (thread) and are never seen again.
I don't mind the short questions--it's when they cut and paste the entire assignment (test) into a post that I get annoyed.
|
Many thanks. I used C program to do such simple tasks before. I simply want to know whether there are approaches to improve it -- make things more simple.
regards,
George
|
|
|
03-09-2006, 03:29 AM
|
#9
|
Member
Registered: Oct 2003
Posts: 354
Original Poster
Rep:
|
Thanks macemoneta,
Quote:
Originally Posted by macemoneta
grep -v "^#" somefile | wc -l
PS- Try to do your own homework - this isn;t going to help you pass any tests.
|
I have tried your method. It works! Cool!
regards,
George
|
|
|
03-09-2006, 03:30 AM
|
#10
|
Member
Registered: Oct 2003
Posts: 354
Original Poster
Rep:
|
Thank you very much, macemoneta!
Quote:
Originally Posted by macemoneta
I've been watching the forums, and while I initially avoided answering those that appeared to be obvious school work I noted that someone always answers, and that occassionally incorrect responses will be posted.
I know that for me, "unlearning" is very hard. At least if one of the responses is correct, then the forum response becomes a multiple choice quiz - the OP will probably have to test each to see which is correct.
For example, there are some side effects to Jerril's response... will the OP discover them? Will the OP figure out how to simplify my response by combining it with Jerril's response? By providing a working (but perhaps not optimal) response, it becomes a basis for learning.
We can't stop people from answering these questions, but at least we can nag the OP into doing the right thing and provide guidance.
When I started in computer science in the 70's, I received a lot of help from others. Later I tutored other students. Part of providing the answer (to me) is to point the questioner on the path to improving on the answer. If the OP discovers the better, simpler way, then they've really learned. If they just copy what they get, they probably won't be continuing in this field anyway.
|
It is a great answer! You are an admirable man and good teacher. :-)
regards,
George
|
|
|
03-09-2006, 03:33 AM
|
#11
|
Member
Registered: Oct 2003
Posts: 354
Original Poster
Rep:
|
jerril,
Quote:
Originally Posted by jerril
Hi George
Try this:
$ grep -c "^[^#]" foo
jer
|
I have tried your command. One question about it,
suppose the file content is,
--------------------
[root@localhost root]# more foo.grep
line 111
# line 2
line 3#
line 4
--------------------
When I use part of your command,
--------------------
[root@localhost root]# grep -c "[^#]" foo.grep
4
--------------------
I think it should return 2 since I think this command is looking for lines which do not contain '#'. Why the return value is 4.
regards,
George
|
|
|
All times are GMT -5. The time now is 11:15 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|