Morris Li
Morris Li

Reputation: 402

How to use grep to find strings begin with hypen(-)?

For example:

man perl | grep '-w'

only to receive error messages

Upvotes: 0

Views: 162

Answers (3)

tso
tso

Reputation: 4924

or just escape - character:

man perl | grep '\-w' 

output:

   also use the -w flag, but its use is normally discouraged, because it

Upvotes: 2

Roman Cheplyaka
Roman Cheplyaka

Reputation: 38738

Use the double hyphen:

% man perl | grep -- '-w'                                                                                                                                                                    
       The "use warnings" pragma produces some lovely diagnostics. One can also use the -w flag, but its use is normally discouraged, because it gets applied to all executed Perl code,

This usage of -- is recommended by the POSIX standard:

The first -- argument that is not an option-argument should be accepted as a delimiter indicating the end of options. Any following arguments should be treated as operands, even if they begin with the - character.

Upvotes: 4

ncowboy
ncowboy

Reputation: 1335

Use -- to give a parameter which starts with - .

MacBook-Pro:~ chen$ man perl | grep -- '-w'
       also use the -w flag, but its use is normally discouraged, because it
MacBook-Pro:~ chen$ 

In most bash builtin & some other commands, the -- means the end of options. By the same token, you can delete a file which name is -f.txt using this command:

MacBook-Pro:tmp chen$ rm -fv -- -f.txt 
-f.txt
MacBook-Pro:tmp chen$ 

Upvotes: 1

Related Questions