Separate Entries after comma

Hi All

I need help to separate entries after commas in my

I have 2 columns in my file like this

Ramush, Shyam, Mohan              First

Ram, Mohan, Kaavya              Second, Fourth

Kavi, Ram, Shaym, Mohan         Third

I ahve to separate entries after comma in a separate row

Like expected output is

Ramush               First
Shyam               First
Mohan                First
Ram,               Second
Mohan              Second
Kaavya            Second 
Ram                  Fourth
Mohan               Fourth
Kaavya              Fourth
Kavi         Third
Ram             Third
Shaym       Third
Mohan         Third

Kindly let me know scripting regarding this.

Hey,

First of all, Welcome to the The UNIX and Linux Forums!!!

You have not mentioned your field delimiter. So I am assuming that the fields are separated by tab space.

awk -F"\t" '{gsub(/, /,",");split($1,a,",");split($2,b,",");for(i in b){for(j in a){print a[j],b;}}}' filename

Cheers,
Ranga:)

awk '{sub(/,.*/,x,$1); sub(/,.*/,x,$2)}1' FS='\t' OFS='\t' infile

HI ....

By using perl ....

#!/usr/local/bin/perl
$input = "input.file";
open(IN,"$input") or die "Cant open inour file\n";
while(<IN>)
{
        $line = $_;
        chomp($line);
        if ( $line =~ /\t/ )
        {
                $right = $';
                $left = $`;
                if ( $left =~ /\,/ )
                {
                        @left = split(/,/,$left);
                }
                else
                {
                        @left = $left;
                }
                if ( $right =~ /\,/ )
                {
                        @right = split(/,/,$right);
                }
                else
                {
                        @right = $right;
                }
                for ( $i=0;$i<@right;$i++)
                {
                        for ( $j=0;$j<@left;$j++)
                        {
                                print "$left[$j] :: $right[$i]\n ";
                        }
                }
                #print " Right :: $right Left :: $left \n";
        }
}
close(IN);

Thanks
Anusurya.A

Hey,

@anusurya: Please use code tags for data samples and codes. code tag icon look like -->

Perl one liner is,

perl -F'\t' -alne '@l=split(/,\s*/,$F[0]);@r=split(/,\s*/,$F[1]);foreach $i(@r){foreach $j(@l){print "$j $i";}}' filename

Cheers,
Ranga:)

---------- Post updated at 03:58 AM ---------- Previous update was at 03:56 AM ----------

Ramush  First
Ram     Second
Kavi    Third

This is what the above code gives me. But the expected is different.

Cheers,
Ranga:)

Oops, yes I rather misread the requirements :slight_smile:

similar type of requirement
my echo $var1 gives below value

1.7_NEW=25,1.7_RETAINED=30,1.7_RETURNING=40

i want it in 3 different values....

i.e. as

echo $1.7_NEW=25
echo $1.7_RETAINED=30
echo $1.7_RETURNING=40

the o/p of $1.7_NEW should be 25 :stuck_out_tongue:

@nikhil jain:
Please don't hijack others thread. Open new thread with your query

Cheers,
Ranga:)

Back to the office, HAPPY NEW YEAR :slight_smile:

The Perl script vs :

#!/usr/bin/perl -w
use strict;

my $cur_dir = $ENV{PWD};
my $filename = $cur_dir."/file";
my ($record,@fields,$i,$j);

open(FILE,"<$filename") or die"open: $!";

while( defined( $record = <FILE> ) ) {
  chomp $record;
  @fields=split(/\t/,$record);
  foreach $i (split(/, /,$fields[0])) {
    foreach $j (split(/, /,$fields[1])) {
      print "$i\t$j\n";
    }
  }
}

close(FILE);
Ramush  First
Shyam   First
Mohan   First
Ram     Second
Ram     Fourth
Mohan   Second
Mohan   Fourth
Kaavya  Second
Kaavya  Fourth
Kavi    Third
Ram     Third
Shaym   Third
Mohan   Third

A slightly different approach, Perl and awk:

perl -F'\s{2,}' -lane'
  for $s (split /,\s/, $F[1]) {
    print join "\t", $_, $s  
      for split /,\s/, $F[0]
  }' infile
awk -F'   *' '{
  for (i = 0; ++i <= split($2, t, /, */);) 
    for (j = 0; ++j <= split($1, tt, /, */);)
      print tt[j], t
  }' OFS='\t' infile