Haifeng Zhang
Haifeng Zhang

Reputation: 31895

what does dash e(-e) mean in sed commands?

I am new to sed, and always execute one command on an input file, recently I try to use "-e" to work on multiple commands, but I cannot figure out how it really work, the default print is quite annoying, so I cannot figure out in which order the commands are executed.

   sed -e 'command 1' -e 'command 2'  input.txt

content of input.txt:

line1
line2
line3

Question 1: What is the processing flow? is it

command1 processes line1 and then command2 processes new-line1(processed by cmd1)
command1 processes line2 and then command2 processes new-line2(processed by cmd1)
command1 processes line3 and then command2 processes new-line3(processed by cmd1)

or

command1 processes line1
command1 processes line2
command1 processes line3
command2 processes new-line1(already processed by cmd1)
command2 processes new-line2(already processed by cmd1)
command2 processes new-line3(already processed by cmd1)

Question 2: As i mentioned, the default print is quite annoying, should I use -n in front of the first -e or in front of both -e?

Thanks in advance.

Edit(It seems the working flow is the first assumption):

input.txt

1
2
3

sed -e '{s/1/ONE/;s/2/TWO/;/3/q}' -e '{s/ONE/THREE/}' numbers.txt 

THREE
TWO
3

I tried the above command, and it seems the working flow is command1 processes line1, and then command2 processes new-line1(cmd1 processed it), and then cmd1 processes next line

Upvotes: 4

Views: 3322

Answers (2)

Cole Tierney
Cole Tierney

Reputation: 10314

The sed -e commands are combined to create a single sed script. The following yields the same results (notice that -e is implied):

sed '
    s/1/ONE/
    s/2/TWO/
    /3/q
    s/ONE/THREE/
' input.txt

Or as a one liner:

sed 's/1/ONE/; s/2/TWO/; /3/q; s/ONE/THREE/' input.txt

Upvotes: 5

Tiago Lopo
Tiago Lopo

Reputation: 7959

Sed --help will tell you what -e means:

 -e script, --expression=script
             add the script to the commands to be executed

I have used sed for years, and 99% of the time I use -e, except when I want to change the file then I used -i

Question 1: I am not sure about the flow, but to play safe I always pipe it to another sed

ex:

cat input.txt | sed -e 'command 1' | sed -e 'command 2'

Question 2: WRT default print, yes you can combine.

Default:

tiago@dell:~$ cat /etc/passwd | sed -e '/root/ !d'
root:x:0:0:root:/root:/bin/bash

Quiet:

tiago@dell:~$ cat /etc/passwd | sed -ne '/root/p'
root:x:0:0:root:/root:/bin/bash

Upvotes: 1

Related Questions