Split a text file into multiple pages based on pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Split a text file into multiple pages based on pattern
# 1  
Old 07-09-2014
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.
For all the Jurisdictions in the file, i should split based on the Jurisdiction number. Some scenarios (like Jurisdiction 15), a jurisdiction can span multiple pages. In that scenario, it should retain all those pages into a separate file.

and these file names should be appended with the Jurisdiction number, so that they can be identified.
Please help.
# 2  
Old 07-10-2014
Why not look for something that will always appear in the first line and insert a form feed character at the beginning of the line?
# 3  
Old 07-10-2014
The criteria for a new file is the string JURISDICTION NUMBER:
No problem if this is the first line in the new file.
But you want already the previous line to go to the new file.
Here is a general solution with a backlog (ring-)buffer.
Code:
awk '
BEGIN {
  # backlog buffer length
  b=1
  # backlog buffer will be B[0]...B[b-1]
  # NR is the current line number
  # NR%b cycles thru 0...(b-1)
}
($1=="JURISDICTION" && $2=="NUMBER:") {
 # new output file
 ofile="Report-"$3".txt"
}
(NR>b) {
 # print the last line of the backlog buffer, but not for line 1...(b-1)
 print B[NR%b] > ofile
}
{
  B[NR%b]=$0
}
END {
  # print the outstanding lines from the backlog buffer
  for (i=1; i<=b; i++) print B[(NR+i)%b] > ofile
}
' Report-02.txt


Last edited by MadeInGermany; 07-11-2014 at 08:14 AM.. Reason: changed b=2 to b=1 and adapted code
# 4  
Old 07-18-2014
Awesome!! Thanks! that was wonderful Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

UNIX script to append multiple text files into one file based on pattern present in filaname

Hi All-I am new to Unix , I need to write a script. Can someone help me with a requirement where I have list of files in a directory, I want to Merge the files if a pattern of string matches in filenames? AAAL_555A_ORANGE1_F190404.TXT AAAL_555A_ORANGE2_F190404.TXT AAAL_555A_ORANGE3_F190404.TXT... (6 Replies)
Discussion started by: Shankar455
6 Replies

2. UNIX for Advanced & Expert Users

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 (9 Replies)
Discussion started by: deal1dealer
9 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. UNIX for Dummies Questions & Answers

print multiple lines from text file based on pattern list

I have a text file with a list of items/patterns: ConsensusfromCGX_alldays_trimmedcollapsedfilteredreadscontiglist(229095contigs)contig12238 ConsensusfromCGX_alldays_trimmedcollapsedfilteredreadscontiglist(229095contigs)contig34624... (1 Reply)
Discussion started by: Oyster
1 Replies

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

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

10. UNIX for Dummies Questions & Answers

Split text file by pages

Hello! Firts of all, I'm sorry for my English. My problem: I have text file with few Form Feed symbols (FF, ASCII code =12) inside (for example - some report, consists of some pages for printing). I want to split this text by pages - each page (until FF symbol) in single file. I... (2 Replies)
Discussion started by: ranri
2 Replies
Login or Register to Ask a Question