LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 09-26-2016, 05:45 AM   #1
paviter619
LQ Newbie
 
Registered: Sep 2016
Posts: 1

Rep: Reputation: Disabled
Linux


I have to complete the following question but have no idea how. I am new have tried everything but stuck on this last one.

Question: Use a pipe to combine grep and wc to count the number of files in your home directory that (2)

contain the # mark. Then modify the command to count how many lines there are in total

across all of those files. For both of these commands, redirect standard error so that any errors

get sent to /dev/null.

Hint: to get grep to only match once per file, use the -m option.
 
Old 09-26-2016, 07:29 AM   #2
aragorn2101
Member
 
Registered: Dec 2012
Location: Mauritius
Distribution: Slackware
Posts: 552

Rep: Reputation: 273Reputation: 273Reputation: 273
Hi,

First let me tell you that it seems inappropriate for someone to post their homework here. But, if at least, you had posted what you have tried, it would encourage us to help you more.

Let me explain a few things, then you try to construct an answer from these. If you have problems you ask.

Code:
grep PATTERN FILENAME
will look for PATTERN in the file FILENAME and print every line containing this pattern from the file.

Code:
grep -m 3 PATTERN FILENAME
will look for PATTERN in the file but will print only the first 3 lines having the matching pattern. Please verify this by reading the man pages for grep.

Code:
grep PATTERN *
will look for PATTERN in all files in current directory.

Code:
wc -l FILENAME
will count all lines in the file FILENAME. Again, please read the man pages for wc.

Ok, I'll give you one of the answers. To redirect errors to /dev/null, use this:
Code:
somecommand  2> /dev/null
For the answer to the second question, try to use cat along with wc.

Man pages are a great source of documentation. Also, have a look at Introduction to Linux - A Hands on Guide at http://www.tldp.org/guides.html. Read about I/O redirection, and later, read the whole book if you have time. You'll learn a lot.

Cheers.

Last edited by aragorn2101; 09-26-2016 at 07:32 AM.
 
2 members found this post helpful.
Old 09-26-2016, 08:46 AM   #3
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (current), FreeBSD, Win10, It varies
Posts: 9,952

Rep: Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148
first you have to figure out how to get a count on your files, then using the pipe key add it to grep then pipe again to wc to get count . google how tos on grep and wc and pipe using the key word linux to get more ideas on how to use the pipe key along with the functions in the terminal ~ cli

don't forget to look up how to redirect output to /dev/null while you're at it

their are plenty of examples on how to do almost everything linux now days.

not that using grep why ?? ls dir pipe wc redirect works like a charm too. Unless you're removing links to the count. where you'd use grep to do so. hum perhaps you better learn how to use the internet to gather resoures rather then just giving up on the homework and asking for the answer.

it will broden your skill set on how to find what you need in a more skilfull manner.

using grep to get number of files in directory linux

Last edited by BW-userx; 09-26-2016 at 09:05 AM.
 
Old 09-26-2016, 08:51 AM   #4
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 23,520

Rep: Reputation: 6737Reputation: 6737Reputation: 6737Reputation: 6737Reputation: 6737Reputation: 6737Reputation: 6737Reputation: 6737Reputation: 6737Reputation: 6737Reputation: 6737
Quote:
Originally Posted by paviter619 View Post
I have to complete the following question but have no idea how. I am new have tried everything but stuck on this last one.

Question: Use a pipe to combine grep and wc to count the number of files in your home directory that (2) contain the # mark. Then modify the command to count how many lines there are in total across all of those files. For both of these commands, redirect standard error so that any errors get sent to /dev/null.

Hint: to get grep to only match once per file, use the -m option.
Please read the LQ Rules about posting verbatim homework questions, and about NOT DOING IT. We are happy to HELP you, but as aragorn2101 said, you need to show us what YOU have done first.

The homework question even gives you the flag to use for grep, tells you to use pipe and wc too...seems very clear as to what you should be looking at, so show us what you've done.
 
1 members found this post helpful.
Old 09-26-2016, 12:35 PM   #5
hazel
LQ Guru
 
Registered: Mar 2016
Location: Harrow, UK
Distribution: LFS, AntiX, Slackware
Posts: 5,191
Blog Entries: 14

Rep: Reputation: 3000Reputation: 3000Reputation: 3000Reputation: 3000Reputation: 3000Reputation: 3000Reputation: 3000Reputation: 3000Reputation: 3000Reputation: 3000Reputation: 3000
Oh, and while we're at it, do try to use more informative titles for your posts. I mean "Linux"? What the heck does that mean? This whole forum is about Linux.
 
1 members found this post helpful.
Old 09-28-2016, 05:37 AM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 7.7 (?), Centos 8.1
Posts: 17,831

Rep: Reputation: 2559Reputation: 2559Reputation: 2559Reputation: 2559Reputation: 2559Reputation: 2559Reputation: 2559Reputation: 2559Reputation: 2559Reputation: 2559Reputation: 2559
1. Definitely a better title
2. show us what you've tried so far

(For everybody else, there is a different grep flag I'd use.. )
 
2 members found this post helpful.
Old 09-28-2016, 09:03 AM   #7
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,223
Blog Entries: 13

Rep: Reputation: 4286Reputation: 4286Reputation: 4286Reputation: 4286Reputation: 4286Reputation: 4286Reputation: 4286Reputation: 4286Reputation: 4286Reputation: 4286Reputation: 4286
Quote:
Originally Posted by paviter619 View Post
I have to complete the following question but have no idea how. I am new have tried everything but stuck on this last one.

Question: Use a pipe to combine grep and wc to count the number of files in your home directory that (2)

contain the # mark. Then modify the command to count how many lines there are in total

across all of those files. For both of these commands, redirect standard error so that any errors

get sent to /dev/null.

Hint: to get grep to only match once per file, use the -m option.
Well did you at least try the -m option?

Welcome to LQ, please read the following link regarding LQ Rules - See Bullet #12 and note that your first post is not a proper post.

Note that if this is how you do your homework and a fairly simple question such as this is causing you to post a question, then you aren't quite of the correct mindset yet to help yourself be successful with learning. Just an opinion.
 
Old 09-28-2016, 10:27 AM   #8
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (current), FreeBSD, Win10, It varies
Posts: 9,952

Rep: Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148
Quote:
Originally Posted by chrism01 View Post
1. Definitely a better title
2. show us what you've tried so far

(For everybody else, there is a different grep flag I'd use.. )
flag - flag - flag why does everybody have thier own flag? lol
I actually found the anwser by doing some quick research on it , but I am not posting it in here, but
keywords anyone?
 
Old 09-28-2016, 05:49 PM   #9
c0wb0y
Member
 
Registered: Jan 2012
Location: Inside the oven
Distribution: Windows
Posts: 417

Rep: Reputation: 74
Sounds like a homework to me. Please don't be complacent and lazy. Learn things the right way which is to read and comprehend things.
 
Old 09-28-2016, 11:15 PM   #10
jamison20000e
Senior Member
 
Registered: Nov 2005
Location: ...uncanny valley... infinity\1975; (randomly born:) Milwaukee, WI, US( + travel,) Earth( I wish,) END BORDER$!◣◢┌∩┐ Fe26-E,e...
Distribution: any GPL that works well on my cheapest; has been KDE or CLI but open... http://goo.gl/NqgqJx &c ;-)
Posts: 4,229
Blog Entries: 3

Rep: Reputation: 1394Reputation: 1394Reputation: 1394Reputation: 1394Reputation: 1394Reputation: 1394Reputation: 1394Reputation: 1394Reputation: 1394Reputation: 1394
Cool

Hi.

If you don't mind me asking what degree* you taking? School in general's a good call GNU\Linux even better,,, here is my favorite study tip: http://www.linuxquestions.org/questi...4/#post5179283 (ie passive\active;) have fun!

Last edited by jamison20000e; 09-28-2016 at 11:16 PM. Reason: fixed link
 
1 members found this post helpful.
Old 09-29-2016, 09:16 PM   #11
Fred Caro
Senior Member
 
Registered: May 2007
Posts: 1,007

Rep: Reputation: 167Reputation: 167
well grep options

but it seems that grep is less of a preference than exams.

Fred.
 
  


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



LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:32 AM.

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