Escaping backslash and asterisk in egrep to match "\*"


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Escaping backslash and asterisk in egrep to match "\*"
# 1  
Old 02-28-2013
Escaping backslash and asterisk in egrep to match "\*"

So far what i've got is
Code:
egrep '^(\\)\*$'

No luck.

I've searched the web and not much luck. I know about the escape character
Code:
\

but its confusing to figure out how to use it to match a backslash and use it to escape the asterisk also. Any ides? Thanks!
# 2  
Old 02-28-2013
Quote:
Originally Posted by matthewfs
So far what i've got is
Code:
egrep '^(\\)\*$'

No luck.

I've searched the web and not much luck. I know about the escape character
Code:
\

but its confusing to figure out how to use it to match a backslash and use it to escape the asterisk also. Any ides? Thanks!
Without knowing what you're trying to match, what your input looks like, and what output you're trying to get, there isn't much we can do to help.
# 3  
Old 02-28-2013
What you have appears to work fine for me:

Code:
$ cat testit
\*
$ od -c testit
0000000   \   *  \n
0000003
$ egrep '^(\\)\*$' testit
\*

# 4  
Old 02-28-2013
Quote:
Originally Posted by Don Cragun
Without knowing what you're trying to match, what your input looks like, and what output you're trying to get, there isn't much we can do to help.
IM trying to match "\*" as a parameter when running a script. The input is "\*". I have the if statement already i just need the regular expression to not return a empty string.
Code:
elif [ "$(echo $domain | egrep '^(\\)\*$')" != "" ]; then

---------- Post updated at 05:38 PM ---------- Previous update was at 05:37 PM ----------

Quote:
Originally Posted by Chubler_XL
What you have appears to work fine for me:

Code:
$ cat testit
\*
$ od -c testit
0000000   \   *  \n
0000003
$ egrep '^(\\)\*$' testit
\*

THats what i thought but when i run my script and parameter as "/*" i don't get he output i want. THe output i want is this echo in this if statement.

Code:
elif [ "$(echo $domain | egrep '^(\\)\*$')" != "" ]; then
        echo $domain is a * wilcard

# 5  
Old 02-28-2013
What you want is:

Code:
elif (echo $domain | egrep -q '^(\\)\*$') ; then
        echo $domain is a * wilcard

# 6  
Old 02-28-2013
Quote:
Originally Posted by matthewfs
IM trying to match "\*" as a parameter when running a script. The input is "\*". I have the if statement already i just need the regular expression to not return a empty string.
Code:
elif [ "$(echo $domain | egrep '^(\\)\*$')" != "" ]; then

---------- Post updated at 05:38 PM ---------- Previous update was at 05:37 PM ----------


THats what i thought but when i run my script and parameter as "/*" i don't get he output i want. THe output i want is this echo in this if statement.

Code:
elif [ "$(echo $domain | egrep '^(\\)\*$')" != "" ]; then
        echo $domain is a * wilcard

Are you looking for /* or for \*?

How was domain set? The results are very different if domain is set by:
Code:
domain=\*

as opposed to:
Code:
domain="\*"

What is the output from the command:
Code:
echo "$domain"|od -c

when the egrep is not giving you the results you expected.
# 7  
Old 03-01-2013
Quote:
Originally Posted by Don Cragun
Are you looking for /* or for \*?

How was domain set? The results are very different if domain is set by:
Code:
domain=\*

as opposed to:
Code:
domain="\*"

What is the output from the command:
Code:
echo "$domain"|od -c

when the egrep is not giving you the results you expected.
sorry the /* is wrong it should be \* and its "\*" a string. I will run the command and check it out (the od -c command)

---------- Post updated at 12:25 PM ---------- Previous update was at 12:04 AM ----------

So i found out the output of
Code:
echo "$domain"|od -c

is
Code:
0000000   *  \n
0000002

which is right because i am trying to pass an asterisks as the first parameter when i call my script. But when the script runs , checks the parameter and get to the asterisk part of my if statement, it does not go through. Here is the part of the if statement that check for the asterisks
Code:
elif [ "$(echo $domain | egrep '^\\*$')" != "" ]; then
        checkItem

I know the whole if statement whole because for every other condition i get the right output. BUt when i enter a asterisks as a parameter, checkItem function does not get called. I have a feeling its the regular expression.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search file containing ps results for a match "my.cnf" and then for a second match . "ok:" and

I need to find two matches in the output from ps. I am searching with ps -ef |grep mysql for: my.cnf /bin/sh /usr/bin/mysqld_safe --defaults-file=/data/mysql/master/agis_core/etc/my.cnf after this match I want to search back and match the hostname which is x number of lines back, above the... (2 Replies)
Discussion started by: bash_in_my_head
2 Replies

2. 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

3. UNIX for Advanced & Expert Users

AIX - io info get from "libperfstat" not match "iostat"

Hi, everyone. I need to write a program to get io info based on libperfstat. But the "write time" of a disk is just half of the value get from iostat. I'm confused and can't explain. Help please. How I calculate "write service time per sec": In iostat: write service... (0 Replies)
Discussion started by: jackliang
0 Replies

4. UNIX for Dummies Questions & Answers

Egrep confusion with "I" and "-I" pattern

I am executing following command egrep -w I filename.txt the filename.txt has following data .... -I 07-18 08:31:19.924 9880 6 SessionManager ConnectConfig: ConfigurationWebService LoginResults=SuccessfulLogin I am so hungry that I need to eat I expect egrep to print only the second... (1 Reply)
Discussion started by: VBG
1 Replies

5. Shell Programming and Scripting

sed command escaping backslash "/"

Hello friends/'unix experts', i have a file as below cat sample.txt satish /rakesh/ sandhya /sandeep/ i have to replace /rakesh/ with rakesh, how can i do it with sed, i tried below code but its throwing errors sed -e 's/'"\(/rakesh/)\"'/\1rakesh/g' sample.txt ... (1 Reply)
Discussion started by: only4satish
1 Replies

6. Shell Programming and Scripting

Disabling Backslash Interpretation with "echo -E"?

Hello All, In a Bash Script I'm writing I have a section where I loop through a text file that was outputted by another script. In the text file some of the strings in there are enclosed with the BOLD "character sequences" (i.e. "\033But it's weird, because if I run this command: echo -E... (12 Replies)
Discussion started by: mrm5102
12 Replies

7. 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

8. UNIX for Dummies Questions & Answers

How to use the "grep/egrep" command to search files.

Hi Team, I am new to this forum and also trying to learn Unix. I will highly appriciate your help if you can help me to get the right command . {{{ I use the command " today | egrep '(10:| 11: )' | grep ERROR " to grep all the files that has been error betweeen 10 to 11... (6 Replies)
Discussion started by: rkhanal
6 Replies

9. UNIX for Dummies Questions & Answers

search ")" with egrep - egrep: syntax error

Hi Guys, we have a shell script which basically query the Database which retrieves huge data and use the data with "egrep" . Now there is some data which contains characters like "abc)" and the same is used like below : "egrep (.+\|GDPRAB16\|GDPR/11702 96 abc)\|$ temp.txt" now while... (7 Replies)
Discussion started by: sagarjani
7 Replies

10. 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
Login or Register to Ask a Question