Ubuntu – sed : difference between ” sed ‘s/\-.*// ” & ” sed ‘/\-/s/\-.*//gw “

command linesedtext processing

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?

Best Answer

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.

Related Question