Split one file to many based on pattern


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Split one file to many based on pattern
# 1  
Old 11-13-2014
Split one file to many based on pattern

Hello All,

I have records in a file in a pattern A,B,B,B,B,K,A,B,B,K

Is there any command or simple logic I can pull out records into multiple files based on A record? I want output as

File1: A,B,B,B,B,K
File2: A,B,B,K
# 2  
Old 11-13-2014
So you want the first six records in one file, and the last four records in another?

Code:
awk -F, -v OFS="," '{ print $1,$2,$3,$4,$5,$6 > "file1" ; print $7, $8, $9, $10 > "file2" }' inputfile

This will work for the data you've given but might not for your real data if it's much more complicated. A representative sample (obscured where necessary) would be appreciated.
# 3  
Old 11-13-2014
Quote:
Originally Posted by Corona688
So you want the first six records in one file, and the last four records in another?

Code:
awk -F, -v OFS="," '{ print $1,$2,$3,$4,$5,$6 > "file1" ; print $7, $8, $9, $10 > "file2" }' inputfile

Thank you very much for the reply, I should not be counting number of lines, I have to pick lines based on A record.

Whenever a line starts with 'A', I need to pick all the lines until next 'A' line and write it to file.
# 4  
Old 11-13-2014
This is not apparent from the input and output you have shown. It shows a single line being divided into two different files.

Show the input you have, and show the output you want.
# 5  
Old 11-13-2014
I am sorry for any confusion, this is my first time posting here, below is the what the main file looks like:

Code:
A200198565634
B769348348547
B837563487567
K656895565906
A387562985749
B893745647875
B394857348957
K734564735644
A893745634785
B938457348953
K783456347856
A890345765875
B378945634789
B934785643534
K378945634764

Desired Output:

File1:
Code:
A200198565634
B769348348547
B837563487567
K656895565906

File2:
Code:
A387562985749
B893745647875
B394857348957
K734564735644

File3:
Code:
A893745634785
B938457348953
K783456347856

File:
Code:
A890345765875
B378945634789
B934785643534
K378945634764


Last edited by Corona688; 11-13-2014 at 05:36 PM..
# 6  
Old 11-13-2014
Why 'file' and not 'file4', or does that matter?

Code:
awk '/^A/ { if(F) close(F); F=sprintf("%s%d", NAME, ++N) } { print > F }' NAME="File" inputfile

Use nawk on solaris.
This User Gave Thanks to Corona688 For This Post:
# 7  
Old 11-13-2014
You could also do file0001, file0002, etc, etc, which sorts more nicely in a directory listing, by putting "%s%04d" in sprintf instead.
This User Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Split a text file into multiple pages based on pattern

Hi, I have a text file (attached the sample). I have also, attached the way the way the files need to be split. We get this file, that will either have 24 Jurisdictions, or will miss some and retain some. Like in the attached sample file, there are only Jurisdictions 03,11,14,15, 20 and 30.... (3 Replies)
Discussion started by: ebsus
3 Replies

2. Shell Programming and Scripting

How to split a file based on pattern line number?

Hi i have requirement like below M <form_name> sdasadasdMklkM D ...... D ..... M form_name> sdasadasdMklkM D ...... D ..... D ...... D ..... M form_name> sdasadasdMklkM D ...... M form_name> sdasadasdMklkM i want split file based on line number by finding... (10 Replies)
Discussion started by: bhaskar v
10 Replies

3. UNIX for Dummies Questions & Answers

Split a huge 7 GB File Based on Pattern into 4 files

Hi, I have a Huge 7 GB file which has around 1 million records, i want to split this file into 4 files to contain around 250k messages each. Please help me as Split command cannot work here as it might miss tags.. Format of the file is as below <!--###### ###### START-->... (6 Replies)
Discussion started by: KishM
6 Replies

4. Shell Programming and Scripting

Split the file based on pattern

Hi , I have huge files around 400 mb, which has clob data and have diffeent scenarios: I am trying to pass scenario number as parameter and and get required modified file based on the scenario number and criteria. Scenario 1: file name : scenario_1.txt ... (2 Replies)
Discussion started by: sol_nov
2 Replies

5. Shell Programming and Scripting

Split a file based on pattern and size

Hello, I have a large file (2GB) that I would like to split based on pattern and size. I've used the following command to split the file (token is "HELLO") awk '/HELLO/{i++}{print > "file"i}' input.txt and the output is similar to the following (i included filesize in KB): 10 ... (2 Replies)
Discussion started by: jl487
2 Replies

6. Shell Programming and Scripting

split XML file into multiple files based on pattern

Hello, I am using awk to split a file into multiple files using command: nawk '{ if ( $1 == "<process" ) { n=split($2, arr, "\""); file=arr } print > file }' processes.xml <process name="Process1.process"> ... (3 Replies)
Discussion started by: chiru_h
3 Replies

7. Shell Programming and Scripting

Split a file into multiple files based on the input pattern

I have a file with lines something like. ...... 123_start ...... ....... 123_end .... ..... 456_start ...... ..... 456_end .... ..... 789_start .... .... 789_end (6 Replies)
Discussion started by: abinash
6 Replies

8. Shell Programming and Scripting

Split a file based on a pattern

Dear all, I have a large file which is composed of 8000 frames, what i would like to do is split the file into 8000 single files names file.pdb.1, file.pdb.2 etc etc each frame in the large file is seperated by a "ENDMDL" flag so my thinking is to use this flag a a point to split the files... (4 Replies)
Discussion started by: Mish_99
4 Replies

9. Shell Programming and Scripting

Split File Based on Line Number Pattern

Hello all. Sorry, I know this question is similar to many others, but I just can seem to put together exactly what I need. My file is tab delimitted and contains approximately 1 million rows. I would like to send lines 1,4,& 7 to a file. Lines 2, 5, & 8 to a second file. Lines 3, 6, & 9 to... (11 Replies)
Discussion started by: shankster
11 Replies

10. Shell Programming and Scripting

Split a file based on pattern in awk, grep, sed or perl

Hi All, Can someone please help me write a script for the following requirement in awk, grep, sed or perl. Buuuu xxx bbb Kmmmm rrr ssss uuuu Kwwww zzzz ccc Roooowwww eeee Bxxxx jjjj dddd Kuuuu eeeee nnnn Rpppp cccc vvvv cccc Rhhhhhhyyyy tttt Lhhhh rrrrrssssss Bffff mmmm iiiii Ktttt... (5 Replies)
Discussion started by: kumarn
5 Replies
Login or Register to Ask a Question