Need help in reading a file horizontally and printing vertically

Hi Every body,

I have file which has enttries, with each 5 entries as a set of entries, I would like to read the file (line by line) and print five entries of a set vertically, the next entry should come in the next line.

Example:
cat sample_file
I am a
Unix Adminsitrator
new to shell scripting
please help me
in resolving this problem
I am a
Unix Adminsitrator
new to shell scripting
please help me
in resolving this problem
I am a
Unix Adminsitrator
new to shell scripting
please help me
in resolving this problem
I am a
Unix Adminsitrator
new to shell scripting
please help me
in resolving this problem

Output:
I am a Unix Adminsitrator new to shell scripting please help me in resolving this problem
I am a Unix Adminsitrator new to shell scripting please help me in resolving this problem
I am a Unix Adminsitrator new to shell scripting please help me in resolving this problem
I am a Unix Adminsitrator new to shell scripting please help me in resolving this problem

I have tried the following code
counter=0
while read line
do
ent=`echo $line`
counter=`expr $counter + 1`
if [ $counter -le 4 ]
then
echo `awk '{print "\t"}'` $ent
elif [ $counter -eq 5 ]
then
echo -e "\n"
counter=0
fi
done < sample_file

Thanks in advance

This should work

awk 'NR%5{printf "%s ",$0;next}1' file

but will not make you a Unix Administrator :wink:

If everything is structured, keep it simple and use this...

IFS=""
while read line1
do
        read line2
        read line3
        read line4
        read line5

echo $line1 $line2 $line3 $line4 $line5
done < sample

Or keep it short...

awk ' NR % 5 {printf ("%s ", $0);next} { print "" }' file

---------- Post updated at 06:18 AM ---------- Previous update was at 06:12 AM ----------

Even better is keep it short, and make sure it works :wink:

awk ' NR % 5 {printf ("%s ", $0);next} { print }' file

awk ' NR % 5 {printf ("%s ", $0);next} { print "" }' file 	

It won't work; It will truncate the every fifth line. remove the ""

You don't need the parenthesis(in red) and if you replace your last { print } by 1(true) you'll get the same results :wink:

Ops , typo :rolleyes:

One way to do it using perl:

perl -ne 's/\n$/ / if $.%5 != 0; $x[$i++]=$_; END{print @x}' sample_file

Or this -

perl -ne 's/\n/ / if $.%5 != 0; print' sample_file

tyler_durden