grep fixed string with regex


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting grep fixed string with regex
# 8  
Old 11-08-2010
Try:
Code:
echo "u_sA9e-u" | grep -E '([[:space:]]|^)[a-z_][a-z0-9_-]*[$]?([[:space:]]|$)'

This User Gave Thanks to Scrutinizer For This Post:
# 9  
Old 11-08-2010
Thank you!

Code:
$ echo "u_sA9e-u" | grep -oE '([[:space:]]|^)[a-z_][a-z0-9_-]*[$]?([[:space:]]|$)'
$ echo "u_s9e-u" | grep -oE '([[:space:]]|^)[a-z_][a-z0-9_-]*[$]?([[:space:]]|$)'
u_s9e-u

# 10  
Old 11-09-2010
Quote:
Originally Posted by teresaejunior
Thank you, bakunin! But it still greps the "A", or I'm doing something wrong...
Code:
$ echo " u_sA9e-u " | grep '[ ]*[^_a-z][-_a-z0-9]*\$*[ ]*'
 u_sA9e-u

And I tried a bunch of different commands again with no luck... Would you try it there
Sorry, my mistake. somehow a superfluous caret slipped by:

Code:
grep '[ ]*[^_a-z][-_a-z0-9]*\$*[ ]*'

Code:
grep '[ ]*[_a-z][-_a-z0-9]*\$*[ ]*'

The caret means a logical NOT in this case, so in fact the meaning was inverted. ("[ab]" finds all "a"s and "b"s, ""[^ab]" finds anything except these).

Of course Scrutinizers comment about words at the beginning or end of a line is correct. I didn't intend to give a complete solution, just a hint how to construct such regexps for yourself. See also below.

In the case of your echo statement you already made sure there is a leading and a trailing blank therefore you should remove the conditionals from the word delimiters in the regexp, which makes it work:

Code:
echo " u_sA9e-u " | grep '[ ][_a-z][-_a-z0-9][-_a-z0-9]*\$*[ ]'

Scrutinizers solution is correct, but makes use of the extended regular expression syntax of the GNU-grep. This may or may not be a problem in your case. Standard POSIX-greps won't understand the pipe symbol ("|") as logical OR. If this doesn't matter in your case you should go for this solution, as it is the most comprehensive one possible.

If you would need to port the solution to different Unixes you wouldn't want to rely on this extended regular expression syntax because some "grep"s only support the standard (POSIX) features. The solution would be in this case to grep for words on the end/beginning of a line separately. The difference would be to replace the anchoring leading/trailing spaces by beginning-of-line-("^")/end-of-line-("$")-symbols.

search in the middle of the line:
Code:
grep '[ ][_a-z][-_a-z0-9][-_a-z0-9]*\$*[ ]'

search the beginning of the line:
Code:
grep '^[_a-z][-_a-z0-9][-_a-z0-9]*\$*[ ]'

search the end of the line:
Code:
grep '[ ][_a-z][-_a-z0-9][-_a-z0-9]*\$*$'

I hope this helps.

bakunin
# 11  
Old 11-09-2010
Quote:
Originally Posted by bakunin
Scrutinizers solution is correct, but makes use of the extended regular expression syntax of the GNU-grep. This may or may not be a problem in your case. Standard POSIX-greps won't understand the pipe symbol ("|") as logical OR. If this doesn't matter in your case you should go for this solution, as it is the most comprehensive one possible.
Hi Bakunin,

The OP stated she was using GNU grep at the beginning. However please note that I suggested a general egrep solution. The Posix specification states that egrep is deprecated and that grep -E should be used instead (see grep: rationale). As such grep -E is using ERE's and certainly understands alternation, so IMO it should work with any Posix-compliant grep.

S.
# 12  
Old 11-09-2010
Quote:
Originally Posted by Scrutinizer
However please note that I suggested a general egrep solution. The Posix specification states that egrep is deprecated and that grep -E should be used instead
I have overlooked the O/Ps requirement, but saw it now, as you mentioned it. The information about EREs in POSIX was news to me, thanks for the info. I stand corrected.

bakunin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grep multiple words in a file with help of fixed string switch

I have multiple strings in a file which have special character $, when i search strings by ignoring $ with \ using single quotes it returns empty results. My search strings are set char_1($lock) and set new_char_clear_3($unlock) I tried searching with but it returns empty results.However... (3 Replies)
Discussion started by: g_eashwar
3 Replies

2. Shell Programming and Scripting

Grep string with regex numeric characters

Hi all, I have the following entries in a file: Cause Indicators=80 90 Cause Indicators=80 90 Cause Indicators=82 90 Cause Indicators=82 90 Cause Indicators=82 90 The first 2 digits might change so I am after a sort of grep which could find any first 2 digits + the second 2,... (3 Replies)
Discussion started by: nms
3 Replies

3. Shell Programming and Scripting

Grep with regex containing one string but not the other

Hi to you all, I'm just struggling with a regex problem and I'm pretty sure that I'm missing sth obvious... :confused: I need a regex to feed my grep in order to find lines that contain one string but not the other. Here's the data example: 2015-04-08 19:04:55,926|xxxxxxxxxx| ... (11 Replies)
Discussion started by: stresing
11 Replies

4. Shell Programming and Scripting

grep regex, match exact string which includes "/" anywhere on line.

I have a file that contains the 2 following lines (from /proc/mounts) /dev/sdc1 /mnt/backup2 xfs rw,relatime,attr2,noquota 0 0 /dev/sdb1 /mnt/backup xfs rw,relatime,attr2,noquota 0 0 I need to match the string in the second column exactly so that only one result is returned, e.g. > grep... (2 Replies)
Discussion started by: jelloir
2 Replies

5. Shell Programming and Scripting

Getting a string without fixed delimiters

I have a line of text for example aaaa bbbb cccc dddd eeee ffffff I would need to get the cccc however bbbb could be there or not. So whether bbbb is in the line or not I need cccc. I was looking at either awk or sed....and trying to start at c and end until the next space. Also... (11 Replies)
Discussion started by: bombcan1
11 Replies

6. UNIX for Dummies Questions & Answers

Using grep to check for character at fixed position

i have a file (test.txt) that contains: 20799510617900000928000000005403020110315V 20799510617900000928000000005403020110316 20799510617900000928000000005403020110317 20799510617900000928000000005403020110318V grep V test.txt > /tmp/void.log if then mail -s "void" < test.txt fi... (2 Replies)
Discussion started by: tjmannonline
2 Replies

7. Shell Programming and Scripting

filtering out duplicate substrings, regex string from a string

My input contains a single word lines. From each line data.txt prjtestBlaBlatestBlaBla prjthisBlaBlathisBlaBla prjthatBlaBladpthatBlaBla prjgoodBlaBladpgoodBlaBla prjgood1BlaBla123dpgood1BlaBla123 Desired output --> data_out.txt prjtestBlaBla prjthisBlaBla... (8 Replies)
Discussion started by: kchinnam
8 Replies

8. UNIX for Dummies Questions & Answers

Regex to match when input is not a certain string (can't use grep -v)

Hey everyone, Basically, all I'm looking for is a way to regex for not a certain string. The regex I'm looking to avoid matching is: D222 i.e. an equivalent of: awk '!/D222/' The problem is that I use this in the following command in a Bash script: ls ${source_directory} | awk... (1 Reply)
Discussion started by: kdelok
1 Replies

9. UNIX for Dummies Questions & Answers

| help | unix | grep (GNU grep) 2.5.1 | advanced regex syntax

Hello, I'm working on unix with grep (GNU grep) 2.5.1. I'm going through some of the newer regex syntax using Regular Expression Reference - Advanced Syntax a guide. ls -aLl /bin | grep "\(x\)" Which works, just highlights 'x' where ever, when ever. I'm trying to to get (?:) to work but... (4 Replies)
Discussion started by: MykC
4 Replies

10. Shell Programming and Scripting

sed, grep, awk, regex -- extracting a matched substring from a file/string

Ok, I'm stumped and can't seem to find relevant info. (I'm not even sure, I might have asked something similar before.): I'm trying to use shell scripting/UNIX commands to extract URLs from a fairly large web page, with a view to ultimately wrapping this in PHP with exec() and including the... (2 Replies)
Discussion started by: ropers
2 Replies
Login or Register to Ask a Question