1

I am learning few simple sed commands.

Command #1

sed 's/\-.*//gw without' tgs.txt

Command #2

sed '/\-/s/\-.*//gw with' tgs.txt

Output #1

$ cat with
2. Databases 
6. Windows
 Raja

Output #2

$ cat without 
2. Databases 
6. Windows
 Raja

Difference Between them

$ diff with without 
$

What's the difference between these two commands?

3

There is no difference between these in effect:

sed 's/\-.*//g'
sed '/\-/s/\-.*//g'

The first form acts on all lines, the second form acts only on those lines which match /-/ using addresses. Since the action taken includes -, in effect both lines will only affect those which contain -.

Now if you'd used /Raja/ as an address instead, you'd only have seen the last line in with - that is, only those lines which contained Raja, and had the substitution performed.

  • I mentioned Globally , so It have to perform on all the lines with - matching only. If no match then It wont do anything right. I am more curious about special meaning of second command '/\-/' – rɑːdʒɑ Jan 29 '15 at 6:06
  • @Raja in the second case, yes, what you said is correct. But the g acts on the s, not on the address, so it is not affecting which lines are edited. The meaning of that initial /-/ is "act on only those lines which match -". – muru Jan 29 '15 at 6:08
  • @muru, you are correct. But still a question from a beginner in sed. Why sed 's/-.*//g' and sed 's/\-.*//g' produce the same result. – g_p Jan 29 '15 at 6:15
  • @g_p As far as I know - is not a special character in BRE. The standard says using \- when - is not special results in undefined behaviour, but most programs treat them as the same. Special characters like ^.$*, on the other hand, are a different case. – muru Jan 29 '15 at 6:18
  • I have executed few combination on an online shell emulator(I am sitting in front of a window machine with lots of restriction) and its behaviour is confusing me. I will try it later. Thanks for explaining it. – g_p Jan 29 '15 at 6:31
3

These two sed commands,

sed 's/\-.*//g'
sed '/\-/s/\-.*//g'

will do the same job. Let me explain how..

sed 's/\-.*//g'

will replace all the characters from - to the last character with an empty string. If a line don't have any - character then the replacement won't occur. That is, it won't affect the lines which don't have the hyphen - symbol.

sed '/\-/s/\-.*//g'

will first search for the lines which have the - symbol. If it finds any, then it will do the replacement (replaces all the characters from - to the last character with an empty string) on those lines.

So both are the same. Your first sed command would be enough. You don't need to put the search condition. Also, since - is not a special character, you don't need to escape it.

  • 2
    Nice answer. Surprised to see it as not accepted. – Ron Jun 13 '15 at 12:43

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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