Converting a list to a row delimited

Hello,
I have a large database with the following structure:
set of clustered names followed by a hard return and followed by a second set of clustered names and so on. Sometimes the clusters can be as many as 150.
Since the data is in an Indian language, a theoretical example will make this clear.

What I need is to convert the series of lists to a single row so that the desired output is as under:

I have been using a simple macro to do the job, but given the size of the file which runs to over 300 thousand names, the macro is too slow and takes time.
A perl or awk script would be most helpful.
Many thanks in advance

Naive way:

 awk '{a=""; for(i=1;i<=NF;i++) a=a?(a "=" $i):$i ; print a}'  RS= names.txt

or more elegant:

awk '$1=$1' OFS="=" RS= names.txt
1 Like

Hi

$ paste -sd= file | sed 's/==/\n/g'
john=jon=jhon
mary=marry=meri=mari

Guru.

1 Like

you can try this...

$FILE_NAME="abc.txt";
$/="\n\n";

open(FH_R,"<",$FILE_NAME)|| die "Unable to open $FILE_NAME for reading: $!\n";
while (<FH_R>)
{
        chomp;
        s/\n+/=/g;
        print "$_\n";
}
close FH_R;
1 Like

HI ....

Try this untested perl script.....

open (FILE,"file1");
while(<FILE>)
{
   $line = $_;
   if ( $line =~ /^\s*$/ )
   {
       print "$value\n";
       $value = "";  @value = ();
   }
   else
   {
     push (@value,$line);
     $value = join "=",@value;
   }
}
close(FILE);

Thanks
Anusurya.A:b:

1 Like
$tr '\n' "=" < file | sed 's/==/\n/g'
john=jon=jhon
mary=marry=meri=mari
1 Like
awk 'NF==0{f=0;printf "\n";next}{if(f==1){printf "="$1}else{f=1;printf $1}}' filename
1 Like

Many thanks for all the responses which I got.
All the scripts both in Perl and AWK worked perfectly.
I could not implement sed since I am in windows environment and sed does not work under Windows. I am sorry but I should have mentioned that.

Just replace sed with awk..:slight_smile:

$tr '\n' "=" < file |  awk '{ gsub("==","\n")}1'
1 Like

Many thanks. Tried your suggestion and it works beautifully,