Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
597 views
in Technique[技术] by (71.8m points)

awk - How do I make these sed commands into a oneliner


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

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...