LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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 10-12-2010, 07:11 PM   #1
vendtagain
Member
 
Registered: Sep 2009
Distribution: Slackware, Debian, Mac OS X, Zenwalk, Puppy, Gentoo
Posts: 199

Rep: Reputation: 32
difference between 'grep "\n" * | wc -l' and 'wc -l *' total count


I am trying to find the number of lines in a directory containing .h and .cpp files.
When I type
Code:
grep "\n" * | wc -l
this prints about 2000.
Code:
wc -l *
The total count that this prints is nearly twice as much, about 4000. From reading man pages, it seems that both of these should print the same number, the number of lines in the files, but they are definitely not equal.
 
Old 10-12-2010, 07:33 PM   #2
Meson
Member
 
Registered: Oct 2007
Distribution: Arch x86_64
Posts: 606

Rep: Reputation: 67
The grep one isn't doing what you think it is. Try doing it with the pipe. You have 2000 lines that contain the letter n.
 
Old 10-12-2010, 07:42 PM   #3
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Indeed ... grep "\n" doesn't grep for newlines. If you want that
you need to do
Code:
grep -E '$' *
 
Old 10-12-2010, 08:30 PM   #4
vendtagain
Member
 
Registered: Sep 2009
Distribution: Slackware, Debian, Mac OS X, Zenwalk, Puppy, Gentoo
Posts: 199

Original Poster
Rep: Reputation: 32
Quote:
Originally Posted by Tinkster View Post
Indeed ... grep "\n" doesn't grep for newlines. If you want that
you need to do
Code:
grep -E '$' *
Thanks Tinkster, this worked correctly.

Quote:
The grep one isn't doing what you think it is. Try doing it with the pipe. You have 2000 lines that contain the letter n.
You are right, I checked grep with "n" instead of "\n" and it produced the same results, I guess it ignores the backslash..
 
Old 10-12-2010, 08:32 PM   #5
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Quote:
Originally Posted by vendtagain View Post
Thanks Tinkster, this worked correctly.


You are right, I checked grep with "n" instead of "\n" and it produced the same results, I guess it ignores the backslash..
Yup, it does. As far as regular expressions go \n has
no meaning, so the escape just gets dropped.

Well - it does have a meaning as in back-referencing, but
that's not what you were doing (or trying to do).


Cheers,
Tink
 
  


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
how can I "cat" or "grep" a file to ignore lines starting with "#" ??? callagga Linux - Newbie 7 08-16-2013 06:58 AM
newbie question: whats the difference between "su root", "su" and "su -&quo mojarron Slackware 9 12-07-2009 04:08 PM
Problem "$value=`mpstat 1 1 | grep "Average"`;" Alias pipe return nothing adamlucansky Linux - General 8 09-25-2009 07:26 AM
difference between "Web server local URL" and "IPv4 address"? kpachopoulos Linux - General 2 09-17-2004 01:30 PM
"Undeleting" data using grep, but get "grep: memory exhausted" error SammyK Linux - Software 2 03-13-2004 03:11 PM

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

All times are GMT -5. The time now is 04:26 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