Unix/Linux Go Back    


UNIX for Dummies Questions & Answers This forum is closed for new posts. Please post beginner questions to learn unix and learn linux in this forum UNIX for Beginners Questions & Answers

Split and Rename multiple files

UNIX for Dummies Questions & Answers


 
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 02-25-2014   -   Original Discussion by armsaran
armsaran's Unix or Linux Image
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 09:54 AM..
Sponsored Links
    #2  
Old Unix and Linux 02-25-2014   -   Original Discussion by armsaran
RudiC's Unix or Linux Image
RudiC RudiC is online now Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 18 January 2018, 12:47 PM EST
Location: Aachen, Germany
Posts: 11,951
Thanks: 353
Thanked 3,686 Times in 3,384 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   -   Original Discussion by armsaran
armsaran's Unix or Linux Image
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 03:11 AM.. Reason: Please use CODE tags thanks.
    #4  
Old Unix and Linux 02-25-2014   -   Original Discussion by armsaran
RudiC's Unix or Linux Image
RudiC RudiC is online now Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 18 January 2018, 12:47 PM EST
Location: Aachen, Germany
Posts: 11,951
Thanks: 353
Thanked 3,686 Times in 3,384 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   -   Original Discussion by armsaran
armsaran's Unix or Linux Image
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
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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 07:51 AM
Rename multiple files sylcam Shell Programming and Scripting 4 08-16-2011 02:02 PM
Split and Rename files using Terminal and bin/bash mschpers UNIX for Dummies Questions & Answers 7 12-02-2010 05:58 PM
awk split and rename files vijay52 Shell Programming and Scripting 4 06-02-2010 04:47 AM
help with multiple files rename... makikicindy UNIX for Dummies Questions & Answers 3 01-12-2010 03:51 AM



All times are GMT -4. The time now is 01:52 PM.