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
278 views
in Technique[技术] by (71.8m points)

linux - cut not cutting the columns when used with pipe

I'm trying to grep and cut using pipes.
file:

c1 c2
e1 e2
e3 e4

this file has space-separated values, but when I execute

cut -f2 -d' ' | grep "e2" example.csv
output is e1 e2

the output should be e2 but I'm not sure if the cut takes input in some other format, in the man pages it says that file name if unspecified then input would be taken.

question from:https://stackoverflow.com/questions/65887783/cut-not-cutting-the-columns-when-used-with-pipe

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

1 Reply

0 votes
by (71.8m points)

You have it backwards. The output of cut must be sent to grep.

cut -f2 -d' ' example.csv | grep e2

Otherwise, grep operates on the file, not on its input, and cut operates on the standard input, which is coming from the outer context, probably your keyboard?

The pipeline takes the output of the left hand side command and connects it to the input of the right hand side one, not the other way round. The left hand side command doesn't know what the arguments of the right hand side command are (and vice versa).


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

...