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

Control output of c++ program using makefile

I have an executable called print-word-length.cc that takes prints the length of words using from a *.txt file.

So in my makefile, ./print-word-length </dir/dictionary.txt prints out words in the dictionary.

How would I go about just printing the first 10 lengths of words in dictionary.txt?

I've tried head -n 10 ./print-word-length </dir/dictionary.txt, but it prints gibberish.

question from:https://stackoverflow.com/questions/66054672/control-output-of-c-program-using-makefile

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

1 Reply

0 votes
by (71.8m points)

This isn't a makefile question, it's a shell scripting question. You solve the problem by writing a shell command at your shell prompt, then you just put that same solution into your makefile.

The head utility prints the first lines it reads from standard input or from a file. It doesn't run a command that you give it as an argument.

It's printing the first 10 lines of the contents of your program file, which is binary "gibberish" as you say.

This is what pipes are for:

./print-word-length.cc </dir/dictionary.txt | head -n10

This says: start print-word-length.cc and set it up so whatever it prints to its output is sent to the head command as its input.


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

...