Unix/Linux Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Split and Rename multiple files

UNIX for Dummies Questions & Answers


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 02-25-2014
armsaran armsaran is offline
Registered User
 
Join Date: Feb 2014
Last Activity: 20 March 2014, 6:49 AM EDT
Posts: 9
Thanks: 3
Thanked 0 Times in 0 Posts
Unix or Linux Question Split and Rename multiple files

Hi,

I have a data file like below

Code:
messageid|email|timestamp
750452173|123@googlemail.com|2013-05-24 16:14:32
750464921|000@gmail.com|2013-06-13 19:38:01
750385426|001@googlemail.com|2013-01-06 12:06:36
750373470|000@wz.eu|2012-11-30 22:32:07
.
.

I want to split the files based on the first column (messageid),
Output file name should be like below

Code:
750452173-RE-file1.csv (This file should contain only the same message id)
750464921-RE-file2.csv (This file should contain only the same message id)
750385426-RE-file3.csv (This file should contain only the same message id)
750373470-RE-file4.csv (This file should contain only the same message id)
.
.

Could you please provide the unix command or perl script?

Thanks,
Saravanan

Last edited by DukeNuke2; 02-25-2014 at 08:54 AM..
Sponsored Links
    #2  
Old Unix and Linux 02-25-2014
RudiC RudiC is online now Forum Advisor  
Registered User
 
Join Date: Jul 2012
Last Activity: 21 April 2015, 7:36 AM EDT
Location: Aachen, Germany
Posts: 5,730
Thanks: 101
Thanked 1,506 Times in 1,415 Posts
This is not too difficult for the data lines only; did you try anything? If you want to keep the header for each file, additional measures must be taken. Try (and adapt)
Code:
awk 'NR>1 {print >> $1"-RE-file"++CNT".csv"}' FS=\| OFS=\| file

Sponsored Links
    #3  
Old Unix and Linux 02-25-2014
armsaran armsaran is offline
Registered User
 
Join Date: Feb 2014
Last Activity: 20 March 2014, 6:49 AM EDT
Posts: 9
Thanks: 3
Thanked 0 Times in 0 Posts
Thanks Rudic.

But its not working as per my requirements.

If we have more than one row with the same message id then it should be present in one output file. (Not separate output file)
Input :

Code:
750452173|123@googlemail.com|2013-05-24 16:14:32
750464921|000@gmail.com|2013-06-13 19:38:01
750385426|001@googlemail.com|2013-01-06 12:06:36
750373470|000@wz.eu|2012-11-30 22:32:07
750452173|120@googlemail.com|2013-05-24 16:14:32
750385426|000@googlemail.com|2013-01-06 12:06:36
750452173|1230@googlemail.com|2013-05-24 16:14:32

Output File:
File Name : 750452173-RE-file1.csv

Code:
750452173|123@googlemail.com|2013-05-24 16:14:32
750452173|120@googlemail.com|2013-05-24 16:14:32
750452173|1230@googlemail.com|2013-05-24 16:14:32

File Name : 750464921-RE-file2.csv

Code:
750464921|000@gmail.com|2013-06-13 19:38:01

File Name : 750373470-RE-file3.csv

Code:
750373470|000@wz.eu|2012-11-30 22:32:07

File Name : 750385426-RE-file4.csv

Code:
750385426|001@googlemail.com|2013-01-06 12:06:36
750385426|000@googlemail.com|2013-01-06 12:06:36


Last edited by Franklin52; 02-26-2014 at 02:11 AM.. Reason: Please use CODE tags thanks.
    #4  
Old Unix and Linux 02-25-2014
RudiC RudiC is online now Forum Advisor  
Registered User
 
Join Date: Jul 2012
Last Activity: 21 April 2015, 7:36 AM EDT
Location: Aachen, Germany
Posts: 5,730
Thanks: 101
Thanked 1,506 Times in 1,415 Posts
Well, try
Code:
awk    'NR==1         {next}
        !($1 in T)    {T[$1]=++CNT}
                      {print >> $1"-RE-file"T[$1]".csv"}
    ' FS=\| OFS=\| file

Sponsored Links
    #5  
Old Unix and Linux 02-26-2014
armsaran armsaran is offline
Registered User
 
Join Date: Feb 2014
Last Activity: 20 March 2014, 6:49 AM EDT
Posts: 9
Thanks: 3
Thanked 0 Times in 0 Posts
Thanks Rudic.

Its working fine.
Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Unix or Linux Image More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to split multiple worksheets into multiple files? Abhisrajput Shell Programming and Scripting 1 01-09-2014 06:51 AM
Rename multiple files sylcam Shell Programming and Scripting 4 08-16-2011 01:02 PM
Split and Rename files using Terminal and bin/bash mschpers UNIX for Dummies Questions & Answers 7 12-02-2010 04:58 PM
awk split and rename files vijay52 Shell Programming and Scripting 4 06-02-2010 03:47 AM
help with multiple files rename... makikicindy UNIX for Dummies Questions & Answers 3 01-12-2010 02:51 AM



All times are GMT -4. The time now is 07:39 AM.