split and rename the file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting split and rename the file
# 1  
Old 06-24-2009
split and rename the file

Hi All,

I have a requirement .I want to split a file and the split files should have certain names.

Currently when i use the split command
Code:
split -1000 testdata testdata_

Then the output is
Code:
testdata_aa
testdata_bb
testdata_cc

and so on.

But i want the output as
Code:
testdata1.snd
testdata2.snd
testdata3.snd

etc..can we achieve this..Since the number of output files are dynamic, i am not sure how to do this.

Last edited by Yogesh Sawant; 06-24-2009 at 04:20 AM.. Reason: added code tags
# 2  
Old 06-24-2009
my hints for your issue
sort the generated files rename them
Code:
ls -1 testdata_* | sort -t "_" -n -k2,2 -k3,3 -k4,4

Then from the sort list you can rename in a for or while loop

Last edited by amitranjansahu; 06-24-2009 at 04:23 AM.. Reason: code tag
# 3  
Old 06-24-2009
use the below code:

Code:
k=1
for i in $( ls test* )
do
mv -f $i ${i%%_*}$k.snd
((k++))
done

Best Regards
# 4  
Old 06-24-2009
Code:
nawk -v f="$1" -v l="${2-1}" 'BEGIN{n=1}
{
file=sprintf("%s%s.snd",f,n)
print $0 >> file
if(NR % l ==0)
{
	file=sprintf("%s%s.snd",f,n)
	n++
}
}' yourfile

perl:
Code:
my $n=1;
my ($file,$line)=@ARGV;
$line||=1;
open FH,"<$file";
my $outfile=sprintf("%s%s.snd",$file,$n);
open OFH,">>$outfile";
while(<FH>){
        print OFH $_;
        if ($. % $line eq '0'){
                $n++;
                $outfile=sprintf("%s%s.snd",$file,$n);
                open OFH,">>$outfile";
        }
}

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

Rename specific file extension in directory with match to another file in bash

I have a specific set (all ending with .bam) of downloaded files in a directory /home/cmccabe/Desktop/NGS/API/2-15-2016. What I am trying to do is use a match to $2 in name to rename the downloaded files. To make things a more involved the date of the folder is unique and in the header of name... (1 Reply)
Discussion started by: cmccabe
1 Replies

3. Shell Programming and Scripting

awk : split file and rename and save in path according to content

Hello, I'm using Windows 7 ; sed, awk and gnuwin32 are installed. I have a big text file I need to manipulate. In short, I will have to split it in thousands of short files, then rename and save in a folder which name is based upon filename. Here is a snippet of my big input.txt file (this... (4 Replies)
Discussion started by: sellig
4 Replies

4. UNIX for Dummies Questions & Answers

awk - Rename output file, after processing, same as input file

I have one input file ABC.txt and one output DEF.txt. After the ABC is processed and created output, I want to rename ABC.txt to ABC.orig and DEF to ABC.txt. Currently when I am doing this, it does not process the input file as it cannot read and write to the same file. How can I achieve this? ... (12 Replies)
Discussion started by: High-T
12 Replies

5. 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

6. UNIX for Dummies Questions & Answers

Split and Rename multiple files

Hi, I have a data file like below 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... (4 Replies)
Discussion started by: armsaran
4 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. UNIX for Dummies Questions & Answers

Split then rename

Hi everyone, I am trying to write an if statement that will split a file if it is over 1 million records/lines into files with say 900,000 records and then rename those files without the aaa, aab, aac format that splitting normally does and into a specific naming convention. For instance, if... (2 Replies)
Discussion started by: coach5779
2 Replies

9. 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

10. UNIX for Dummies Questions & Answers

Split a file with no pattern -- Split, Csplit, Awk

I have gone through all the threads in the forum and tested out different things. I am trying to split a 3GB file into multiple files. Some files are even larger than this. For example: split -l 3000000 filename.txt This is very slow and it splits the file with 3 million records in each... (10 Replies)
Discussion started by: madhunk
10 Replies
Login or Register to Ask a Question