Help with splitting of file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with splitting of file
# 1  
Old 07-10-2015
Help with splitting of file

Hi,

I'm beginner in UNIX
I would like to split file in separate files depending on Pattern.

Input file looks like:-
Code:
A
B
C
brfbeg
A
B
C
brfbeg
A
B
C
brfbeg
.
.

n so on

Expected output:-
File 1 -
Code:
A
B
C

File 2 -
Code:
A
B
C

n so on

I found $ awk '/START/{x="F"++i;}{print > x;}' INPUT
but it looks for pattern at the beginning & I don't have it in my file

Regards,
Rohit

Last edited by rbatte1; 07-10-2015 at 12:43 PM.. Reason: Added CODE and ICODE tags
# 2  
Old 07-10-2015
Welcome Rohit_Mokal ,

Do you want to break to a new file each time you encounter the string brfbeg?

If that is correct, have a look at csplit

We have something that does this, so I have adjusted it to see if it helps:-
Code:
filecnt=`grep -c "^brfbeg" input_file`
((filecnt=$filecnt-1))
csplit -f my_filename -n 5 -s input_file "/^brfbeg/" {$filecnt}

This will create a set of files called my_filename.00000, my_filename.00001, my_filename.00002, my_filename.00003, etc.

The -n flag gives you the number of digits to use.

It will keep use the string to split on as the first line, but hopefully that's not too much of an issue.



Does this meet your needs?

Robin

Last edited by rbatte1; 07-10-2015 at 01:39 PM.. Reason: Changed "grep ..... | wc -l" to "grep -c ....." because I stole the original from someone else's coding ;)
# 3  
Old 07-10-2015
Well, your attempt was not too far off. Try
Code:
awk '
NR==1           {x="F"++i}       
/brfbeg/        {x="F"++i
                 next 
                }
                {print > x}
' file


Last edited by RudiC; 07-10-2015 at 01:25 PM..
This User Gave Thanks to RudiC For This Post:
# 4  
Old 07-10-2015
Quote:
Originally Posted by RudiC
Well, your attempt was not too far off. Try
Code:
awk '
NR==1           {x="F"++i}       
/brfbeg/        {x="F"++i
                 next 
                }
                {print > x}
' file

Depending on what OS you're using, what version of awk you're using, and the number of files to being created, this could run out of file descriptors. And if you're creating more than 9 files, you probably want to format your file numbers with leading zeros so all of the filenames will be the same length (and so file* will expand to a list of files with contents in the same order as the contents of the original file). Assuming you'd like 3 decimal digits in your output filenames, I'd suggest the slightly more complicated:
Code:
Code:
awk -v n=3 '
NR==1           {x=sprintf("File%0*d", n, ++i)}       
/brfbeg/        {close(x)
                 x=sprintf("File%0*d", n, ++i)
                 next
                }
                {print > x}
' file

If you want a different number of digits in your filenames, change the number shown in red to the number of digits you want.

And, as always, if someone wants to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk.
# 5  
Old 07-13-2015
Thanks everyone for your response!

I tried solution from Don & it worked fine. We r planning to split input file in 1000 small files & solution works perfect with n =4.
# 6  
Old 07-14-2015
Hi,

Could you please let me know how to direct output files created in above awk (Post from Don) to different Directory?
# 7  
Old 07-14-2015
You can add a path - either as a string constant or as a variable defined in the way n is defined - in front of the x file name, either in the redirected print command, or within the sprintf function parameters.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Splitting the file based on two fields - Fixed length file

Hi , I am having a scenario where I need to split the file based on two field values. The file is a fixed length file. ex: AA0998703000000000000190510095350019500010005101980301 K 0998703000000000000190510095351019500020005101480 ... (4 Replies)
Discussion started by: saj
4 Replies

2. Shell Programming and Scripting

Splitting a text file into smaller files with awk, how to create a different name for each new file

Hello, I have some large text files that look like, putrescine Mrv1583 01041713302D 6 5 0 0 0 0 999 V2000 2.0928 -0.2063 0.0000 N 0 0 0 0 0 0 0 0 0 0 0 0 5.6650 0.2063 0.0000 N 0 0 0 0 0 0 0 0 0 0 0 0 3.5217 ... (3 Replies)
Discussion started by: LMHmedchem
3 Replies

3. Shell Programming and Scripting

Execution of loop :Splitting a single file into multiple .dat file

hdr=$(cut -c1 $path$file|head -1)#extract header”H” trl=$(cut -c|path$file|tail -1)#extract trailer “T” SplitFile=$(cut -c 50-250 $path 1$newfile |sed'$/ *$//' head -1')# to trim white space and extract table name If; then # start loop if it is a header While read I #read file Do... (4 Replies)
Discussion started by: SwagatikaP1
4 Replies

4. Shell Programming and Scripting

Splitting XML file on basis of line number into multiple file

Hi All, I have more than half million lines of XML file , wanted to split in four files in a such a way that top 7 lines should be present in each file on top and bottom line of should be present in each file at bottom. from the 8th line actual record starts and each record contains 15 lines... (14 Replies)
Discussion started by: ajju
14 Replies

5. UNIX for Dummies Questions & Answers

Extracting data from one file, based on another file (splitting)

Dear All, I have two files but want to extract data from one based on another... can you please help me file 1 David Tom Ellen and file 2 David|0010|testnamez|resultsz David|0004|testnamex|resultsx Tom|0010|testnamez|resultsz Tom|0004|testnamex|resultsx Ellen|0010|testnamez|resultsz... (12 Replies)
Discussion started by: A-V
12 Replies

6. Shell Programming and Scripting

Splitting a file in to multiple files and passing each individual file to a command

I have an input file with contents like: MainFile.dat: 12247689|7896|77698080 16768900|hh78|78959390 12247689|7896|77698080 16768900|hh78|78959390 12247689|7896|77698080 16768900|hh78|78959390 12247689|7896|77698080 16768900|hh78|78959390 12247689|7896|77698080 16768900|hh78|78959390 ... (4 Replies)
Discussion started by: rkrish
4 Replies

7. Shell Programming and Scripting

File splitting, naming file according to internal field

Hi All, I have a rather stange set of requirements that I'm hoping someone here could help me with. We receive a file that is actually a concatenation of 4 files (don't believe this would change, but ideally the solution would handle n files). The super-file looks like:... (7 Replies)
Discussion started by: Leedor
7 Replies

8. Shell Programming and Scripting

splitting the file

Hi , I have one file which has many headers. Say suppose HEDAER ..data DATA DATA ..data ..data HEADER ..data ..data DATA .data HEADER. ..data ..data If there are 3 HEADERS in source file then I need to split the source file into 3 separate file.... (2 Replies)
Discussion started by: tanyaheerani
2 Replies

9. UNIX for Dummies Questions & Answers

Splitting a file based on record sin another file

All, We receive a file with a large no of records (records can vary) and we have to split it into two files based on another file. e.g. File1: UHDR 2008112 "25187","00000022","00",21-APR-1991,"" ,"D",-000000519,+0000000000,"C", ,+000000000,+000000000,000000000,"2","" ,21-APR-1991... (7 Replies)
Discussion started by: er_ashu
7 Replies

10. Shell Programming and Scripting

[Splitting file] Extracting group of segments from one file to others

Hi there, I need to split one huge file into separate files if the condition is fulfilled according to that the position between 97 and 98 matches with “IT” at the segment MAS. There is no delimiter file is fix-width with varous line length. Could you please help me how I do split the file... (1 Reply)
Discussion started by: ozgurgul
1 Replies
Login or Register to Ask a Question