SED: Pattern repitition regex matching


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting SED: Pattern repitition regex matching
# 1  
Old 08-10-2012
[SOLVED] SED: Pattern repitition regex matching

Fairly straightforward, but I'm having an awful time getting what I thought was a simple regex to work. I'll give the command I was playing with, and I'm aware why this one doesn't work (the 1,3 is off the A-Z, not the whole expression), I just don't know what the fix is:

Actual Output(s):
Code:
$ echo "This is a test 0A2B9C 0A 3B4C" | sed 's/[0-9][A-Z]\{1,3\}/X/g'
This is a test XXX X XX
$ echo "This is a test 0A2B9C 0A 3B4C" | sed 's/[0-9][A-Z]\{1,3\}/X/' 
This is a test X2B9C 0A 3B4C

Desired Output:
Code:
This is a test X X X

Thanks, just banging my head on this one.

Last edited by Vryali; 08-10-2012 at 02:57 PM..
# 2  
Old 08-10-2012
It's considering [0-9] and [A-Z] as separate ranges. So what you're asking for is actually a number, followed by one to three letters.

Try [0-9A-Z]
# 3  
Old 08-10-2012
I think he's looking for 1, 2, or 3 occurrence of a digit followed by an uppercase letter:
Code:
echo "This is a test 0A2B9C 0A 3B4C" | sed 's/\([0-9][A-Z]\)\{1,3\}/X/g'

This User Gave Thanks to Don Cragun For This Post:
# 4  
Old 08-10-2012
Quote:
Originally Posted by Don Cragun
I think he's looking for 1, 2, or 3 occurrence of a digit followed by an uppercase letter:
Code:
echo "This is a test 0A2B9C 0A 3B4C" | sed 's/\([0-9][A-Z]\)\{1,3\}/X/g'

Thanks so much, that's what I was looking for.

Edit: It works on Linux & AIX (Pretty sure it's running off GNU SED), doesn't work on Solaris by default. Any tips on Solaris, or alternate syntax? If it's not possible without GNU SED that's fine, just checking up.

Code:
# uname -s
SunOS
# echo "This is a test 0A2B9C 0A 3B4C" | sed 's/\([0-9][A-Z]\)\{1,3\}/X/g'
This is a test 0A2B9C 0A 3B4C

vs. Linux
Code:
$ uname -s
Linux
$ echo "This is a test 0A2B9C 0A 3B4C" | sed 's/\([0-9][A-Z]\)\{1,3\}/X/g'
This is a test X X X

vs. AIX:
Code:
# uname -s
AIX
# rpm -qa|grep sed
sed-4.1.1-1
# echo "This is a test 0A2B9C 0A 3B4C" | sed 's/\([0-9][A-Z]\)\{1,3\}/X/g'     
This is a test X X X

# 5  
Old 08-10-2012
I no longer have ready access to a Solaris system, but:
Code:
sed 's/\([0-9][A-Z]\)\{1,3\}/X/g'

is using standard Basic Regular Expression notation. If /usr/bin/sed doesn't work, you might try /usr/xpg4/bin/sed, but I don't see anything in Oracle's Solaris 11 sed(1) man page indicating that this substitute command shouldn't work in both versions of sed.
This User Gave Thanks to Don Cragun For This Post:
# 6  
Old 08-10-2012
Quote:
Originally Posted by Don Cragun
If /usr/bin/sed doesn't work, you might try /usr/xpg4/bin/sed, but I don't see anything in Oracle's Solaris 11 sed(1) man page indicating that this substitute command shouldn't work in both versions of sed.
Definitely doesn't work off-the-bat with 10, at least - however I didn't know about the xpg4 binaries and that version of sed -did- work, so I'm good to go. Thanks a lot!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Use sed to get first matching pattern

HI, I have a file: listenerport: - 1521 - 10520 - 10521 - 10522 - 10523 instances: listenerport: listenerport: - 1521 - 10540 - 10541 - 10542 - 10543 instances: ... (5 Replies)
Discussion started by: netbanker
5 Replies

2. Shell Programming and Scripting

PHP - Regex for matching string containing pattern but without pattern itself

The sample file: dept1: user1,user2,user3 dept2: user4,user5,user6 dept3: user7,user8,user9 I want to match by '/^dept2.*/' but don't want to have substring 'dept2:' in output. How to compose such regex? (8 Replies)
Discussion started by: urello
8 Replies

3. Shell Programming and Scripting

Sed: printing lines AFTER pattern matching EXCLUDING the line containing the pattern

'Hi I'm using the following code to extract the lines(and redirect them to a txt file) after the pattern match. But the output is inclusive of the line with pattern match. Which option is to be used to exclude the line containing the pattern? sed -n '/Conn.*User/,$p' > consumers.txt (11 Replies)
Discussion started by: essem
11 Replies

4. UNIX for Dummies Questions & Answers

blank space in regex pattern using sed

why does sed 's/.* //' show the last word in a line and sed 's/ .*//' show the first word in a line? How is that blank space before or after the ".*" being interpreted in the regex? i would think the first example would delete the first word and the next example would delete the second... (1 Reply)
Discussion started by: glev2005
1 Replies

5. Shell Programming and Scripting

sed to awk (regex pattern) how?

Hello, I am trying to covert a for statement into a single awk script and I've got everything but one part. I also need to execute an external script when "not found", how can I do that ? for TXT in `find debugme -name "*.txt"` ;do FPATH=`echo $TXT | sed 's/\(.*\)\/\(.*\)/\1/'` how... (7 Replies)
Discussion started by: TehOne
7 Replies

6. Shell Programming and Scripting

sed - print only matching regex

Hi folks, Lets say I have the following text file: name, lastname, 1234, name.lastname@test.com name1, lastname1, name2.lastname2@test.com, 2345 name, 3456, lastname, name3.lastname3@test.com 4567, name, lastname, name4.lastname4@test.com I now need the following output: 1234... (5 Replies)
Discussion started by: domi55
5 Replies

7. Shell Programming and Scripting

sed - matching pattern one but not pattern two

All, I have the following file: -------------------------------------- # # /etc/pam.d/common-password - password-related modules common to all services # # This file is included from other service-specific PAM config files, # and should contain a list of modules that define the services... (2 Replies)
Discussion started by: RobertBerrie
2 Replies

8. Shell Programming and Scripting

sed pattern matching

Hi, I have a file which contains a word like ravi and ravi30. i want to replace only the word ravi with xxx for that i am using the below sed command sed -e 's/ravi/xxx/g' . but the above command out put is xxx and xxx30 but i dont need to change ravi30 please guide me how to proceed.... (4 Replies)
Discussion started by: ravi_rn
4 Replies

9. Shell Programming and Scripting

Regex/sed - matching any char,space,underscore between : and /

trying to remove the portion in red: Data: mds_ar/bin/uedw92wp.ksh: $AI_SQL/wkly.sql mds_ar/bin/uedw92wp.ksh: $EDW_TMP/wkly.sql output to be: mds_ar/bin/uedw92wp.ksh: wkly.sql mds_ar/bin/uedw92wp.ksh: wkly.sql SED i'm trying to use: sed 's/:+\//: /g' input_file.dat >... (11 Replies)
Discussion started by: danmauer
11 Replies

10. Shell Programming and Scripting

Pattern matching sed

MSG="THERE WERE XX RECORDS IN ERROR TABLE,AAAA, WHEN LOADING THE BBBB TABLE WITH EXTRACT FROM CCCC INTO TABLES FOR , DATABASE DDDD." echo "$MSG" > /tmp/mplanmsg.$$.out I wan to replace XX with the content in $recordXX cat /tmp/mplanmsg.$$.out|sed 's/XX/\$recordXX/g'| sed... (3 Replies)
Discussion started by: leemjesse
3 Replies
Login or Register to Ask a Question