Grep with Regex multiple characters


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep with Regex multiple characters
# 1  
Old 04-15-2016
Grep with Regex multiple characters

Hi everyone,

So I'm a bit confused about a regex pattern that should exist, but I can't really find any way to do it...

Let's say I want to match any lines that have a specific string, but I don't know the order of the letters but I know the length. Let's say it's 10 characters and begins with a V

Do I seriously have to do something like:

Code:
egrep '^N [a-z][a-z][a-z][a-z][a-z][a-z][a-z][a-z][a-z]'

It could get worse if I knew there was a number in there somewhere... Is there no way to shorten this? I know there is a quantifier that I can add, something like

\{x,y\} but I've used it and it doesn't seem to work with letters?

Is there anyway to sum up saying that you're looking for 9 letters each could be a-z?

Last edited by Scrutinizer; 04-16-2016 at 12:52 AM.. Reason: code tags
# 2  
Old 04-15-2016
try:
Code:
grep "^V[a-zA-Z]\{9\}\b" infile

# 3  
Old 04-15-2016
Thanks for the quick response on a friday! Okay..so the syntax you provided sort of worked. It did return the specified value of 9...but it also returned any line that had more than 9. It basically used 9 as the minimum but it had no limit.

Also... I've been so used to using egrep...why on earth can't egrep do this, but regular grep can??
# 4  
Old 04-16-2016
Whether your grep supports \b depends on your implementation. GNU grep can, but regular grep probably not.
  • \b will only work here if the character that follows is a non-word character (so if it is number or an underscore then \b will not work)..
  • Also [a-zA-Z] is unreliable in some locale and it does not work for diacritical characters, so it is better to use the Posix [[:alpha:]] character class instead...
  • egrep (or grep -E as is preferred nowadays) can also use iteration but you need to leave out the escapes (\) before the curly braces

So combining these remarks, try:
Code:
grep -E '^V[[:alpha:]]{9}([^[:alpha:]]|$)' file


--
grep -E is the same as grep for a POSIX compliant grep.

Last edited by Scrutinizer; 04-16-2016 at 01:31 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Regex to identify illegal characters in a perso-arabic database

I am working on Sindhi: a perso-Arabic script and since it shares the Unicode-block with over 400 other languages, quite often the database contains characters which are not wanted: illegal characters. I have identified the character set of Sindhi which is given below: For clarity's sake, each... (8 Replies)
Discussion started by: gimley
8 Replies

2. UNIX for Beginners Questions & Answers

Grep regex

Hi everyone, I'm looking for a grep command to match the following pattern from a file: <EGS>10234567<EGS> I used this following command to do this: grep -E '^<EGS>{8}<EGS>' test.txt In output I got: <EGS>10234567<EGS> Till now it work, but if I add something at the end of the line... (2 Replies)
Discussion started by: Arnaudh78
2 Replies

3. UNIX for Beginners Questions & Answers

Grep in regex

Hello guys, Here i am writing a script in bash to check for a valid URL from a file using regex This is my input file http://www.yahoo.commmmmm http://www.google.com https://www.gooogle.co www.test6.co.in www.gmail.com www.google.co htt://www.money.com http://eeeess.google.com... (2 Replies)
Discussion started by: Meeran Rizvi
2 Replies

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

5. Shell Programming and Scripting

grep -v and regex

How to match lines that don't contain a patern in regex it self, without using the -v option of grep? (15 Replies)
Discussion started by: vistastar
15 Replies

6. Shell Programming and Scripting

print last five characters with PERL regex

greetings citizens of Unix.com I am perplexed with an issue. The issue is trying to print the last 5 characters of a string in PERL. Below are demonstrated my daft attempts at performing the forementioned task. $row =~ m/^.*(.....)\s$/; $row =~ m/\w{5}\s*$/i;$row =~... (3 Replies)
Discussion started by: simply seth
3 Replies

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

8. Shell Programming and Scripting

regex and grep

I want it to find lines that contain any number of capital letters before P this is what I have tried echo "AAAAAP" | grep 'P' echo "AAAAAP" | grep '\{1\}P' echo "AAAAAP" | grep '^*P' But none of them seem to work, any help is much appreciated thanks Calypso (4 Replies)
Discussion started by: Calypso
4 Replies

9. UNIX for Dummies Questions & Answers

grep with Regex help!

Hello everybody, I'd like to know how is it I should write a regex in unix to match a string not followed by another string (anywhere in the line). To be more specific, I want to find lines where "drop table" is found, but not followed anywhere in the line by the character "&". For... (3 Replies)
Discussion started by: mvalonso
3 Replies

10. UNIX for Dummies Questions & Answers

use of regex on grep

having a look on the regex site I saw that characters can be search using hex values http://www.regular-expressions.info/characters.html So I try to use it whith grep to find a è on a string (octal Decimal Hexa : 350 232 E8) but it doesn't work E.g. /usr/bin/echo '\0350' | egrep '\xE8' ... (0 Replies)
Discussion started by: solea
0 Replies
Login or Register to Ask a Question