Need help in splitting the string to diff rows


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help in splitting the string to diff rows
# 1  
Old 04-25-2013
Need help in splitting the string to diff rows

Hi,

I have file with values as below
Code:
1~ab~456~ac:bd:de:ef~yyyy-mm-dd
2~cd~458~af:fg:ty:er:ty:uj:io:~yyyy-mm-dd

I want the o/p as for frist row
Code:
1~ab~456~ac~yyyy-mm-dd
1~ab~456~bd~yyyy-mm-dd
1~ab~456~de~yyyy-mm-dd
1~ab~456~ef~yyyy-mm-dd

and for the second row
Code:
2~cd~458~af~yyyy-mm-dd
2~cd~458~fg~yyyy-mm-dd
2~cd~458~ty~yyyy-mm-dd

and so on...

So i want split the colon seperated values in rows along with other couln values..

Please help

Last edited by Franklin52; 04-25-2013 at 07:11 AM.. Reason: Please use code tags
# 2  
Old 04-25-2013
Code:
$ cat file
1~ab~456~ac:bd:de:ef~yyyy-mm-dd
2~cd~458~af:fg:ty:er:ty:uj:io~yyyy-mm-dd

Code:
$ awk '{n = split ($4, a, ":"); for (i = 1; i <= n; i++) { print $1, $2, $3, a[i], $5} }' FS='~' OFS='~' file
1~ab~456~ac~yyyy-mm-dd
1~ab~456~bd~yyyy-mm-dd
1~ab~456~de~yyyy-mm-dd
1~ab~456~ef~yyyy-mm-dd
2~cd~458~af~yyyy-mm-dd
2~cd~458~fg~yyyy-mm-dd
2~cd~458~ty~yyyy-mm-dd
2~cd~458~er~yyyy-mm-dd
2~cd~458~ty~yyyy-mm-dd
2~cd~458~uj~yyyy-mm-dd
2~cd~458~io~yyyy-mm-dd

# 3  
Old 04-25-2013
Thanks....

Thanks a ton!!!! It worked like magic...
# 4  
Old 04-25-2013
You're very welcome.
# 5  
Old 04-25-2013
Quote:
Originally Posted by hanson44
Code:
$ cat file
1~ab~456~ac:bd:de:ef~yyyy-mm-dd
2~cd~458~af:fg:ty:er:ty:uj:io~yyyy-mm-dd

Code:
$ awk '{n = split ($4, a, ":"); for (i = 1; i <= n; i++) { $4=a[i];print;} }' FS='~' OFS='~' file
1~ab~456~ac~yyyy-mm-dd
1~ab~456~bd~yyyy-mm-dd
1~ab~456~de~yyyy-mm-dd
1~ab~456~ef~yyyy-mm-dd
2~cd~458~af~yyyy-mm-dd
2~cd~458~fg~yyyy-mm-dd
2~cd~458~ty~yyyy-mm-dd
2~cd~458~er~yyyy-mm-dd
2~cd~458~ty~yyyy-mm-dd
2~cd~458~uj~yyyy-mm-dd
2~cd~458~io~yyyy-mm-dd

Have shortened a bit.

Cheers!
-R
This User Gave Thanks to rangarasan For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Splitting delimited string into rows

Hi, I have a requirement that has 50-60 million records that we need to split a delimited string (Delimeter is newline) into rows. Source Date: SerialID UnidID GENRE 100 A11 AAAchar(10)BBB 200 B11 CCCchar(10)DDD(10)ZZZZ Field 'GENRE' is a string with new line as delimeter and not sure... (5 Replies)
Discussion started by: techmoris
5 Replies

2. Shell Programming and Scripting

Transpose Datefield from rows to column + Print time diff

Hi Experts, Can you please help me in transposing Datefield from rows to column and calculate the time difference for each of the Jobids: Input File: 08/23/2012 12:36:09,JOB_5340 08/23/2012 12:36:14,JOB_5340 08/23/2012 12:36:22,JOB_5350 08/23/2012 12:36:26,JOB_5350 Required Output:... (6 Replies)
Discussion started by: asnandhakumar
6 Replies

3. UNIX for Dummies Questions & Answers

[SOLVED] splitting a single column(with spaces) into multiple rows

Hi All, My requisite is to split a single column of phonemes seperated by spaces into multiple rows. my input file is: a dh u th a qn ch A v U r k my o/p should be like: adhu a dh u (3 Replies)
Discussion started by: girlofgenuine
3 Replies

4. Shell Programming and Scripting

Replicating rows by splitting column in text file

Hi, I have flat file with following format Col1, Col2, Col3, Col4 --------------------------------- r1_c1, r1_c2, r1_c3, abc | def | efg r2_c1, r2_c2, r2_c3, abcwdw | dweweef | efg | ijk r3_c1, r3_c2, r3_c3, abaac ........... The first three columns contain only one entry per... (3 Replies)
Discussion started by: nick2011
3 Replies

5. UNIX for Dummies Questions & Answers

[diff] hide missing rows, show similar

Hi all! Having the following two csv files: file1 AAA;0000;RED CCC;9900;GREEN file2 AAA;0000;BLACK BBB;0099;BLU What's the correct syntax to hide only the missing rows (BBB,CCC) and show the rows that differ only with last field? I expect something like this: diff <options> file1... (2 Replies)
Discussion started by: Evan
2 Replies

6. Shell Programming and Scripting

Join txt files with diff cols and rows

I am a new user of Unix/Linux, so this question might be a bit simple! I am trying to join two (very large) files that both have different # of cols and rows in each file. I want to keep 'all' rows and 'all' cols from both files in the joint file, and the primary key variables are in the rows.... (1 Reply)
Discussion started by: BNasir
1 Replies

7. Linux

Splitting a Text File by Rows

Hello, Please help me. I have hundreds of text files composed of several rows of information and I need to separate each row into a new text file. I was trying to figure out how to split the text file into different text files, based on each row of text in the original text file. Here is an... (2 Replies)
Discussion started by: dvdrevilla
2 Replies

8. UNIX for Dummies Questions & Answers

Splitting a string and putting another string in the middle?

Hello all! I'm trying to put together a small script that will take in a file name and attach a datestamp to the end of it (but before the file type extension). To illustrate... Before: filename.txt anotherfilename.txt After: filename_20090724.txt anotherfilename_20090724.txt ... (7 Replies)
Discussion started by: jisoo411
7 Replies

9. Shell Programming and Scripting

Splitting file based on number of rows

Hi, I'm, new to shell scripting, I have a requirement where I have to split an incoming file into separate files each containing a maximum of 3 million rows. For e.g: if my incoming file say In.txt has 8 mn rows then I need to create 3 files, in which two will 3 mn rows and one will contain 2... (2 Replies)
Discussion started by: wahi80
2 Replies

10. UNIX for Dummies Questions & Answers

splitting a column into rows

I have a column of data of the format: EDITORIAL OPED 193987141 193986701 193987451 193986321 STATISTICS 193986351 COLUMN EDITORIAL OPED 193987171 NEWS 193321171 NEWS 193321111 NEWS 193320891 NEWS 193321841 (3 Replies)
Discussion started by: spindoctor
3 Replies
Login or Register to Ask a Question