Awk 2 lines to 1 line

Input File:

Required Result:

I have tried the following code:

nawk '{for(i=1; i<=NR; i+2) {print NR,$0; getline ;print \n $0; NR=NR+2}}' temp

But doesnt gives the right result.
Help is appreciated

paste -d' ' - - < temp
OR
nawk 'ORS=(FNR%2)?FS:RS' temp

Thank you pro :slight_smile:

paste -d' ' - - < temp
nawk 'ORS=(FNR%2)?FS:RS' temp

Would appreciate if you can explain this.

You can read the man pages for 'paste' to give you a high-level of what it does - the rest should be easy.
awk:

(FNR%2) - get a 'modulo' of the current file RecordNumber (FNR) over 2 - every OTHER line.
If the mod is NON-zero, return 'FS' (FieldSeparator)
If the mod is zero, return RecordSeparator (RS)
ORS= - assign the returned value to the OutputRecordSeparator (ORS)

In other words...
If we're dealing with the ODD record/line numbers (1,3,5,7 etc), print the line and FS (separate the next line)
If we're dealing with the EVEN record/line numbers (2.4.6.8 etc), print the line and the ORS (which is by default is newLine).

Thank you Sir,

Minor change; need output comma seperated.

I have tried this but has syntax error.

nawk '{ORS=(FNR%2)?FS:RS}; OFS=,' temp

Appreciate your help

nawk -F, 'ORS=(FNR%2)?FS:RS' temp

When i pasted input file on website. i have some weird spaces but when i pasted i think website removed it. hence i am attaching it.

Requried Result
host1,server1,database1,5
host2,server2,database2,5
host3,server3,database3,5

Result i am getting is also attached

Appreciate help

you really should be able to figure it all out by now...

nawk 'ORS=(FNR%2)?OFS:RS' OFS=, temp