split records into different files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting split records into different files
# 1  
Old 09-19-2011
split records into different files

Hi All,
I want my file to be split based on value of 'N' (passed as argument). If value of 'N' is '2' then 4 new files will be generated from the below source file and the o/p file shoud look like File_$num , where num will be incremental.

Source file:
1
2
3
4
5

O/p Files:
File_001.txt
1
2

File_002.txt
3
4

File_003.txt
5

Thanks in Advance,
# 2  
Old 09-19-2011
Code:
ln=2
split -l $ln  inputfile_name

one way...
# 3  
Old 09-19-2011
how to give file name as File_$num
where num is incremental as 1, 2, 3........


Thanks
# 4  
Old 09-19-2011
Code:
kent$  seq 10|awk '{f=(NR%2)==0?NR/2:int(NR/2+1); print >  f".txt"}'

kent$  head {1..5}.txt
==> 1.txt <==
1
2

==> 2.txt <==
3
4

==> 3.txt <==
5
6

==> 4.txt <==
7
8

==> 5.txt <==
9
10

# 5  
Old 09-19-2011
Change val as required.
Code:
val=2
awk -v val="$val" 'BEGIN{i=1}{if(t==1){i++;t=0}}
{if($0%val==0){t=1};file="File_"i".txt";print >>file}' inputFile

if Solaris, please use nawk

--ahamed

Last edited by ahamed101; 09-19-2011 at 10:13 AM..
# 6  
Old 09-19-2011
Thanks above code works

---------- Post updated at 08:14 AM ---------- Previous update was at 08:11 AM ----------

When I use val=4 (i expected 4 records in File_3.txt but getting 2 records)
Code:
awk -v val="$val" 'BEGIN{i=1}{if(t==1){i++;t=0}}
{if($0%val==0){t=1};file="File_"i".txt";print >>file}' inputFile

o/p:
Code:
$ cat File_1.txt
1
2
3
4
$ cat File_2.txt
5
6
7
8
$ cat File_3.txt
9
0
$ cat File_4.txt
11
12
$ cat File_5.txt
13
14
15

---------- Post updated at 08:17 AM ---------- Previous update was at 08:14 AM ----------

sk1418: without "seq 10" code is working fine. Thanks

---------- Post updated at 08:19 AM ---------- Previous update was at 08:17 AM ----------

sk1418: Contencts are getting split based on number but my files are coming with only numbers and not with f1.txt, f2.txt...

O/p file names:
Code:
4.txt
3.txt
2.txt
1.txt

Thanks

Last edited by Franklin52; 09-19-2011 at 02:43 PM.. Reason: Please use code tags for data and code samples, thank you
# 7  
Old 09-19-2011
Code:
[root@D152153 tmp]# cat input_file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

[root@D152153 tmp]# awk -v val="$val" 'BEGIN{i=1}{if(t==1){i++;t=0}}
{if($0%val==0){t=1};file="File_"i".txt";print >>file}' input_file

[root@D152153 tmp]# cat File_1.txt
1
2
3
4

[root@D152153 tmp]# cat File_2.txt
5
6
7
8

[root@D152153 tmp]# cat File_3.txt
9
10
11
12

[root@D152153 tmp]# cat File_4.txt
13
14
15

Its working for me. Can you paste your input file?
--ahamed
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Want to grep records in alphabetical order from a file and split into other files

Hi All, I have one file containing thousands of table names in single column. Now I want that file split into multiple files e.g one file containing table names starting from A, other containing all tables starting from B...and so on..till Z. I tried below but it did not work. for i in... (6 Replies)
Discussion started by: shekhar_4_u
6 Replies

2. Shell Programming and Scripting

How to split one record to multiple records?

Hi, I have one tab delimited file which is having multiple store_ids in first column seprated by pipe.I want to split the file on the basis of store_id(separating 1st record in to 2 records ). I tried some more options like below with using split,awk etc ,But not able to get proper output. can... (1 Reply)
Discussion started by: jaggy
1 Replies

3. Shell Programming and Scripting

Split records

Hi I have a file $cat test a,1;2;3 b,4;5;6;7 c,8;9 I want to split each record to multiple based on semicolon in 2nd field. i.e a,1 a,2 a,3 b,4 b,5 (3 Replies)
Discussion started by: Shivdatta
3 Replies

4. Shell Programming and Scripting

Split file based on records

I have to split a file based on number of lines and the below command works fine: split -l 2 Inputfile -d OutputfileMy input file contains header, detail and trailor info as below: H D D D D TMy split files for the above command contains: First File: H DSecond File: ... (11 Replies)
Discussion started by: Ajay Venkatesan
11 Replies

5. Shell Programming and Scripting

Split Records

I have a flat file with 2 columns Id,loc 1,nj:ny:pa 2,pa 3,ca:tx:fl:nj Second colum data is seperated by semi colon and can i have many locations for one id Output i need is 1,nj 1,ny 1,pa 1,pa 3,ca 3,tx 3,fl (1 Reply)
Discussion started by: traininfa
1 Replies

6. Shell Programming and Scripting

Split the Master and Child Records

Hi, I receive a file that has Master record followed by one/more Child Records as shown below & also as attached in the file. Now , The key for the child record is from pos 4 to position 80 in the parent record, now the requirement is to create two files 1. Parent file --> has all the parent... (1 Reply)
Discussion started by: KNaveen
1 Replies

7. Shell Programming and Scripting

Split records into multiple records

Hi All, I am trying to split a record into multiple records based on a value. Input.txt "A",1,0,10 "B",2,0,10,15,20 "C",3,11,14,16,19,21,23 "D",1,0,5 My desired output is: "A",1,0,10 "B",2,0,10 "B",2,15,20 "C",3,11,14 "C",3,16,19 "C",3,21,23 (4 Replies)
Discussion started by: kmsekhar
4 Replies

8. UNIX for Advanced & Expert Users

Split records based on '-'

HI, I have a pipe delimiter file , I have to search for second field pattern, if the second field does not contain a '-' , I need to start capturing the record from this line till I find another second field with '-' value. Below is the sample data SOURCE DATA ABC|ABC_702148-PARAM... (3 Replies)
Discussion started by: mora
3 Replies

9. UNIX for Dummies Questions & Answers

How to split multiple records file in n files

Hello, Each record has a lenght of 7 characters I have 2 types of records 010 and 011 There is no character of end of line. For example my file is like that : 010hello 010bonjour011both 011sisters I would like to have 2 files 010.txt (2 records) hello bonjour and ... (1 Reply)
Discussion started by: jeuffeu
1 Replies

10. Shell Programming and Scripting

dynamically split the records

Hi, I was wandering would it be possible to split the record dynamically based on the certain values, for an instance i have a file with record with predefined split value i.e 10 col1 col2 col3 col4 ------------------------ aaaa bbbb 2 44aaaabbbb55cccddd1110 mmn xnmn 3... (6 Replies)
Discussion started by: braindrain
6 Replies
Login or Register to Ask a Question