gnu sed regex grouping not working?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting gnu sed regex grouping not working?
# 1  
Old 01-20-2009
gnu sed regex grouping not working?

Hello,

from the gnu sed manual, I should be able to do this:

`\(REGEXP\)'
Groups the inner REGEXP as a whole, this is used to:

* Apply postfix operators, like `\(abcd\)*': this will search
for zero or more whole sequences of `abcd', while `abcd*'
would search for `abc' followed by zero or more occurrences
of `d'. Note that support for `\(abcd\)*' is required by
POSIX 1003.1-2001, but many non-GNU implementations do not
support it and hence it is not universally portable.

the version of sed I am using is gnu sed v 4.1.5

The feature mentioned above does not seem to be working; eg,

echo "abc-abc" | sed -r 's/[\(abc\)]/%/g'

gives:

%%%-%%%

If it were treating abc as grouped, I should get:

%-%

I have tried to get this feature to work on several different systems, and never have. I have done a fair amount of googling on this with no answers.

Ultimately what I would like to do is remove text between two strings, eg, <script and </script>, without being greedy. So I would like to be able to do something like:

sed 's/<script[^\(<\/script>\)]*<\/script>//g'

in order to only remove text between a <script string and the next </script> string without removing all the text clear to the last </script> string on the line.

An input on why this does not seem to be working for me would be greatly appreciated.

Allasso
# 2  
Old 01-20-2009

Inside [...] is a list of characters, and it matches a single character.

Code:
echo "abc-abc" | sed -r 's/abc/%/g'

# 3  
Old 01-20-2009
thank you for your reply.

Your example works fine for what it does, but the whole point is to be able to use the square brackets so I can use the NOT form -- [^...] -- in order to keep the match from being greedy.

Are you saying that you cannot have a group inside of [...] ?

Last edited by Allasso; 01-20-2009 at 09:11 PM.. Reason: clarity
# 4  
Old 01-20-2009
Quote:
Originally Posted by Allasso
Are you saying that you cannot have a group inside of [...] ?

Exactly. Square brackets match a single character not a string or group.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using GNU Regex

I'm just learning Regex and while testing my understanding I received some unexpected results. I created example.txt with the text "abcddd". Running the command grep --color 'd' example.txt I received the results: "abcddd" with the first and second letter d highlighted in red. So... (1 Reply)
Discussion started by: rthomas529
1 Replies

2. Shell Programming and Scripting

Regex not working

I am using a regex to exactly match a string abcdef as ^abcdef$. But it does'nt seem to work :( (11 Replies)
Discussion started by: gaurav99
11 Replies

3. UNIX for Dummies Questions & Answers

Gsub regex not working

I have a number of files that I pass through awk/gsub. I believe to have found a working regex and on 'test bed' sites it matches, however within gsub it does not. Examples: Initial data: /Volumes/Daniel/Public/Drop Box/_Hellsing_Ultimate_OVA_-_10_.mkv gsub & regex: gsub("\]+\]","" ... (4 Replies)
Discussion started by: unknownn
4 Replies

4. Shell Programming and Scripting

grouping using sed or awk

I have below inside a file. 11.22.33.44 user1 11.22.33.55 user2 I need this manipulated as alias server1.domain.com='ssh user1@11.22.33.44' alias server2.domain.com='ssh user2@11.22.33.55' (3 Replies)
Discussion started by: anil510
3 Replies

5. Shell Programming and Scripting

Grouping sed commands

Hello, would you please help me with why my SED command file is outputting the entire input file instead of only the text that I'm trying to block? cat testfile O 111111111-00 DUE-DATE METHOD: FREQUENCY: O 222222222-00 DUE-DATE METHOD: FREQUENCY: O 333333333-02 DUE-DATE METHOD:... (4 Replies)
Discussion started by: lneedh1
4 Replies

6. Shell Programming and Scripting

matching a regex using egrep not working

Hi, I'm trying to validate if a string matches a regular expression, but it is not working. Am I missing something? Do I need to scape any of the characters? if echo 'en-GB' | egrep '({1,8})(-{1,8})*' >/dev/null; then echo Valid value fi Thanks in advance (6 Replies)
Discussion started by: skrtxao
6 Replies

7. UNIX for Dummies Questions & Answers

Trouble with grouping regex

Hi Forum im trying to use grouping in a regex statement in a function in a script this is the criteria im trying to match :It MUST have 3 character at the beginning. After that it can have a mix of spaces,alpha-numeric and dashes in any order eg HUG this-stuff, FGU taylor-8-shoes, ZDFnintendo... (2 Replies)
Discussion started by: ShinTec
2 Replies

8. Shell Programming and Scripting

Converting perl regex to sed regex

I am having trouble parsing rpm filenames in a shell script.. I found a snippet of perl code that will perform the task but I really don't have time to rewrite the entire script in perl. I cannot for the life of me convert this code into something sed-friendly: if ($rpm =~ /(*)-(*)-(*)\.(.*)/)... (1 Reply)
Discussion started by: suntzu
1 Replies

9. Shell Programming and Scripting

SED:: Using variable while grouping

Hi, I have the following script :- #!/bin/csh -f set var="HOST2" sed -e 's/\(.*TRANSFER TO\).*\(usr\)/\1 "$var" \2/' tempFile tempFile contains: STOP TRANSFER TO HOST1 /usr/bin/myscript 1. How to use variable in the above sed command. It replaces with $var... (6 Replies)
Discussion started by: angshuman_ag
6 Replies

10. Shell Programming and Scripting

Grouping using sed/awk ?

I run awk cat $1|awk '{print $6}' and get a lot of results and I want results to group them. For example my result is (o/p is unknown to user) xyz xyz abc pqr xyz pqr etc I wanna group them as xyz=total found 7 abc=total .... pqr= Thank (3 Replies)
Discussion started by: pujansrt
3 Replies
Login or Register to Ask a Question