Hi Guys,
I have file A.txt
1
2
3
4
5
6
7
8
9
10
11
Want Output :-
1 2 3
4 5 6
7 8 9
10 11
I have tried Below Code but Only work for first few lines.
sed '1,3d;N;s/\n/ /'
Thanks
Hi Guys,
I have file A.txt
1
2
3
4
5
6
7
8
9
10
11
Want Output :-
1 2 3
4 5 6
7 8 9
10 11
I have tried Below Code but Only work for first few lines.
sed '1,3d;N;s/\n/ /'
Thanks
Hello pareshkp,
Following may help you in same.
1st: By using xargs
as follows.
xargs -n3 < Input_file
Output will be as follows.
1 2 3
4 5 6
7 8 9
10 11
2nd: By using awk
solution.
awk '{ORS=NR%3==0?"\n":" "} 1' Input_file
Output will be as follows.
1 2 3
4 5 6
7 8 9
10 11
Hope this helps you.
Thanks,
R. Singh
With the awk suggestion there may be a missing newline at the end, rendering the output an improper Unix format. This could be corrected:
awk 'END{if(NR%3) printf RS} {ORS=NR%3?FS:RS}1'
--
And for completeness, the inevitable :
paste -d" " - - - < file
is another one
Or with:
[highlight=bash]#!/bin/bash
STR="1
2
3
4
5
6
7
8
9
10
11"
NR=2 ## 0, 1 , 2 = 3 items in a row
C=0
for n in $STR;do
if [ $C -eq $NR ]
then printf "%s\n" "$n"
else printf "%s " "$n"
fi
((C++))
[ $C -gt $NR ] && C=0
done
printf "\n"
exit 0[/highlight]
hth