Understanding sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Understanding sed
# 8  
Old 03-02-2013
Discrepancies like these are why every now and then a space probe crashes into the surface of a planet instead of safely entering orbit.

Thank you for living up to your nick, Scrutinizer. Your observations make us all better.

From POSIX: 9.3.6 BREs Matching Multiple Characters:
Quote:
A subexpression repeated by an <asterisk> ( '*' ) or an interval expression shall not match a null expression unless this is the only match for the repetition or it is necessary to satisfy the exact or minimum number of occurrences for the interval expression.
GNU sed 4.1.5:
Code:
$ echo :BEFORE: | sed 's/[^:]*/AFTER/'
AFTER:BEFORE

I expect :AFTER:. It's behaving as if the match is anchored.

Do other implementations (including newer GNU sed) agree in this as well? I don't have access at the moment to my *BSD stuff, only a windows machine and an old linux laptop.

Regards,
Alister
# 9  
Old 03-03-2013
Code:
FreeBSD 9.0-RELEASE
$ echo :BEFORE: | sed 's/[^:]*/AFTER/'
AFTER:BEFORE:

Code:
Linux 3.5.0-26-generic
GNU sed version 4.2.1
$ echo :BEFORE: | sed 's/[^:]*/AFTER/'
AFTER:BEFORE:

This User Gave Thanks to RudiC For This Post:
# 10  
Old 03-03-2013
@alister:
There seems to be no difference with a single (first) substitution or a global substitution, but only with the flag with a specific number. The difference is about a zero match after a previous match (was it already covered by a previous match or not?). Compare:

Code:
$ echo :BEFORE: | gsed 's/[^:]*/AFTER/'
AFTER:BEFORE:
$ echo :BEFORE: | sed 's/[^:]*/AFTER/'
AFTER:BEFORE:

$ echo :BEFORE: | gsed 's/[^:]*/AFTER/2'
:AFTER:
$ echo :BEFORE: | sed 's/[^:]*/AFTER/2'
:AFTER:

$ echo :BEFORE: | gsed 's/[^:]*/AFTER/3'
:BEFORE:AFTER
$ echo :BEFORE: | sed 's/[^:]*/AFTER/3'
:BEFOREAFTER:

$ echo :BEFORE: | gsed 's/[^:]*/AFTER/4'
:BEFORE:
$ echo :BEFORE: | sed 's/[^:]*/AFTER/4'
:BEFORE:AFTER

$ echo :BEFORE: | gsed 's/[^:]*/AFTER/5'
:BEFORE:
$ echo :BEFORE: | sed 's/[^:]*/AFTER/5'
:BEFORE:

$ echo :BEFORE: | gsed 's/[^:]*/AFTER/g'
AFTER:AFTER:AFTER
$ echo :BEFORE: | sed 's/[^:]*/AFTER/g'
AFTER:AFTER:AFTER

$ echo x:BEFORE: | gsed 's/[^:]*/AFTER/2'
x:AFTER:
$ echo x:BEFORE: | sed 's/[^:]*/AFTER/2'
xAFTER:BEFORE:


Last edited by Scrutinizer; 03-03-2013 at 02:02 PM..
This User Gave Thanks to Scrutinizer For This Post:
# 11  
Old 03-03-2013
That sed implementation isn't self-consistent. Using numeric s-command flags, there are 4 matches:
Quote:
Originally Posted by Scrutinizer
Code:
$ echo :BEFORE: | sed 's/[^:]*/AFTER/'
AFTER:BEFORE:

$ echo :BEFORE: | sed 's/[^:]*/AFTER/2'
:AFTER:

$ echo :BEFORE: | sed 's/[^:]*/AFTER/3'
:BEFOREAFTER:

$ echo :BEFORE: | sed 's/[^:]*/AFTER/4'
:BEFORE:AFTER

Using the s-command's g flag, there are only 3:
Quote:
Originally Posted by Scrutinizer
Code:
$ echo :BEFORE: | sed 's/[^:]*/AFTER/g'
AFTER:AFTER:AFTER

Regards,
Alister
# 12  
Old 03-03-2013
I agree this seems to be inconsistent, but it appears to be the case in several sed implementations...
# 13  
Old 03-03-2013
So then we are all in agreement: every sed implementation sucks. This thread can now be closed. Smilie

Regards,
Alister
# 14  
Old 03-06-2013
The correct output (GNU sed) is:
Code:
echo :BEFORE: | sed 's/[^:]*/AFTER/3'
:BEFORE:AFTER

How could the output possibly be:
Code:
:BEFOREAFTER:

What's the possible logic? What sed version are you using?

All the other outputs make sense. It's only the one with n=3 that seems off.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need Quick help on Understanding sed Regex

Hi Guys, Could you please kindly explain what exactly the below SED command will do ? I am quite confused and i assumed that, sed 's/*$/ /' 1. It will remove tab and extra spaces .. with single space. The issue is if it is removing tab then it should be Î right .. please assist.... (3 Replies)
Discussion started by: Nandy
3 Replies

2. Shell Programming and Scripting

Need your help in understanding this

Hi, I found this in a script and I would like to know how this works Code is here: # var1=PART1_PART2 # var2=${var1##*_} # echo $var2 PART2 I'm wondering how ##* makes the Shell to understand to pick up the last value from the given. (2 Replies)
Discussion started by: sathyaonnuix
2 Replies

3. Shell Programming and Scripting

help understanding regex with grep & sed

I have the following line of code that works wonders. I just don't completely understand it as I am just starting to learn regex. Can you help me understand exactly what is happening here? find . -type f | grep -v '^\.$' | sed 's!\.\/!!' (4 Replies)
Discussion started by: trogdortheburni
4 Replies

4. UNIX for Dummies Questions & Answers

understanding sed command

Hi Friends, I need a small help in understanding the below sed command. $ cat t4.txt 1 root 1 58 0 888K 368K sleep 4:06 0.00% init 1 root 1 58 0 888K 368K sleep 4:06 0.00% init last $ sed 's/*$//' t4.txt 1 root 1 58 0 888K ... (3 Replies)
Discussion started by: forroughuse
3 Replies

5. UNIX for Dummies Questions & Answers

understanding {%/*}/

Hi Gurus: I am trying to understand the following line of code.I did enough of googling to understand but no luck.Please help me understand the follow chunk of code: X=$0 MOD=${X%/*}/env.ksh X is the current script from which I am trying to execute. Say if X=test.ksh $MOD is echoing :... (3 Replies)
Discussion started by: vemana
3 Replies

6. Shell Programming and Scripting

need help understanding mv

I just started shell coding and I'm a bit confused on how 'mv' works can someone explain to me how it works and if i did this correctly. Thanks. echo "Enter Name of the first file:" read file1 #echo $file1 if ; then echo "Sorry, file does not exist." exit 1 ... (16 Replies)
Discussion started by: taiL
16 Replies

7. UNIX for Dummies Questions & Answers

Help understanding sed

I am trying to create a basic script that converts an Oracle script into a Sybase script. The only things im changing are Datatypes and the to_char and to_date functions. I am not really 100% sure of the way it works. I have tried running the functions through a loop to replace each word line... (6 Replies)
Discussion started by: Makaer
6 Replies

8. Shell Programming and Scripting

understanding the sed command

Guys, I am trying to understand the sed command here. adx001 $ a=/clocal/dctrdata/user/dctrdat1/trdroot/recouncil adx001 $ b=`echo $a | sed 's/\//\\\\\//g'` adx001 $ echo $b \/clocal\/dctrdata\/user\/dctrdat1\/trdroot\/recouncil The sed command i took it from the script. Please... (3 Replies)
Discussion started by: mac4rfree
3 Replies

9. UNIX for Advanced & Expert Users

need further understanding of init.d

I needt o know how what init.d does and how it knows which dameons/applications to turn off and how to restart the applications after reboot. any OS - solaris/hp-ux (1 Reply)
Discussion started by: jigarlakhani
1 Replies
Login or Register to Ask a Question