Text formatting

I have an input file as below.

1
Sanjib
Gayen
2
Chetan
Jadhav
3
Vijaykumar
Uddi
4
Pinaki
Sarkar

I want to generate an output file as below.

1-Sanjib Gayen
2-Chetan Jadhav
3-Vijaykumar Uddi
4-Pinaki Sarkar

I have achieved this using Recording in Vi Editor. But I want this to be done on command prompt. I tried sed command as well, but couldn't make it. So, your suggestions required!!!!!:slight_smile:

$ xargs -n3 < infile
1 Sanjib Gayen
2 Chetan Jadhav
3 Vijaykumar Uddi
4 Pinaki Sarkar
$ xargs -n3 < infile | sed 's/ /-/'
1-Sanjib Gayen
2-Chetan Jadhav
3-Vijaykumar Uddi
4-Pinaki Sarkar
awk '{ORS=NR%3==1?"-":NR%3==2?FS:RS}1' file

please explain this command, as I didn't understand

awk '{ORS=NR%3==1?"-":NR%3==2?FS:RS}1' file

To determine the ORS (output record separator) we use a modulo and a conditional operator.

ORS=NR%3==1?"-" |	If NR%3==1 ORS="-"

:NR%3==2?FS:RS  |	...else if NR%3==2 ORS=FS else ORS=RS

If these stuff is new to you can have read of:

Awk tutorial:

Awk - A Tutorial and Introduction - by Bruce Barnett

Modulo operator:

Modulo operation - Wikipedia, the free encyclopedia

Conditional operator:

?: - Wikipedia, the free encyclopedia