LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
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


Reply
  Search this Thread
Old 03-08-2006, 06:25 AM   #1
George2
Member
 
Registered: Oct 2003
Posts: 354

Rep: Reputation: 30
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
 
Old 03-08-2006, 06:31 AM   #2
macemoneta
Senior Member
 
Registered: Jan 2005
Location: Manalapan, NJ
Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android
Posts: 4,593
Blog Entries: 2

Rep: Reputation: 344Reputation: 344Reputation: 344Reputation: 344
grep -v "^#" somefile | wc -l

PS- Try to do your own homework - this isn;t going to help you pass any tests.
 
Old 03-08-2006, 06:46 AM   #3
jerril
Member
 
Registered: Nov 2005
Location: Ontario, Canada
Distribution: Linux Mint
Posts: 116

Rep: Reputation: 16
Hi George

Try this:
$ grep -c "^[^#]" foo

jer
 
Old 03-08-2006, 08:12 AM   #4
cs-cam
Senior Member
 
Registered: May 2004
Location: Australia
Distribution: Gentoo
Posts: 3,545

Rep: Reputation: 57
So you identified to yourself that the question was for a school-related task and you still answered it? Forgive my ignorance, but why..?
 
Old 03-08-2006, 08:42 AM   #5
macemoneta
Senior Member
 
Registered: Jan 2005
Location: Manalapan, NJ
Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android
Posts: 4,593
Blog Entries: 2

Rep: Reputation: 344Reputation: 344Reputation: 344Reputation: 344
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.
 
Old 03-08-2006, 09:38 AM   #6
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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.
 
Old 03-09-2006, 02:43 AM   #7
George2
Member
 
Registered: Oct 2003
Posts: 354

Original Poster
Rep: Reputation: 30
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
 
Old 03-09-2006, 02:45 AM   #8
George2
Member
 
Registered: Oct 2003
Posts: 354

Original Poster
Rep: Reputation: 30
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
 
Old 03-09-2006, 03:29 AM   #9
George2
Member
 
Registered: Oct 2003
Posts: 354

Original Poster
Rep: Reputation: 30
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
 
Old 03-09-2006, 03:30 AM   #10
George2
Member
 
Registered: Oct 2003
Posts: 354

Original Poster
Rep: Reputation: 30
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
 
Old 03-09-2006, 03:33 AM   #11
George2
Member
 
Registered: Oct 2003
Posts: 354

Original Poster
Rep: Reputation: 30
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
 
  


Reply



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
bash: how to edit cat <filename> | grep <keyword> feature? sirpelidor Linux - Software 2 06-20-2005 02:00 PM
logrotate log files - keeping in order when using grep or cat and bash dmellem Linux - Software 6 02-11-2005 01:05 PM
output to a file - cat? grep? Godsmacker777 Linux - Newbie 6 12-08-2004 10:06 AM
Question about Grep irfanhab Linux - Newbie 6 08-23-2004 02:57 PM
grep question djgerbavore Linux - Newbie 7 07-07-2004 07:11 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 03:23 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
Open Source Consulting | Domain Registration