1

Print first N words of a file

view story
linux-howto

http://stackoverflow.com – Is there any way to print the first N words of a file? I've tried cut but it reads a document line-by-line. The only solution I came up with is: sed ':a;N;$!ba;s/\n/δ/g' file | cut -d " " -f -20 | sed 's/δ/\n/g' Essentially, replacing newlines with a character that doesn't not exist in the file, applying "cut" with space as delimiter and then restoring the newlines. Is there any better solution? (HowTos)