Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have the following text in the file sentence.txt

Linux refers to the family of Unix-like computer operating systems that use the Linux kernel.
Linux can be installed on a wide variety of computer hardware, ranging from mobile phones, tablet computers and video game consoles, to mainframes and supercomputers. Linux is predominantly known for its use in servers.
It has a server market share ranging between 20–40%.
Most desktop computers run either Microsoft Windows or Mac OS X, with Linux having anywhere from a low of an estimated 1–2% of the desktop market to a high of an estimated 4. 8%.
However, desktop use of Linux has become increasingly popular in recent years, partly owing to the popular Ubuntu, Fedora, Mint, and openSUSE distributions and the emergence of netbooks and smart phones running an embedded Linux.
Linux can be installed on a wide variety of computer hardware, ranging from mobile phones, tablet computers and video game consoles, to mainframes and supercomputers.

In this text 2 sentences repeat. It is the last sentence. Actually I just copy-pasted the 2nd sentence to the end of the text so that I have 2 occurrences of the mobile phones

~$ grep mobile sentence.txt   
Linux can be installed on a wide variety of computer hardware, ranging from mobile   phones, tablet computers and video game consoles, to mainframes and supercomputers.  
Linux can be installed on a wide variety of computer hardware, ranging from mobile   phones, tablet computers and video game consoles, to mainframes and supercomputers.  

Then I was wondering which occurrence would be deleted by the following regex:
[^.]*mobile phones[^.]*\.
I believe that the last occurrence should be removed due to the back tracking. To verify this assumption and my understanding I applied the following:

:~$ sed  's/[^.]*mobile phones[^.]*\.//' sentence.txt > sentence2.txt    

Notice that I did not use g so that it would replace the first occurrence found (I think it would be the last sentence).
But I see that both are removed!

~$ grep mobile sentence2.txt    
~$  

What am I doing wrong here? 1) Is my understanding of the regex that the last sentence would match first wrong? 2) Why are both sentences removed without g?

share|improve this question
1  
First let's make sure that sentence2.txt contains anything at all. Does it? –  Marki Feb 9 at 22:57
    
@Marki:Yes it does contain the rest –  Jim Feb 10 at 9:04

1 Answer 1

up vote 2 down vote accepted

Your understanding would be correct if the regex were applied to the file as a whole. That's not how sed rolls: it operates line by line instead. Thus the g modifier would only come into play if your regex could match multiple times on the same line. In your case, the substitution was applied only once to each line so naturally both instances were removed.

Slurp the whole file instead and see the difference:

perl -p0777e 's/[^.]*mobile phones[^.]*\.//' sentence.txt > sentence2.txt
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.