Split file based on records


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Split file based on records
# 8  
Old 07-12-2013
looking at your example, you could use sed to do the pattern match and pipe it to split

Code:
sed -n /^D/p file | split -l2

# 9  
Old 07-12-2013
Hi Rajamadhavan,

This works cool. Just few confirmation required.

I guess the ^ is escape character, Am i right?
Secondly, the split command is creating the files with names xaa,xab and so on.
I want to give specific name and i tried giving the below command:
Code:
sed -n /^D/p file | split -l 2 -d test

This fails saying that, "test" does not exist. Where in i want to create file names as test01,test02 and so on.

Can you suggest where to mention the spit file name.

Thanks,
Ajay

Last edited by Franklin52; 07-14-2013 at 05:59 PM.. Reason: Please use code tags
# 10  
Old 07-12-2013
^ is the regex indicating the beginning of the line.

This will change the prefix from 'x' to 'test'. As far as I know, you can only change the prefix and not the suffix. So the split files will be testaa, testab and so on

Code:
sed -n /^D/p file | split -l2 - test

This User Gave Thanks to rajamadhavan For This Post:
# 11  
Old 07-12-2013
Thanks ton Rajamadhavan Smilie Smilie

It worked. And we are also able to change the suffix also:
Code:
sed -n /^$a/p file | split -l 2 -d - test

And my output is test00, test01 and so on.

Thanks again for ur help.

Thanks,
Ajay

Last edited by Franklin52; 07-14-2013 at 05:59 PM.. Reason: Please use code tags
# 12  
Old 07-12-2013
Ok. -d option is not available on split on my system.
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

Split a large file in n records and skip a particular record

Hello All, I have a large file, more than 50,000 lines, and I want to split it in even 5000 records. Which I can do using sed '1d;$d;' <filename> | awk 'NR%5000==1{x="F"++i;}{print > x}'Now I need to add one more condition that is not to break the file at 5000th record if the 5000th record... (20 Replies)
Discussion started by: ibmtech
20 Replies

3. Shell Programming and Scripting

Filter records based on 2nd file

Hello, I want to filter records of a file if they fall in range associated with a second file. First the chr number (2nd col of 1st file and 1st col of 2nd file) needs to be matched. Then if the 3rd col of the first file falls within any of the ranges specified by the 2nd and 3rd cols , then... (4 Replies)
Discussion started by: ritakadm
4 Replies

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

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

6. Shell Programming and Scripting

parallel while loop based on the file records

Hi, I need to execute parallel with while loop. Input File(source_file.csv) contains filenames the below source_file.csv file contains Customer1.txt Product1.txt Sales.txt Emp.txt Dept.txt Based on the number of rows that file I want to run the script ‘n' times. while... (2 Replies)
Discussion started by: onesuri
2 Replies

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

8. Shell Programming and Scripting

Based on num of records in file1 need to check records in file2 to set some condns

Hi All, I have two files say file1 and file2. I want to check the number of records in file1 and if its atleast 2 (i.e., 2 or greater than 2 ) then I have to check records in file2 .If records in file2 is atleast 1 (i.e. if its not empty ) i have to set some conditions . Could you pls... (3 Replies)
Discussion started by: mavesum
3 Replies

9. UNIX for Dummies Questions & Answers

Filtering records of a file based on a value of a column

Hi all, I would like to extract records of a file based on a condition. The file contains 47 fields, and I would like to extract only those records that match a certain value in one of the columns, e.g. COL1 COL2 COL3 ............... COL47 1 XX 45 ... (4 Replies)
Discussion started by: risk_sly
4 Replies

10. Shell Programming and Scripting

Sort & Split records in a file

Hi, I am new to scripting. I need a script to sort and the records in a file and then split them into different files. For example, the file is: H1...................... H2...................... D2.................... D2.................... H1........................... (15 Replies)
Discussion started by: Sunitha_edi82
15 Replies
Login or Register to Ask a Question