bash: combine five lines of input to each line of output
Use the paste command:
paste -d ' ' - - - - - < tmp.txt
paste
is far better, but I couldn't bring myself todelete my previous mapfile
-based solution.
[UPDATE: mapfile
reads too many lines prior to version 4.2.35 when used with -n
]
#!/bin/bashfile=timing.csvwhile true; do mapfile -t -n 5 arr (( ${#arr} > 0 )) || break echo "${arr[*]}"done < "$file"exit 0
We can't do while mapfile ...; do
because mapfile
exists with status 0 even when it doesn't read any input.
In pure bash, with no external processes (for speed):
while true; do out=() for (( i=0; i<5; i++ )); do read && out+=( "$REPLY" ) done if (( ${#out[@]} > 0 )); then printf '%s ' "${out[@]}" echo fi if (( ${#out[@]} < 5 )); then break; fidone <input-file >output-file
This correctly handles files where the number of lines is not a multiple of 5.