Help with replace comma with new line and repeat the content


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with replace comma with new line and repeat the content
# 1  
Old 02-10-2015
Help with replace comma with new line and repeat the content

Input file
Code:
G       A
G       A,T
A       C
A       G,T,C
T       C
.
.

Desired Output file
Code:
G       A
G       A
G       T
A       C
A       G
A       T
A       C
T       C
.
.

Do anybody know how to replace "," in column 2 with newline and repeat the column 1 content at the same time?

Thanks for any advice.
# 2  
Old 02-10-2015
This thread from 3.5 years ago seems to be very close to what you want. Can you modify one of the solutions suggested there to get what you need?
# 3  
Old 02-10-2015
Hi Don Cragun,

Thanks for your advice.
I edit the following code:
Code:
perl -lne 'do {foreach (split /,/,$2) {print $1,":",$_}} if /^(.*?):.*:(.*?)$/' f28

to
Code:
perl -lne 'do {foreach (split /,/,$2) {print $1,"\t",$_}} if /^(.*?):.*:(.*?)$/' f28

I guess I can't get my desired output is due to the "if /^(.*?):.*Smilie.*?)$/" part?
I a bit confusing what does "if /^(.*?):.*Smilie.*?)$/" means Smilie
# 4  
Old 02-10-2015
Quote:
Originally Posted by perl_beginner
Input file
Code:
G       A
G       A,T
A       C
A       G,T,C
T       C
.
.

Desired Output file
Code:
G       A
G       A
G       T
A       C
A       G
A       T
A       C
T       C
.
.

Do anybody know how to replace "," in column 2 with newline and repeat the column 1 content at the same time?

Thanks for any advice.
Hello perl_beginner,

Could you please try following and let me know if this helps.
Code:
awk '{A=$1;gsub(/,/,"\n"A"\t",$0);print}'  Input_file

Output will be as follows.
Code:
G       A
G       A
G       T
A       C
A       G
A       T
A       C
T       C

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 5  
Old 02-10-2015
Thanks, R.Singh.
Your code worked perfect for my case Smilie
Thanks a lot.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace spaces with underscores up to first comma but not after the comma

I have a comma delimited file of major codes and descriptions. I want to replace all occurrences of spaces with underscores up to the first comma (only in the first field), but not replace spaces following the comma. For instance I have the following snippet of the file: EK ED,Elementary and... (7 Replies)
Discussion started by: tdouty
7 Replies

2. Shell Programming and Scripting

Need Help in rearranging the content of a file comma seperated

I have a file with the below content a = test1 b = test2 a = test3 b= test4 c = test6 b = test5 d = test7 d = test9 Need the output to be as follows a = test1,test3 b = test2, test5 c = test6 d = test7, test9 (4 Replies)
Discussion started by: iron_michael86
4 Replies

3. Shell Programming and Scripting

Replace comma and blank with comma and number

I, I have a file and i need to replace comma and blank space with comma and 0. cat file.txt a,5 b,1 c, d, e,4 I need the output as cat file.txt a,5 b,1 c,0 d,0 (4 Replies)
Discussion started by: jaituteja
4 Replies

4. Shell Programming and Scripting

Repeat line convert upper line

HI Guys, My Input :- R3 AV44 50 0 100 100 100 R3 AV44 1 0 100 100 100 R3 AV45 50 0 100 100 100 R3 AV45 0 3 100 100 100 R3 AV45S 50 0 100 100 100 R3 AV45S 0 4 100 100 100 Output :- R3 AV44 50 0 100 100 100 1 0 100 100 100 R3 AV45 50 0 100 100 100 0 3 100 100 100 R3 AV45S 50 0... (9 Replies)
Discussion started by: pareshkp
9 Replies

5. Shell Programming and Scripting

Repeat command with new variable for each line in txt file

Well here is my question. Let's say I have this Script: find /var/mobile/ maxdepth -2 name "$x" >> /"$x".txt The thing is I want to repeat this script and pull out a variable from a text file like this (each line = new variable $x and another run of the whole command) Thanks for... (27 Replies)
Discussion started by: pasc
27 Replies

6. 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

7. Shell Programming and Scripting

Comma delimited row into multiple rows, repeat first value

i am building a database to keep track of unix groups. Using the command "ypcat group" I get an output similar to the following group1:GROUP:9999:user1,user2,user3 groupA:GROUP:1111:usera,userb,userc I want to convert this output so it looks like this group1:user1 group1:user2... (2 Replies)
Discussion started by: newreverie
2 Replies

8. UNIX for Advanced & Expert Users

Search Parameter in first line and replace next line content

Hi, I need help. I have XML file as below <a n="infoLevel"> <v s="true"/> </a> <a n="localAddr"> <v s="server.host.com"/> </a> <a n="ListenPort"> <v s="21111"/> </a> I need to find variable "ListenPort" in line and then replace... (4 Replies)
Discussion started by: rdtrivedi
4 Replies

9. Shell Programming and Scripting

use SED to replace repeat statements

Hi I need to write a script that read a input file that had same statement repeatedly to replace only 2nd & 5th time repeated statements (ex: This is UNIX forum) with another statement ( UNIX forum threads in Shell programming) with out modifying 1st,3,4th repeated statements. I am planning to do... (2 Replies)
Discussion started by: watsup
2 Replies

10. Shell Programming and Scripting

How to replace a line content

Hi Experts, I have binary files contain an ID Line as: : : $Header: FNDSCMON.fmb 115.6 2000/01/11 10:26:10 pkm ship$ : : where the ID Line format is: $Header: <File_Name> <Version> <Last_update_date_time> pkm ship$ In this Example: File_Name = FNDSCMON.fmb Version = 115.6... (13 Replies)
Discussion started by: victorcheung
13 Replies
Login or Register to Ask a Question