awk joining multiple lines based on field count

Hi Folks,

I have a file with fields as follows which has last field in multiple lines. I would like to combine a line which has three fields with single field line for as shown in expected output. Please help.

INPUT

hname01			windows	appnamec1eda_p1,                                                            appnamec2eda_p2,
				Vappnamec3eda_p2,
halkat-A		rhel	VPC1E3DEP0,                                                                 VP27C1E4DEP1
vimame-B		vmw	PX0427E3DBF1,
				VPC1E4DAEP1
poxdem-A		windows	VP27C1DE0,
				VP27C1DE0,
				VP27C1DE0,

Expected OUTPUT

hname01			windows	appnamec1eda_p1,appnamec2eda_p2,Vappnamec3eda_p2,
halkat-A		rhel	VPC1E3DEP0,VP27C1E4DEP1
vimame-B		vmw	PX0427E3DBF1,VPC1E4DAEP1
poxdem-A		windows	VP27C1DE0,VP27C1DE0,VP27C1DE0,

Your expected output is inconsistent as to whether there should be a trailing comma at the of your output lines. Do you want all of the commas in the input to be copied to the output, or do you want to remove the trailing comma?

What operating system are you using?

What shell are you using?

Hi Don,

Thanks for the response. I would like to keep the comma at the end. Appreciate your help.

---------- Post updated at 05:00 PM ---------- Previous update was at 04:59 PM ----------

I am using RHEL 5.0 and I am in bash shell.

Try:

awk '
BEGIN {	FS = ",[[:space:]]*"
	OFS = ","
}
/^[^[:space:]]/ {
	if(NR > 1)
		print ""
	for(i = 1; i <= NF; i++)
		if($i != "")
			printf("%s%s", $i, OFS)
	next
}
{	sub(/^[[:space:]]*/, "", $1)
	for(i = 1; i <= NF; i++)
		if($i != "")
			printf("%s%s", $i, OFS)
}
END {	print ""
}' INPUT

which, with your sample input, produces the output:

hname01			windows	appnamec1eda_p1,appnamec2eda_p2,Vappnamec3eda_p2,
halkat-A		rhel	VPC1E3DEP0,VP27C1E4DEP1,
vimame-B		vmw	PX0427E3DBF1,VPC1E4DAEP1,
poxdem-A		windows	VP27C1DE0,VP27C1DE0,VP27C1DE0,

If someone else would like to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk .

1 Like

Try also

awk '
                {gsub (/[       ]+/, " ")
                 DL = ""
                }
!/^[    ]+/     {sub (/ /, "    ")
                 DL = RS
                }
                {printf "%s%s", DL, $0
                }
END             {printf RS
                }
' file
hname01	windows appnamec1eda_p1, appnamec2eda_p2, Vappnamec3eda_p2,
halkat-A	rhel VPC1E3DEP0, VP27C1E4DEP1
vimame-B	vmw PX0427E3DBF1, VPC1E4DAEP1
poxdem-A	windows VP27C1DE0, VP27C1DE0, VP27C1DE0,
1 Like

Thank you Don and RudiC,

Both of these codes work like charm.