Split and Rename multiple files


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Split and Rename multiple files
# 1  
Old 02-25-2014
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..
# 2  
Old 02-25-2014
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

# 3  
Old 02-25-2014
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 02-25-2014
Well, try
Code:
awk    'NR==1         {next}
        !($1 in T)    {T[$1]=++CNT}
                      {print >> $1"-RE-file"T[$1]".csv"}
    ' FS=\| OFS=\| file

# 5  
Old 02-26-2014
Thanks Rudic.

Its working fine.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Split and Rename Split Files

Hello, I need to split a file by number of records and rename each split file with actual filename pre-pended with 3 digit split number. What I have tried is the below command with 2 digit numeric value split -l 3 -d abc.txt F (# Will Produce split Files as F00 F01 F02) How to produce... (19 Replies)
Discussion started by: techedipro
19 Replies

2. Shell Programming and Scripting

SBATCH trinity for multiple files and rename/move the output files

Hey guys, I have wrote the following script to apply a module named "trinity" on my files. (it takes two input files and spit a trinity.fasta as output) #!/bin/bash -l #SBATCH -p node #SBATCH -A <projectID> #SBATCH -n 16 #SBATCH -t 7-00:00:00 #SBATCH --mem=128GB #SBATCH --mail-type=ALL... (1 Reply)
Discussion started by: @man
1 Replies

3. Shell Programming and Scripting

Rename a multiple files

I have multiple files in folder which i want to rename. hence I am using the below command in my script by I get an error: export XXX_LOG_DIR="${LOG_DIR}/${XXX_HOST}/xxx/${REPORT_DATE}" mv $XXX_LOG_DIR/*.audit.gz $XXX_LOG_DIR/*.audit.log.gz But I get the below error: mv: target... (5 Replies)
Discussion started by: karan8810
5 Replies

4. Shell Programming and Scripting

Split and rename files

Hello, Need to split files into n number of files and rename the files Example: Input: transaction.txt.1aa transaction.txt.1ab ...... Output: transaction.txt.1 transaction.txt.2 transaction.txt.3 (3 Replies)
Discussion started by: krux_rap
3 Replies

5. UNIX for Dummies Questions & Answers

Rename Multiple Files

Hey guys, I am the definition of a newbie. I am in the process of trying to rip all my dvds onto a new HTPC I setup. While doing this, I am also trying to organize a bunch of other files I already have to proper naming conventions. So far I have just been naming each file separately (I am on a... (4 Replies)
Discussion started by: Ralze34
4 Replies

6. Shell Programming and Scripting

Rename the multiple files

Hi I need to reanme the multiple file using unix script I have multiple file like: sample_YYYYMMDD.xls test new_YYYYMMDD.xls simple_YYYYMMDD.xls I need to rename this file sample.xls testnew.xls SIMPLE.xls thanks (8 Replies)
Discussion started by: murari83.ds
8 Replies

7. UNIX for Dummies Questions & Answers

Split and Rename files using Terminal and bin/bash

I have a file named Me_thread_spell.txt that I want to split into smaller files. I want it to be split in each place there is a ;;;. For example, blah blah blah ;;; blah bhlah hlabl awasnceuir asenduhfoijhacseiodnbfxasd;;; oabwcuhaweoir;;; This full file would be three separate files... (7 Replies)
Discussion started by: mschpers
7 Replies

8. Shell Programming and Scripting

awk split and rename files

I have a file test1.html like below: <dctm_topnav_en_US> <html> ..... </html> <dctm_topnav_en_CA> <html> ..... </html> <dctm_topnav_en_FR> <html> ..... </html> I need to use awk to split this into three file names like en_US.html , en_CA.html, en_FR.html each having content between... (4 Replies)
Discussion started by: vijay52
4 Replies

9. UNIX for Dummies Questions & Answers

How to rename multiple files

Hi all, I have some files like: pickup.0000043200.t001.t001.data pickup.0000043200.t001.t002.data pickup.0000043200.t002.t001.data pickup.0000043200.t002.t002.data pickup.0000043200.t003.t001.data pickup.0000043200.t003.t002.data I need to rename these files to ... (4 Replies)
Discussion started by: a_dor8
4 Replies

10. UNIX for Dummies Questions & Answers

Rename multiple files

Hello, I want to rename multiple files at a time and I don't know how to do it. I have various ".mp3" files, like "band name - music name.mp3" and I want to remove the "band name" from all files. Anybody knows how to do it using shell script or sed or even perl? Thanks (7 Replies)
Discussion started by: luiz_fer10
7 Replies
Login or Register to Ask a Question