Visit Our UNIX and Linux User Community


Assistence With Using Asterisks in GREP Expressions


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Assistence With Using Asterisks in GREP Expressions
# 1  
Old 07-22-2009
Assistence With Using Asterisks in GREP Expressions

I am attempting to find all complete words which contain an asterisk at the beginning and the end - for instance, "*Hello?*" or "*you*".

From what I've read, I would have thought that the following expression would do that just fine:

\<\*.*\*\>

\< denoting the beginning of a word.

\* escaping the first asterisk

.* acting as a placeholder for any number of characters

\* escaping the second asterisk

and finally

\> to denote the end of the word.

Yet, the string doesn't do a thing. Why might that be? If it matters, I'm using this string in InDesign, but I figured here would be a better place to ask since it's technically a scripting issue. Might InDesign be using a pseudo-GREP structure, or is my script just wrong?

Thanks.
# 2  
Old 07-22-2009
from what i know when were dealing with asterisk(*) char in a line we need to use double qoutes. This evaluates the "middle *" (.*) and use escape character on both the first character (^) and last character ($).

Code:
-bash-3.2$ cat test
*ryan*
*you
*me*
-bash-3.2$ cat test | grep "^\*.*\*$"
*ryan*
*me*
-bash-3.2$

# 3  
Old 07-22-2009
Hop this is what you are looking for

Code:
sh-3.2$ cat f1
word1 word2 *word3* word4
word5 *word6*
*word7* word8* *word9*

sh-3.2$ grep -ow "*[^*]*\*" f1
*word3*
*word6*
*word7*
*word9*
sh-3.2$

# 4  
Old 07-22-2009
Quote:
Originally Posted by ranjithpr
Hop this is what you are looking for

Code:
sh-3.2$ cat f1
word1 word2 *word3* word4
word5 *word6*
*word7* word8* *word9*

sh-3.2$ grep -ow "*[^*]*\*" f1
*word3*
*word6*
*word7*
*word9*
sh-3.2$

you did not include last char check

Code:
-bash-3.2$ cat test
*wor*d1*
*wor*d
-bash-3.2$ grep -ow "*[^*]*\*" test
-bash-3.2$

# 5  
Old 07-22-2009
Quote:
Originally Posted by ryandegreat25
from what i know when were dealing with asterisk(*) char in a line we need to use double qoutes. This evaluates the "middle *" (.*) and use escape character on both the first character (^) and last character ($).

Code:
-bash-3.2$ cat test
*ryan*
*you
*me*
-bash-3.2$ cat test | grep "^\*.*\*$"
*ryan*
*me*
-bash-3.2$

Quote:
Originally Posted by ranjithpr
Hop this is what you are looking for

Code:
sh-3.2$ cat f1
word1 word2 *word3* word4
word5 *word6*
*word7* word8* *word9*

sh-3.2$ grep -ow "*[^*]*\*" f1
*word3*
*word6*
*word7*
*word9*
sh-3.2$

I tried both - neither worked.

\<.*\> returns everything, as would be expected.

\<\*.*\> , however, doesn't return words that start with an asterisk. I think it's choking on asterisks for some reason. It doesn't make a difference if instead of escaping the asterisk, I put it in single or double quotes, like so:

\<'*'.*\>

\<"*".*\>

Can an asterisk not be legitimately part of a "word" - as defined by GREP? I figured a word was simply any string with spaces or tabs around it.
# 6  
Old 07-22-2009
what do you mean not working? why do you use "\<"*".*\>"?

^ - denotes first char of the string
$ - last char of the string

my sample above came from my bash shell.
# 7  
Old 07-22-2009
Quote:
Originally Posted by ryandegreat25
what do you mean not working?
Simply that - your sample code does not return any results.

Quote:
why do you use "\<"*".*\>"?

^ - denotes first char of the string
$ - last char of the string
InDesign seems to store paragraphs as strings - as such I want to find words within that string that begin and end with an asterisk. For that, I believe the proper syntax is \< and \>

Previous Thread | Next Thread
Test Your Knowledge in Computers #628
Difficulty: Easy
In PHP, True !== 1;
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

grep and regular expressions

Hi All, For the past many days I have solved a lot of grep and regular expression questions, Now I am in a search for a good quality set of questions that can help me build and check my knowledge of grep with regular expressions, it would be great if anyone could help me with my requirement. ... (1 Reply)
Discussion started by: rahulkalra9
1 Replies

2. Homework & Coursework Questions

Regular Expressions with GREP

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Given a text file (big_english.txt) containing roughly 250,000 words, answer the following using grep and... (2 Replies)
Discussion started by: blahblahblah123
2 Replies

3. UNIX for Dummies Questions & Answers

extract columns using grep or regular expressions

I am trying to print columns from a table whose name (header) matches a certain string. E.g., patient1001 patient1002 patient2005 patient3005 patient4001 0 0 0 0 0 2 9 2 8 3 2 7 3 0 2 Say I want to print columns whose names end with "01" patient1001 patient4001 0 0 2 3 2 2 ... (3 Replies)
Discussion started by: quextil
3 Replies

4. Homework & Coursework Questions

Grep expressions homework, need assistance

Please dont delete, im listing my assignment and will be editing as i work on it. I am NOT looking for answers but help in understanding how to use grep 1. The problem statement, all variables and given/known data: For each question, list the command lines used in addition to any other details... (3 Replies)
Discussion started by: alindner
3 Replies

5. Shell Programming and Scripting

need to replace asterisks

I need to replace occurrences of twelve asterisks "************" with the string " 0000000.00" . Note that there are two spaces in front of the first zero. How can I do this using awk or sed? (3 Replies)
Discussion started by: mustang_9333
3 Replies

6. Shell Programming and Scripting

How to grep using a line break in regular expressions?

Hi, I have a file as below, {#### if file then file else file } print file i need to fine the count of all the pattern - file, inside the { } i'm using a grep command as grep -c \{'*file*'\} fake.sh\ It doesn't gives me any result, i think the problem here is the... (5 Replies)
Discussion started by: divak
5 Replies

7. Shell Programming and Scripting

searching regular expressions with special characters like dot using grep

hi everybody I am a new user to this forum and its previous posts have been very useful. I'm searching in a file using grep for patterns like 12.13.444 55.44.443 i.e. of form <digit><digit>.<digit><digit>.<digit><digit><digit> Can anybody help me with this. Thanks in advance (4 Replies)
Discussion started by: jpriyank
4 Replies

8. UNIX for Dummies Questions & Answers

grep and regular expressions :

I wrote a simple korn shell where I am trying to filter all the good record layouts of a file to only leave the bad ones to look at. That file is hudge. Aside from '# comments' and 'var=ssss', all record should follow a specific record layout, with comma seperated fields. Some fields can have any... (2 Replies)
Discussion started by: Browser_ice
2 Replies

9. UNIX for Dummies Questions & Answers

More Grep - Regular Expressions

Hey all! I'm trying to search a file and return all instances of a word, let's say 'foo' in this case, as long as it's not a function name. For example: 1) int foo; //OK 2) //'this is totally fooed up' is also OK 3) int foo (int x, int y) //not ok to return I've tried a lot of regular... (7 Replies)
Discussion started by: Jombee
7 Replies

10. UNIX for Dummies Questions & Answers

grep where expressions reside in file

I have a program that displays many messages on stdout. I have another file that contain error messages only - Each line is a separate message. (msgs.txt) I am trying to show only the errors and not all the output. I tried this but got nothing: myprog | grep < msgs.txt I also tried... (2 Replies)
Discussion started by: GMMike
2 Replies

Featured Tech Videos