LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 12-05-2010, 11:14 AM   #1
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Rep: Reputation: 76
Making grep find files containing '$' (the dollar sign char).


grep (GNU grep) 2.5

Hi:
I want to find files containing the "$" char (ascii 0x24). 'Grep -irl $ *' would output the names of every file in path *, of course, because it means end of line (EOL). So giving grep the string "$" won't do. So I tried 'grep -irl \$ *'. But this doesn't work either and I do not understand why. Am I not escaping the dollar sign? grep should interpret it literally. Neither 'grep -irl "$" *' will work. Fortunately, there's LQ, besides grep's man page. Any hint? Thanks for reading.

Last edited by stf92; 12-05-2010 at 11:46 AM.
 
Old 12-05-2010, 11:22 AM   #2
AlucardZero
Senior Member
 
Registered: May 2006
Location: USA
Distribution: Debian
Posts: 4,824

Rep: Reputation: 615Reputation: 615Reputation: 615Reputation: 615Reputation: 615Reputation: 615
There's some sort of tomfoolery going on not just with grep, but with your shell, because $ denotes a variable. This will work:

Code:
grep -irl \\$ *
I imagine it's because you want grep to get that \, not the shell, though I'm not sure what the shell is doing with it.
 
Old 12-05-2010, 11:35 AM   #3
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Original Poster
Rep: Reputation: 76
That certainly worked and I thank you. But can somebody tell me what \$ is to grep? If \$ is a var name, then a second backslash will take away that meaning, leaving this one: escaped dollar sign; which in turn takes away the meaning "end of line" leaving a mere dollar sign. But is \$ really a var name to bash (or to the shell)?

EDIT: I'm very sorry. It's all wrong. I reread AlucardZero's post and think I've now got it right.

EDIT 2: one thinks LQers will get bored with the whole story, but sometimes it's the best option. So here it goes:
I know some ebook in the HDD contains this line:
Quote:
for i in $FILE
which is part of a script I want to consult. And so the need for grep. That's all.

Last edited by stf92; 12-05-2010 at 11:47 AM.
 
Old 12-05-2010, 11:48 AM   #4
AlucardZero
Senior Member
 
Registered: May 2006
Location: USA
Distribution: Debian
Posts: 4,824

Rep: Reputation: 615Reputation: 615Reputation: 615Reputation: 615Reputation: 615Reputation: 615
The $ character in regular expressions denotes the end of the line, so you have to escape it to search for a literal $.

But like I said, not sure exactly what's going on with the shell.
 
1 members found this post helpful.
Old 12-05-2010, 12:04 PM   #5
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Original Poster
Rep: Reputation: 76
Well, maybe you're not sure. But I'll keep this thread anyways in my hard disk. Regards.
 
Old 12-05-2010, 09:37 PM   #6
mahu_mahu
Member
 
Registered: Dec 2007
Location: Japan
Distribution: Debian
Posts: 42

Rep: Reputation: 3
When you invoke
Code:
grep -irl \$ *
at first shell translates \$ to $ and give it to grep.
So above code is equivalent to
Code:
grep -irl $ *
But if you invoke
Code:
grep -irl \\$ *
shell translates \\ to \ and gives '-irl \$ *' to grep.
and you can get what you want.
 
2 members found this post helpful.
Old 12-05-2010, 10:32 PM   #7
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Hi,

alternatively you could use single quotes
Code:
grep '\$' file
This way bash will not try to interpret anything between the single quotes but just pass it "as is" to grep.
 
1 members found this post helpful.
Old 12-06-2010, 04:07 AM   #8
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Original Poster
Rep: Reputation: 76
All this is highly instructive, and thanks be given to these wonderful LQers. But then a question arises: how to know, save by careful study of a manual page, as of now several thousand lines long, the scope of things "eaten" by the shell and, therefor, what is left to programs like grep?

I presume ignorance interrogates once and again.
 
Old 12-06-2010, 07:59 PM   #9
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
http://tldp.org/LDP/abs/html/special-chars.html

Enjoy
 
1 members found this post helpful.
Old 12-08-2010, 11:16 AM   #10
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Original Poster
Rep: Reputation: 76
Thanks Chris for the link, and may I not have to fall back on LQ so heavily in the future.
 
  


Reply

Tags
grep



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
Problem Searching for php files with Find and Grep ssmela Linux - General 3 11-02-2010 01:55 PM
Howto find out files that are changed less than 10 hours with grep command coolloo_djack Programming 1 03-20-2010 01:05 PM
Find/grep/wc command to find matching files, print filename and word count dbasch Linux - Newbie 10 09-14-2009 05:55 PM
searching within files. cat grep find w3bd3vil Linux - Newbie 4 10-13-2008 03:59 AM
Xubuntu char set support (pound sign) peoplesrepublican Ubuntu 1 12-15-2006 06:58 AM

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

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