Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
608 views
Welcome To Ask or Share your Answers For Others

1 Answer

The pipe character (|) is interpreted by shell and it is used to glue together commands (like cat | sed in your case). sed knows nothing about this and that's the first issue that I see in the example.

So, you can chain many sed commands into one like this:

cat ... | sed 's/aa/bb/' | sed 's/cc/dd/' >file

Another approach is to execute sed only once and pass all sed commands separated by a semicolon:

cat ... | sed 's/aa/bb/;s/cc/dd/' >file

Alternatively, you can provide them with -e option:

cat ... | sed -e 's/aa/bb/' -e 's/cc/dd/' >file

Note also, that when you used many sed commands, you also passed -i option that tells sed to modify a file in-place. But when you use pipe and redirect stdout to a file, you shouldn't pass this option because sed can't modify a file because there is no file, all data comes from stdin.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...