How to Dump data into CSV file which is Separate by <tab>?

Dear Team,
please help me to solve this problem using Linux command. I want to dump this data into an excel sheet,
Suppose I have a string like:

ABC
PQR

XYZ
ASD

then I expect output as a

ABC        XYZ
PQR        ASD
pr -mts$'\t' <(sed '/^$/,$d' file) <(sed '1,/^$/d' file)

a different order would be easier

fmt file | column -t

But I understand the elementary example has a more complex structure?

--- Post updated at 09:46 ---

paste  <(sed '/^$/,$d' file) <(sed '1,/^$/d' file)

--- Post updated at 10:45 ---

awk '
BEGIN           {i=0}
!/^$/           {BUF = BUF sp $0; i+=1}
/^$/            {i=0; sp = "\t"}
END             {for (i in BUF) print BUF}
' file
1 Like

In case your column count isn't always two or the row count isn't the same in all columns, you could also try:

awk '
BEGIN {	ncol = row = 0
}
/^$/ {	ncol++
	if(row > maxrow)
		maxrow = row
	row = 0
	next
}
{	d[row++, ncol] = $0
}
END {	if(row > maxrow)
		maxrow = row
	for(row = 0; row < maxrow; row++)
		for(col = 0; col <= ncol; col++)
			printf("%s%s", d[row, col],
			    (col == ncol) ? "\n" : "\t")
}' file

which with your sample input file produces the output:

ABC	XYZ
PQR	ASD

And, if given an input file that contains:

ABC
PQR
c0r2

XYZ
ASD
c1r2
c1r3

c2r0
c2r1
c2r2
c2r3
c2r4

produces the output:

ABC	XYZ	c2r0
PQR	ASD	c2r1
c0r2	c1r2	c2r2
	c1r3	c2r3
		c2r4

If someone else wants to try this on a Solaris/SunOS system, change awk in the code above to /usr/xpg4/bin/awk or nawk .

2 Likes

thank you so much its work for me