How to grep "--"


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to grep "--"
# 1  
Old 07-18-2012
How to grep "--"

Just looking for
Code:
cat file | grep "--"

# 2  
Old 07-18-2012
I'm not clear on the question. Are you looking to match records containing a double hyphen?

Try:-
Code:
grep "\-\-" file

The problem could be just that the hyphen is a special character, so you need to use the backslash to define you string to use it as just some text.


I hope that this helps

Robin
Liverpool/Blackburn
UK
This User Gave Thanks to rbatte1 For This Post:
# 3  
Old 07-18-2012
Quote:
Originally Posted by rbatte1
I'm not clear on the question. Are you looking to match records containing a double hyphen?

Try:-
Code:
grep "\-\-" file

The problem could be just that the hyphen is a special character, so you need to use the backslash to define you string to use it as just some text.


I hope that this helps

Robin
Liverpool/Blackburn
UK
It worked. I tried without the quotes but obviously didn't apply. To show lines containing two hyphens and not more I use:
Code:
cat file | grep "\-\-" | grep -v "\-\-\-"


Last edited by Tribe; 07-18-2012 at 01:31 PM..
# 4  
Old 07-18-2012
Excellent. Pleased to be of assistance.



Robin
# 5  
Old 07-18-2012
See also Useless Use of Cat.

Code:
grep "\-\-" file | grep -v "\-\-\-"

Or you can do it all in one awk:

Code:
awk '/--/ && !/---/' file

This User Gave Thanks to Corona688 For This Post:
# 6  
Old 07-18-2012
Quote:
Originally Posted by Corona688
See also Useless Use of Cat.

Code:
grep "\-\-" file | grep -v "\-\-\-"

Or you can do it all in one awk:

Code:
awk '/--/ && !/---/' file

Loved that one
Code:
 <file awk '/--/ && !/---/'

# 7  
Old 07-19-2012
Quote:
Originally Posted by Tribe
Just looking for
Code:
cat file | grep "--"


-- itself is the solution. If you ever need to grep these kind of words/strings etc, use "--" to disable to treat them as a switch

Code:
$ cat f3
--
-i
-v
$ 


$ grep -- -i f3
-i
$ grep -- -v f3
-v
$ grep -- -- f3
--


This also applies to other commands.

e.g.
Code:
$ touch -t
touch: option requires an argument -- t
Try `touch --help' for more information.
$ 
$
$ touch -- -t
$
$ rm -t
rm: unknown option -- t
Try `rm ./-t' to remove the file `-t'.
Try `rm --help' for more information.
$
$
$ rm -- -t
$


Hope it helps.
These 2 Users Gave Thanks to clx For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. Shell Programming and Scripting

Failure: if grep "$Var" "$line" inside while read line loop

Hi everybody, I am new at Unix/Bourne shell scripting and with my youngest experiences, I will not become very old with it :o My code: #!/bin/sh set -e set -u export IFS= optl="Optl" LOCSTORCLI="/opt/lsi/storcli/storcli" ($LOCSTORCLI /c0 /vall show | grep RAID | cut -d " "... (5 Replies)
Discussion started by: Subsonic66
5 Replies

3. Shell Programming and Scripting

grep with "[" and "]" and "dot" within the search string

Hello. Following recommendations for one of my threads, this is working perfectly : #!/bin/bash CNT=$( grep -c -e "some text 1" -e "some text 2" -e "some text 3" "/tmp/log_file.txt" ) Now I need a grep success for some thing like : #!/bin/bash CNT=$( grep -c -e "some text_1... (4 Replies)
Discussion started by: jcdole
4 Replies

4. Shell Programming and Scripting

Problem with "find" and "grep" command

I want to list all files/lines which except those which contain the pattern ' /proc/' OR ' /sys/' (mind the leading blank). In a first approach I coded: find / -exec ls -ld {} | grep -v ' /proc/| /sys/' \; > /tmp/list.txt But this doesn't work. I got an error (under Ubuntu): grep:... (5 Replies)
Discussion started by: pstein
5 Replies

5. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

6. Shell Programming and Scripting

ps -ef | grep "string1" "string2" " "string3"

Hi all, can any one suggest me the script to grep multiple strings from ps -ef pls correct the below script . its not working/ i want to print OK if all the below process are running in my solaris system. else i want to print NOT OK. bash-3.00$ ps -ef | grep blu lscpusr 48 42 ... (11 Replies)
Discussion started by: steve2216
11 Replies

7. AIX

xx=`date +"%a %b %d"`;rsh xxx grep "^$XX" zzz ?

AIX 4.2 I am trying to do an rsh grep to search for date records inside server logs by doing this : xx=`date +"%a %b %d"` rsh xxx grep "^$XX" zzz gives : grep: 0652-033 Cannot open Jun. grep: 0652-033 Cannot open 11. But if I do : xx=`date +"%a %b %d"` grep "^$XX" zzz it works... (2 Replies)
Discussion started by: Browser_ice
2 Replies

8. Shell Programming and Scripting

ls -laR | grep "^-" | awk '{print $9}'| grep "$.txt"

Hi, I don't know hot to make this command work: ls -laR | grep "^-" | awk '{print $9}'| grep "$.txt" It should return the list of file .txt It's important to search .txt at the end of the line, becouse some file name have "txt" in their name but have other extensions (13 Replies)
Discussion started by: DNAx86
13 Replies

9. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies

10. Shell Programming and Scripting

grep to find content in between curly braces, "{" and "},"

problem String ~~~~~~~~~~~~~~~~~~ icecream= { smart peopleLink "good" LC "happy" , smartpeopleLink "dull" LC "sad" } aend = {smart vc4 eatr kalu} output needed ~~~~~~~~~~~~~~~~~~ smart peopleLink "good" LC "happy" , smartpeopleLink "dull" LC "sad" smart vc4... (4 Replies)
Discussion started by: keshav_rk
4 Replies
Login or Register to Ask a Question