sed : regular expression (hungry mode ?) solved


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed : regular expression (hungry mode ?) solved
# 1  
Old 11-15-2010
sed : regular expression (hungry mode ?) solved

Hi
there is something I don't understand with the repeated element. With the following command I have a weird output:
Quote:
$ echo 3_aaaun+g | sed "s/.*\(aa*\).*/>\1</g"
>a<
Smilie
while I thought the output would be
Quote:
>aaa<
I really don't understand why 2 "a" are skipped. Might someone explain please?
# 2  
Old 11-15-2010
That is because of sed's greedy matching. The .* matches as much as possible so that \(aa*\) only has one a left (the rightmost a). This should remedy it:
Code:
sed "s/[^a]*\(aa*\).*/>\1</g"

or
Code:
sed "s/.*_\(aa*\).*/>\1</g"

This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 11-15-2010
The first .* is eating some of the a's. If you don't want it to eat a's, then give it [icode][^a]* instead of .*

And if you always want it to start at the beginning and end at the end of the string, you should pin it in those places too.

Code:
$  echo 3_aaaun+g | sed "s/^[^a]*\(aa*\).*$/>\1</g"
>aaa<
$

This User Gave Thanks to Corona688 For This Post:
# 4  
Old 11-15-2010
Thank you both of you Smilie so fast reply ^^ I like unix.com Smilie
Scrutinizer you soleved my trouble.

But just to know is there a way to remove the "greedy mode" ? ... in the man I found nothing :s
# 5  
Old 11-15-2010
There is no lazy matching in sed, but by using [^a] (any character but a) instead of . (any character) you can go a long way. Otherwise you would have to switch to perl:
Code:
perl -pe 's/.*?(a+).*/>\1</g'


Last edited by Scrutinizer; 11-15-2010 at 06:58 PM..
This User Gave Thanks to Scrutinizer For This Post:
# 6  
Old 11-16-2010
thank you again Scrutinizer Smilie
perl will get me sooner or later x)

Edit: Hmm I can't "close" my topic ? or put it in a solved mode? Smilie

Last edited by jim mcnamara; 11-16-2010 at 08:22 AM.. Reason: Not closed, marked 'solved'
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

I am learning regular expression in sed,Please help me understand the use curly bracket in sed,

I am learning SED and just following the shell scripting book, i have trouble understanding the grep and sed statement, Question : 1 __________ /opt/oracle/work/antony>cat teledir.txt jai sharma 25853670 chanchal singhvi 9831545629 anil aggarwal 9830263298 shyam saksena 23217847 lalit... (7 Replies)
Discussion started by: Antony Ankrose
7 Replies

2. Shell Programming and Scripting

[Solved] Regular expression for something or nothing

I have some data files of the format HH:mm MM-DD-YY. A frequent human error in the data is a technician entering YYYY instead of YY Reg Expression 1: --Reg Expression 2: --20How can I create one expression? does not seem to work. This RE does not give an error but does not match any... (5 Replies)
Discussion started by: Michael Stora
5 Replies

3. UNIX for Advanced & Expert Users

sed: -e expression #1, char 0: no previous regular expression

Hello All, I'm trying to extract the lines between two consecutive elements of an array from a file. My array looks like: problem_arr=(PRS111 PRS213 PRS234) j=0 while } ] do k=`expr $j + 1` sed -n "/${problem_arr}/,/${problem_arr}/p" problemid.txt ---some operation goes... (11 Replies)
Discussion started by: InduInduIndu
11 Replies

4. Shell Programming and Scripting

Help with sed regular expression

Hi all, I want to get a substring from a string based on given delimiter, for example: str="foo|bar|baz" with delimiter "|", I want to get one substring at each time with the order number the substring in the whole string, given 1 to get "foo", given 2 to get "bar", given 3 to get "baz", I... (2 Replies)
Discussion started by: Roy987
2 Replies

5. Programming

Perl: How to read from a file, do regular expression and then replace the found regular expression

Hi all, How am I read a file, find the match regular expression and overwrite to the same files. open DESTINATION_FILE, "<tmptravl.dat" or die "tmptravl.dat"; open NEW_DESTINATION_FILE, ">new_tmptravl.dat" or die "new_tmptravl.dat"; while (<DESTINATION_FILE>) { # print... (1 Reply)
Discussion started by: jessy83
1 Replies

6. UNIX for Dummies Questions & Answers

Regular Expression In Sed

Hi , I am learing sed echo abc 123 def 456 | sed 's|\(*\) \(*\)|\1|' is returning abc def 456 i was hoping abc def "\1" should only print the occurence of the first pattern but according to my understanding it is just removing the first occurence of the second pattern... (7 Replies)
Discussion started by: max_hammer
7 Replies

7. Shell Programming and Scripting

sed regular expression help

please consider this: echo "11111*X*005010X279~ST*270*1111111*005010X279~BHT*0011*11" | sed 's/.*\(005010X(\d)(\d)(\d)*\).*$/\1/'i'm searching for first occurrence of 005010X while leaving rest of characters out. :confused: any tips? thnx in advance guys. (7 Replies)
Discussion started by: grep01
7 Replies

8. Shell Programming and Scripting

Regular expression (sed)

Hi I need to get text that are within "" For example File: asdasd "test test2" sadasds asdda asdasd "demo demo2" Output: test test2 demo demo2 Any help is good Thank you (12 Replies)
Discussion started by: blito_loco
12 Replies

9. Shell Programming and Scripting

Regular expression with SED

Hi! I'm trying to write a regexp but I have no luck... I have a string like this: param1=sometext&param2=hello&param3=bye Also, the string can be simply: param2=hello I want to return the value of param2: "hello". How can I do this? Thanks. (3 Replies)
Discussion started by: GagleKas
3 Replies

10. Shell Programming and Scripting

Regular expression with sed

Hi, I'm trying following:echo "test line XA24433 test" | sed 's/.*X\(.*\)/X\1/' XA24433 test While I want the output as: XA24433 I want to grab the words starting with letter X till the next space, this word can be anywhere in the line. (9 Replies)
Discussion started by: nervous
9 Replies
Login or Register to Ask a Question