Trying to grep for '--' occurance


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Trying to grep for '--' occurance
# 1  
Old 06-16-2011
Trying to grep for '--' occurance

Hello

Im trying to grep for a string in grub.conf . I've used the -F option since its a long string, but when i execute, i run into errors. Script and output below.

GRUBPASSWD="password --md5 xyz"
if grep -Fxq $GRUBPASSWD /etc/grub.conf
then
.
.
output:
grep: unrecognized option `--md5'

Can anyone please help?

Thanks
# 2  
Old 06-16-2011
Try this line instead as the --md5 is being mistaken for a command line and the quotes may help too.

Code:
if grep -Fxq -- "$GRUBPASSWD" /etc/grub.conf

This User Gave Thanks to SteveDawe For This Post:
# 3  
Old 06-16-2011
How about '[-]-md5' ? The square brackets convince grep that it's not an option without altering the meaning of the expression. This trick's also useful for grepping for program names -- instead of ps | grep program | grep -v grep, just ps | grep '[p]rogram' and grep won't match its own existence.
# 4  
Old 06-16-2011
Quote:
Originally Posted by SteveDawe
Try this line instead as the --md5 is being mistaken for a command line and the quotes may help too.

Code:
if grep -Fxq -- "$GRUBPASSWD" /etc/grub.conf

This worked...thank you!!

Quote:
Originally Posted by Corona688
How about '[-]-md5' ? The square brackets convince grep that it's not an option without altering the meaning of the expression. This trick's also useful for grepping for program names -- instead of ps | grep program | grep -v grep, just ps | grep '[p]rogram' and grep won't match its own existence.

I tried this, enclosing the --md5 as you suggested.

This is the output
grep: '[-]-md5': No such file or directory
grep: xyy.: No such file or directory

Thanks
# 5  
Old 06-16-2011
My apologies, I misunderstood the problem: Your string was getting split on spaces. The quotes like SteveDave suggested fix it. (I don't think the -- before "$GRUBPASSWORD" is necessary but it certainly doesn't hurt, either.)
# 6  
Old 06-16-2011
Yes, they work with or without the "--" before the variable.

Thanks again
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Extract only first occurance

Hi All, From the below file. I need to get only the first occurrence and print. I tried to do it in separate grep not coming as expected Original file 11001;1213;304;;;;;;;111020677.64;;;;;;;;;;;;;;;;;;;;;;;;;; 11001;1214;304;;;;;;;102376462.96;;;;;;;;;;;;;;;;;;;;;;;;;;... (5 Replies)
Discussion started by: arunkumar_mca
5 Replies

2. Shell Programming and Scripting

Grep word after last occurance of string and display next few lines

Hi, I wanted to grep string "ERROR" and "WORNING" after last occurrence of String "Starting" only and wanted to display two lines after searched ERROR and WORNING string and one line before. I have following cronjob log file "errorlog" file and I have written the code for same in Unix as below... (17 Replies)
Discussion started by: nes
17 Replies

3. Shell Programming and Scripting

Multiple occurance of file

Hi all, I have file structure as file.log 84t-rw-r--r-- 1 emily04 us_cms 24492717 Oct 5 13:29 vgtree_84_1_K3L.root 85t-rw-r--r-- 1 emily04 us_cms 50410380 Oct 5 16:06 vgtree_85_1_uZv.root 85t-rw-r--r-- 1 emily04 us_cms 50567380 Oct 5 16:06 vgtree_85_1_hjv.root 86t-rw-r--r-- 1 emily04... (4 Replies)
Discussion started by: emily
4 Replies

4. Shell Programming and Scripting

replace every second occurance of a string

I want to replace every 2nd occurance of a string/character from a line. ababacbsbddbsbbcbdbssb i want to replace every s2nd b with @ like below should be like aba@acbs@ddbs@bc@dbss@ (3 Replies)
Discussion started by: ratheeshjulk
3 Replies

5. Shell Programming and Scripting

First occurance

A PERL script that prints just the first occurrence of a string in a file and immediately exits (the string and the filename are the first and the second command line arguments; I used filehandle to open an input file). (1 Reply)
Discussion started by: aadi_uni
1 Replies

6. Shell Programming and Scripting

Count occurance of multiple strings using grep command

How to grep multiple string occurance in input file using single grep command? I have below input file with many IDP, RRBE messages. Out put should have count of each messages. I have used below command but it is not working grep -cH "(sent IDP Request)(Recv RRBCSM)" *.txt ... (5 Replies)
Discussion started by: sushmab82
5 Replies

7. Shell Programming and Scripting

Copy subsequent contents of a file from first occurance of grep

There is a file which logs all errors and alerts of the database called alert log. I have a requirement as follows: 1. Check the current date and search for the first occurance of the current date in the alert log. 2. As soon as the first occurance is found, copy the subsequent contents... (5 Replies)
Discussion started by: sunpraveen
5 Replies

8. Shell Programming and Scripting

Split on last occurance

I want to call a script like this: ./somescript some-thing.knows.what.ending Inside the script it needs to split at last .(period) so I can: a=some-thing.knows.what b=ending I know I can do it in perl but im still learing awk and sed. Thanks (3 Replies)
Discussion started by: Ikon
3 Replies

9. Shell Programming and Scripting

How to insert values in 1st occurance out of two occurance in a file

Hi I have a file which contains the following two lines which are same But I would like to insert the value=8.8.8.8 in the 1st occurance line and value=9.9.9.9 in the 2nd occurance line. <parameter name="TestIp1" value=""> <parameter name="TestIp1" value=""> Please suggest (1 Reply)
Discussion started by: madhusmita
1 Replies

10. Shell Programming and Scripting

awk: last occurance

Hi All, I need to extract the last occurance of a pattern match. So far I've got the code below which extracts the first occurance. Any ideas how I can modify it so that it extracts the last? BEGIN {} { if (data++ == 0) ... (17 Replies)
Discussion started by: pondlife
17 Replies
Login or Register to Ask a Question