Joining lines in a text file using AWK or SED

Hi All

I'm struggling a bit here :frowning:

I need a way of joining lines contained in a text file. I've seen numerous SED and AWK examples and none of them seem to be working for me.

The text file has 4 lines:

DELL1427 
DOC 
30189342
79

Now bear with me on this one as I'm actually scripting in a batch file using the ported versions of awk and sed (and other unix utils) for Windows.

I've use all these lines (not at the same time) with no luck:

awk "BEGIN {x = 0} {if (x<2) {printf("%s ",$0)x=x+1} if (x==2) {printf("\n")x = 0} }" %OUTPUT% > %OUTPUT2%
type %OUTPUT% | awk "BEGIN {FS="|";FLDmax=10;}{while (NF < FLDmax || $0 ~ /\|\"$/) {getline record;$0 = $0 record}print $0}"  > %OUTPUT3%
sed "{N;N;/s/\n/ /g}" %OUTPUT% > %OUTPUT2%
sed "$!N;s/\n/ /" %OUTPUT% > %OUTPUT2%
sed -e :a -e N -e "s/\n/ /" -e ta %OUTPUT% > %OUTPUT2%
type %OUTPUT% | tr -d "\n" > %OUTPUT2%

In some cases, the output file has been changed to include spaces at the beginning of each line, other times, it hasn't made any changes to the file - and the complex awk commands don't seem to work at all.

Any ideas where I'm going wrong?

What should be the desired output of the given text file?

Ahhh - I've managed to do it using the 'paste' program bundled with coreutils :slight_smile:

---------- Post updated at 04:41 PM ---------- Previous update was at 01:30 PM ----------

Ah - I was missing one vital piece of information for you guys! The desired out put was:

DELL1427,DOC,30189342,79

I've managed to do it using a combination of paste and sed :slight_smile:

With paste:

paste -sd, infile

try:

awk '{a[NR+1]=$1}NR>1{printf a[NR]","}END{print $1}' urfile
 

or:

sed ':a;$!N;s/\n/,/;ta' urfile