Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to grep for the dollar symbol ($)?

Tags:

linux

grep

unix

% cat temp $$$ hello1 $$  hello2     hello3 ##  hello4     hello5 $$$ % cat temp | grep "$$$" Illegal variable name. % cat temp | grep "\$\$\$" Variable name must contain alphanumeric characters. % 

I want to grep for $$$ and I expect the result to be

% cat temp | grep <what should go here?> $$$ hello1     hello5 $$$ % 

To differentiate, I have marked the prompt as %.

  • What is the problem here?
  • What should the grep string be?
like image 588
Lazer Avatar asked Sep 21 '10 11:09

Lazer


People also ask

How do you grep a symbol?

grep is very often used as a "filter" with other commands. It allows you to filter out useless information from the output of commands. To use grep as a filter, you must pipe the output of the command through grep . The symbol for pipe is " | ".

What does grep $1 do?

grep is a program that searches for regular expressions. The first argument for grep is the pattern to look for. In scripts and functions $1 is a reference to the first argument passed to that script or function.

How do you grep special characters?

To match a character that is special to grep –E, put a backslash ( \ ) in front of the character. It is usually simpler to use grep –F when you don't need special pattern matching.

How do I print dollar sign in Linux?

The answer is to use either \$ or single quotes. echo '$PATH' will print $PATH echo '\$PATH' will print \$PATH echo "\$PATH" will print $PATH echo ''\$PATH'' will technically do the right thing, but the two null strings serve no purpose.


2 Answers

The problem is that the shell expands variable names inside double-quoted strings. So for "$$$" it tries to read a variable name starting with the first $.

In single quotes, on the other hand, variables are not expanded. Therefore, '$$$' would work – if it were not for the fact that $ is a special character in regular expressions denoting the line ending. So it needs to be escaped: '\$\$\$'.

like image 159
Konrad Rudolph Avatar answered Sep 21 '22 03:09

Konrad Rudolph


When you use double quotes " or none use double\: "\\\$\\\$\\\$"

cat t | grep \\\$\\\$\\\$  

if you use in single quotes ' you may use:

cat t | grep '\$\$\$' 
like image 40
Gadolin Avatar answered Sep 24 '22 03:09

Gadolin