file splitting


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting file splitting
# 1  
Old 01-17-2008
Lightbulb file splitting

Hi i need to perform the following task
I have a file like this....
------------------------
HDR|FMFILL_269_20071206.txt|0712060212|FMFILL|md_ntserve|Susan Inns
ZHD|0009631099|D0269003|G|CAPG|X|NEEB|20071128114433||||OPER
MDD|138|20071130
OFT|9|19960401
THP|50|19960401
SMP|2.000|19960401
RCT|0|Constant
RCT|1|Day of Week 1
RCT|2|Day of Week 2
RCT|3|Day of Week 3
RCT|4|Day of Week 4
RCT|5|Noon Effective Temperature
RCT|6|Time of Sunset
RCT|7|(Time of Sunset)2
DYT|A1|Early August Bank Holiday
DYT|A2|Late August Bank Holiday
DYT|BD|Boxing Day
DYT|CD|Christmas Day
DYT|EM|Easter Monday
DYT|GF|Good Friday
DYT|J1|New Years Day
DYT|J2|New Year Bank Holiday
DYT|M1|1st May Bank Holiday
DYT|M2|2nd May Bank Holiday
DYT|ML|Millennium Bank
DYT|SA|Saturday
DYT|SD|Shoulder Day
DYT|SU|Sunday
DYT|WE|Weekday
DYT|X1|Adhoc National Holiday 1
DYT|X2|Adhoc National Holiday 2
DOW|1|Monday
DOW|2|Tuesday
DOW|3|Wednesday
DOW|4|Thursday
DOW|5|Friday
DOW|6|Saturday
DOW|7|Sunday
TRL|25
-------------------
what i need to do is..
cut the first two lines and the trailer (HDR,ZHD,TRL) and then i need to create 7 different file based on the first field.Ie if the first field is MDD the data should go to MDD.txt,if it is OFT the data should go to OFT.txt and so on.
CAn anyone please suggest something on this.
# 2  
Old 01-17-2008
Code:
awk 'NR>3{print r>f}{r=$0}!x[$1]++{close(f);f=$1".txt"}' FS="|" filename

Use nawk or /usr/xpg4/bin/awk on Solaris.

If your awk doesn't complain for too many open files:

Code:
awk 'NR>3{print r>f}{r=$0;f=$1".txt"}' FS="|" filename


Last edited by radoulov; 01-17-2008 at 01:04 PM.. Reason: Corrected ...
# 3  
Old 01-17-2008
file split

hi radoulov..thanks for it..
but can it generate the files dynamically..i mean based on the fist field??
also that is the snapshot of the file and i also dnt know howmany different fields will be there ,,,,i mean it may b 7 it may be 10 may be 55 also...
# 4  
Old 01-17-2008
Yes,
did you try it?
(the last version, I mean Smilie, my first post was wrong and it was corrected).
# 5  
Old 01-17-2008
i am trying it now.
# 6  
Old 01-17-2008
thanks a lot boss. its working at the initital level i mean creating the different files.
but tell me one thing..
just considder the following scenario

PER|7
COF|1.483086470|0
COF|0.000000000|1
COF|0.000000000|2
COF|0.000000000|3
COF|0.000000000|4
COF|-0.017649422|5
COF|0.000630192|6
COF|0.000005390|7
PER|8
COF|1.532905599|0
COF|0.000000000|1
COF|0.000000000|2
COF|0.000000000|3
COF|0.000000000|4
COF|-0.018495258|5
COF|0.000661940|6
COF|0.000006000|7
PER|9
COF|1.532905599|0
COF|0.000000000|1
COF|0.000000000|2
COF|0.000000000|3
COF|0.000000000|4
COF|-0.018495258|5
COF|0.000661940|6
COF|0.000006000|7
PER|10

where the first filed is coming in repeatative manner.so in that case what will happen? will it append to the files the new data or again new files will be created?? in the later case we will lose the data.
Please advice.
# 7  
Old 01-17-2008
You can use something like this:

Code:
awk 'NR>3{print r>>f}{if(!x[$1]++)close(f);r=$0;f=$1".txt"}' FS="|" filename


Last edited by radoulov; 01-17-2008 at 02:21 PM.. Reason: modified
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