Sample Header Ad - 728x90

Can `head` read/consume more input lines than it outputs?

14 votes
1 answer
994 views
Given the following 3 scripts: 1. `printf 'a\nb\nc\n' > file && { head -n 1; cat; } file && { head -n 1; cat; } $ printf 'a\nb\nc\n' | { head -n 1; cat; } a

$ { head -n 1; cat; } < <(printf 'a\nb\nc\n') a What is causing the different output from those scripts? --- Additional info - this is apparently not just a head problem: $ printf 'a\nb\nc\n' | { sed '1q'; cat; } a $ printf 'a\nb\nc\n' | { awk '1;{exit}'; cat; } a $ { sed '1q'; cat; } < <(printf 'a\nb\nc\n') a $ { awk '1;{exit}'; cat; } < <(printf 'a\nb\nc\n') a --- What would be a robust, POSIX way in shell (i.e. without just invoking awk or similar once to do everything) to read some number of lines from input and leave the rest for a different command regardless of whether the input is coming from a pipe or a file? --- This question was inspired by comments under an answer to [_sort the whole .csv based on the value in a certain column_](/a/750386/133219).

Asked by Ed Morton (35459 rep)
Jul 3, 2023, 05:39 PM
Last activity: Jul 4, 2023, 03:05 PM