Create new lines using a delimited string.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Create new lines using a delimited string.
# 1  
Old 07-12-2012
Question Create new lines using a delimited string.

Hi

I have a text file called 'fileA' which contains the follwoing line examples
Code:
01:rec1:25,50,75,100
02:rec2:30,60
03:rec3:20,40

I would like to create a new file where each of the comma separated values appears on a new line but prefixed with the first two fields e.g.
Code:
01:rec1:25
01:rec1:50
01:rec1:75
01:rec1:100
02:rec2:30
02:rec2:60
03:rec3:20
03:rec3:40

I can get the delimited string appearing on a new line with
Code:
cat fileA.txt | sed 's/,/\n/g'

- or
Code:
tr ',''\n'

But cannot prefix it with value from the first two fields?

Last edited by Franklin52; 07-12-2012 at 04:54 PM.. Reason: Please use code tags for data and code samples, thank you
# 2  
Old 07-12-2012
Hi


Code:
$ awk -F '[:,]' '{for(i=3;i<=NF;i++){print $1,$2,$i;}}' OFS=":" file
01:rec1:25
01:rec1:50
01:rec1:75
01:rec1:100
02:rec2:30
02:rec2:60
03:rec3:20
03:rec3:40

# 3  
Old 07-12-2012
MySQL

Thanks guruprasadpr - that worked fine. Thank You!!!
# 4  
Old 07-12-2012
Code:
awk -F: '{gsub(/,/,RS $1 FS $2 FS)}1' infile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Need help on an old post - How to convert a comma delimited string to records or lines of text?

Hi, Apologies in advance to the moderator if I am posting this the wrong way. I've searched and found the solution to an old post but as it is a very old post, I don't see an option to update it with additional question. The question I have is in relation to the following post: How to... (6 Replies)
Discussion started by: newbie_01
6 Replies

2. Shell Programming and Scripting

Help/Advise please for converting space delimited string variable to comma delimited with quote

Hi, I am wanting to create a script that will construct a SQL statement based on a a space delimited string that it read from a config file. Example of the SQL will be For example, it will read a string like "AAA BBB CCC" and assign to a variable named IN_STRING. I then concatenate... (2 Replies)
Discussion started by: newbie_01
2 Replies

3. Shell Programming and Scripting

Delimited records splitted into different lines

Hi I am using delimited sequence file. Delimter we are using is pipe .But for some of the records for one of the column the values are getting split into different lines as shown below "113"|"0155"|"2016-04-27 07:59:04"|"1930"|"TEST@TEST"|"2016-04-27 11:04:04.357000000"|"BO"|"Hard... (13 Replies)
Discussion started by: ginrkf
13 Replies

4. UNIX for Dummies Questions & Answers

How to convert a comma delimited string to records or lines of text?

Hi, I am not sure if I've posted this question before. Anyway, I previously asked about converting lines of text into a comma delimited string. Now I am needing to do the other way around ... :( :o Can anyone advise how is this possible? Example as below: Converting records/lines to... (2 Replies)
Discussion started by: newbie_01
2 Replies

5. Shell Programming and Scripting

Create a new file from another file with lines begins with a specific string

Hi, I have a file with below format myfile.txt aa aa1 aa2 qq aa4 ghs aa6 bbc gdf I m looking to create a file where lines begins with aa. myfile1.txt aa aa1 aa2 (4 Replies)
Discussion started by: Litu1988
4 Replies

6. Shell Programming and Scripting

How to split a file with delimited string?

I have a unix file text.txt with below content aaaaa bbbbbbb cccccccccc As of 2013 ddddddddd eeeeeeeeee eeeeeeeee fffffffff As of 2014 gggggggggggg hhhhhhhhh iiiiiiiiiiiiiiii As of 2016 Now I've to split this file with each file ending with line 'As of' . Please suggest how can I do... (6 Replies)
Discussion started by: Steven77
6 Replies

7. UNIX for Advanced & Expert Users

Parse (delimited string) key-value pairs in a column into separate lines

Hi experts, e.g. i/p data looks like 0000xm7zcNDIkP888vRqGv93xA7:176n00qql||9700005405552747,9700005405717924,9700005405733788|unidentified,unidentified,unidentified|| o/p data should like - row1: 0000xm7zcNDIkP888vRqGv93xA7:176n00qql||9700005405552747|unidentified ... (1 Reply)
Discussion started by: sumoka
1 Replies

8. UNIX for Dummies Questions & Answers

Deleting lines that contain a specific string from a space delimited text file?

Hi, I have a space delimited text file that looks like the following: 250 rs10000056 0.04 0.0888 4 189321617 250 rs10000062 0.05 0.0435 4 5254744 250 rs10000064 0.02 0.2403 4 127809621 250 rs10000068 0.01 NA 250 rs1000007 0.00 0.9531 2 237752054 250 rs10000081 0.03 0.1400 4 17348363... (5 Replies)
Discussion started by: evelibertine
5 Replies

9. Shell Programming and Scripting

Delimited string

Hi , I have a file named abc.txt. In which some sentences are written like . Now my requirement is that in a variable say "lines" 1st "Hi my name is anupam " will come then " have blabla ....................................................................................... (2 Replies)
Discussion started by: anupamhalder
2 Replies

10. Shell Programming and Scripting

Create tab-delimited file of outputs - Perl

Title is very broad, but here's an outline of what I have done so far: - I have multiple subdirectories containing multiple *.fna files of DNA sequences - I've been able to traverse these subdirectories and create a tab-delimited text file (i.e. the association file) that shows the contents of... (1 Reply)
Discussion started by: shwang3
1 Replies
Login or Register to Ask a Question