I want my Unix file output, which has each value output on a new line, to be converted into grouped rows as shown below.
Say my output file in Unix looks like this:
Kind of ugly, but doesn't require a shell script:
cat outputfile | tr "\n" " " | sed -e 's/\W*\(\w*\) \(\w*\) \(\w*\) \(\w*\)\W*/\1 \2 \3 \4\n/g'
This is a minimal awk
solution:
awk 'ORS=NR%4?" ":"\n"' input.txt
jobname userid starttime endtime
jobname2 userid starttime endtime
(If you want to align the fields, pipe to column -t
)
If you are looking for a shell script, you can do this as the number of lines to be printed in the output seems to have to fixed length:
while read line1; do
read line2
read line3
read line4
echo $line1 $line2 $line3 $line4 >>output
done <file
This will paste each four consecutive lines as four tab-delimited fields:
cat source_file | paste - - - - > destination_file
With GNU sed:
tr '\n' ' ' < file | sed -r 's/(\w+ +){4}/&\n/g'