Separate Entries after comma


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Separate Entries after comma
# 8  
Old 01-07-2013
@nikhil jain:
Please don't hijack others thread. Open new thread with your query

Cheers,
RangaSmilie
# 9  
Old 01-07-2013
Back to the office, HAPPY NEW YEAR Smilie

The Perl script vs :
Code:
#!/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);

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

# 10  
Old 01-07-2013
A slightly different approach, Perl and awk:

Code:
perl -F'\s{2,}' -lane'
  for $s (split /,\s/, $F[1]) {
    print join "\t", $_, $s  
      for split /,\s/, $F[0]
  }' infile

Code:
awk -F'   *' '{
  for (i = 0; ++i <= split($2, t, /, */);) 
    for (j = 0; ++j <= split($1, tt, /, */);)
      print tt[j], t[i]
  }' OFS='\t' infile


Last edited by radoulov; 01-07-2013 at 08:20 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