Separate Entries after comma


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Separate Entries after comma
# 1  
Old 01-07-2013
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

Code:
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


Code:
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.
# 2  
Old 01-07-2013
awk

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.

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

Cheers,
RangaSmilie
# 3  
Old 01-07-2013
Code:
awk '{sub(/,.*/,x,$1); sub(/,.*/,x,$2)}1' FS='\t' OFS='\t' infile

# 4  
Old 01-07-2013
HI ....

By using perl ....

Code:
#!/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

Moderator's Comments:
Mod Comment Please use code tags for code and data

Last edited by Scrutinizer; 01-07-2013 at 04:31 AM.. Reason: code tags
# 5  
Old 01-07-2013
perl

Hey,

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

Perl one liner is,

Code:
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,
RangaSmilie

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

Quote:
Originally Posted by Scrutinizer
Code:
awk '{sub(/,.*/,x,$1); sub(/,.*/,x,$2)}1' FS='\t' OFS='\t' infile

Code:
Ramush  First
Ram     Second
Kavi    Third

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

Cheers,
RangaSmilie
# 6  
Old 01-07-2013
Oops, yes I rather misread the requirements Smilie
# 7  
Old 01-07-2013
similar type of requirement
my echo $var1 gives below value

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

i want it in 3 different values....

i.e. as

Code:
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 :P

Last edited by radoulov; 01-07-2013 at 06:12 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to parse comma separated field and removing comma in between number and double quotes

Hi Experts, Please support I have below data in file in comma seperated, but 4th column is containing comma in between numbers, bcz of which when i tried to parse the file the column 6th value(5049641141) is being removed from the file and value(222.82) in column 5 becoming value of column6. ... (3 Replies)
Discussion started by: as7951
3 Replies

2. UNIX for Advanced & Expert Users

Comma separate issue in UNIX

In awk the field seprator is not working properly, I am trying to cut the fields from the file based on the delimiter example comma (,) awk -F, "{print {$1 FS $3 FS $5 FS FS $2}}" Sample.csv But i am not getting desired output can anyone help me how to check real ascii comma there in my... (9 Replies)
Discussion started by: rspwilliam
9 Replies

3. Shell Programming and Scripting

Match first column and separate entries

Hi I have 2 big files containing following information: file 1 12345 345634 217341 87234693 8236493 file 2: 12345 1237 (6 Replies)
Discussion started by: kaav06
6 Replies

4. Shell Programming and Scripting

Remove bracket part entires and separate entries after comma

Hi all This time my input conatin 3 columns: ERCC1 (PA155) Platinum compounds (PA164713176) Allele A is not associated with response to Platinum compounds in women with Ovarian Neoplasms as compared to allele C . CES1 (PA107) methylphenidate (PA450464) Genotype CT is not... (4 Replies)
Discussion started by: Priyanka Chopra
4 Replies

5. Shell Programming and Scripting

Need Help - comma inside double quote in comma separated csv,

Hello there, I have a comma separated csv , and all the text field is wrapped by double quote. Issue is some text field contain comma as well inside double quote. so it is difficult to process. Input in the csv file is , 1,234,"abc,12,gh","GH234TY",34 I need output like below,... (8 Replies)
Discussion started by: Uttam Maji
8 Replies

6. Shell Programming and Scripting

separate old entries

Hi I have a file Stomach qwe wer qwew Liver sdfjk shdf jkasfhd I want expected out shuld be in such a way that bold letters shuld comein front of non bold letter qwe Stomach wer Stomach qwew Stomach sdfjk Liver shdf Liver... (8 Replies)
Discussion started by: manigrover
8 Replies

7. Shell Programming and Scripting

Separate certain entries from a very big file

Hello I have to separate certain entries from a Big file with so many drugs and description I want to seaprate only Drug name which is mentioned as #BEGIN_DRUGCARD DB00001(means first drug description initiated) ..same way DB00002...and so on and in description I have to separate #... (3 Replies)
Discussion started by: manigrover
3 Replies

8. Shell Programming and Scripting

How to grep after the first comma till the next comma in a line

Hi Can any one pls tell me how to grep this line POPULATION,69691,20120509 I want the number 69691 from the above line. How to grep from the first comma till the next comma. Thank You.:confused: (8 Replies)
Discussion started by: rxg
8 Replies

9. Shell Programming and Scripting

using diff to on two file but ignoring the last comma separate value

Hi guys I have two file which I sdiff. ie file 1: AA,12,34,56,,789,101,,6666 file 2: AA,12,34,56,,789,101,,7777 The last comma separated value will always change from one day to the next. Is there another unix utility I can use that will sdiff two files but ignore the last comma... (1 Reply)
Discussion started by: wny201
1 Replies

10. Shell Programming and Scripting

count data separate by comma

hi experts, i have some problem with count data which separate by comma, below sample data : 01,011222823b6d,011222823f29,0028a5,002993,6212345678, 659111111111,6598204507,6281105008,6596197849,_,525016160836958,_, ffffffff,000000000000000000000000,_,_,_,fd,fd,ff,00,1,0028a5-002993,_,... (10 Replies)
Discussion started by: bucci
10 Replies
Login or Register to Ask a Question