简体   繁体   中英

Why does “… >> out | sort -n -o out” not actually run sort?

As an exercise, I should find all .c files starting from my home directory, count the lines of each file and store the sorted output in sorted_statistics.txt, using find, wc, cut ad sort. I found this command to work

find /home/user/ -type f -name "*.c" 2> /dev/null -exec wc -l {} \; | cut -f 1 -d " " | sort -n -o sorted_statistics.txt

but I can't understand why

find /home/user/ -type f -name "*.c" 2> /dev/null -exec wc -l {} \; | cut -f 1 -d " " >> sorted_statistics.txt | sort -n sorted_statistics.txt

stops just before the sort command. Just out of curiosity, why is that?

This part of the command makes no sense:

cut -f 1 -d " " >> sorted_statistics.txt | sort ...

because the output of cut is appended to the file sorted_statistics.txt and no output at all goes to the sort command. You will probably want to use tee :

cut -f 1 -d " " | tee -a sorted_statistics.txt | sort ...

The tee command sends its input to a file and also to the standard output. It is like a Tee junction in a pipeline.

You were appending everything to sorted_statistics.txt ( consuming all the output ) and then trying to use that none existing output in a pipe for sort. I have corrected your code so it works now.

find /home/user/ -type f -name "*.c" 2> /dev/null -exec wc -l {} \; | cut -f 1 -d " " >> tmp.txt && sort -n tmp.txt > sorted_statistics.txt

Regards!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM