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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Execution of loop :Splitting a single file into multiple .dat file
# 1  
Old 08-03-2014
Execution of loop :Splitting a single file into multiple .dat file

Code:
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[‘$hdr' ='H']; then  # start loop if it is a header
While read I   #read file
Do
$parh$file>${splitfile}.dat  #create new file with table name as present in header
If[$”trl=”='T'];then
#Continue loop for next iteration to create next table
if If[$”trl=”='T'];then #reached EOF
exit 0
fi
fi
done<”path1$newfile”

Please help with execution of loop
Start loop if 'H' found
Loop through Untill it finds a 'T'
Start next iteration of file continue
Stop loop if it encounters next 'T'
# 2  
Old 08-03-2014
It looks like you have edited this script in word. starting lines with upper case is not valid as shell is case sensitive and keywords like While Do and If will not work.

Also quotes are incorrect and again it looks like word has changed them to sloping version eg “T” instead of "T"

Please notify what your target OS and shell are and provide a sample input file.
Also describe in English what processing you want along with sample output file(s) as appropriate.
# 3  
Old 08-04-2014
Some more comments on top of what Chubler_XL said:

Just from looking at the snippet one can see many syntax errors, maybe sloppy typing. Make sure your variables are correctly expanded with the "$" sign in front. Surround "[", "]", and "=" with spaces. When quoting variables, make sure the "$" is inside the quotes, and don't use single quotes when you want expansion. "if If" will not work. And, some proper indentation will make the code more readable and safer.
# 4  
Old 08-04-2014
Code:
input file:
H|123|45555|4555|FILE1.DAT   WHITE SPACE   |33|444
D|123|45555|4555|45T55|33|444
D|FF|676|YTYYU|878
T|GFG|GFGF|7879|
H|123|45555|4555|FILE2.DAT   WHITE SPACE   |33|444
D|123|45555|4555|45T55|33|444
D|FF|676|YTYYU|878
T|GFG|GFGF|7879|
H|123|45555|4555|FILE3.DAT   WHITE SPACE   |33|444
D|123|45555|4555|45T55|33|444
D|FF|676|YTYYU|878
T|GFG|GFGF|7879|3434

output Files:
File1.dat
H|123|45555|4555|FILE1.DAT   WHITE SPACE   |33|444
D|123|45555|4555|45T55|33|444
D|FF|676|YTYYU|878
T|GFG|GFGF|7879|223
File2.dat
H|123|45555|4555|FILE2.DAT   WHITE SPACE   |33|444
D|123|45555|4555|45T55|33|444
D|FF|676|YTYYU|878
T|GFG|GFGF|7879|
H|123|45555|4555|FILE3.DAT   WHITE SPACE   |33|444
D|123|45555|4555|45T55|33|444
D|FF|676|YTYYU|878
T|GFG|GFGF|7879|3434

program flow
We have to split the input file into multiple new file as shown in output file file1.dat,file2.dat,file3.dat

loop through the input file
create a new file (file name as present in header) as present in table header.
New file should contain all the lines starting from a table header H to Table trailer
H ....T>File1.dat when it encounter next H ....T>file2.dat and so on
Let me know if need more info
input file should split number of H present in file
# 5  
Old 08-04-2014
Your file names don't match the strings in the sample file. Try
Code:
awk '$1=="H" {FN=substr($5, 1, index($5," ")-1)} {print > FN}' FS="|" file
cf FIL*
FILE1.DAT:
H|123|45555|4555|FILE1.DAT   WHITE SPACE   |33|444
D|123|45555|4555|45T55|33|444
D|FF|676|YTYYU|878
T|GFG|GFGF|7879|
FILE2.DAT:
H|123|45555|4555|FILE2.DAT   WHITE SPACE   |33|444
D|123|45555|4555|45T55|33|444
D|FF|676|YTYYU|878
T|GFG|GFGF|7879|
FILE3.DAT:
H|123|45555|4555|FILE3.DAT   WHITE SPACE   |33|444
D|123|45555|4555|45T55|33|444
D|FF|676|YTYYU|878
T|GFG|GFGF|7879|3434

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Output file name and file contents of multiple files to a single file

I am trying to consolidate multiple information files (<hostname>.Linux.nfslist) into one file so that I can import it into Excel. I can get the file contents with cat *Linux.nfslist >> nfslist.txt. I need each line prefaced with the hostname. I am unsure how to do this. --- Post updated at... (5 Replies)
Discussion started by: Kentlee65
5 Replies

2. Shell Programming and Scripting

Splitting a single xml file into multiple xml files

Hi, I'm having a xml file with multiple xml header. so i want to split the file into multiple files. Sample.xml consists multiple headers so how can we split these multiple headers into multiple files in unix. eg : <?xml version="1.0" encoding="UTF-8"?> <ml:individual... (3 Replies)
Discussion started by: Narendra921631
3 Replies

3. Shell Programming and Scripting

Splitting a single file to multiple files

Hi Friends , Please guide me with the code to extract multiple files from one file . The File Looks like ( Suppose a file has 2 tables list ,column length may vary ) H..- > File Header.... H....- >Table 1 Header.... D....- > Table 1 Data.... T....- >Table 1 Trailer.... H..-> Table 2... (1 Reply)
Discussion started by: AspiringD
1 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. Shell Programming and Scripting

Need help splitting huge single record file

I was given a data file that I need to split into multiple lines/records based on a key word. The problem is that it is 2.5GB or bigger and everything I try in perl or sed causes a Segmentation fault. Can someone give me some other ideas. The data is of the form:... (5 Replies)
Discussion started by: leolson
5 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

Splitting single file into n files

Hi all, I am new to scripting and I have a requirement we have source file as HEADER 01.10.2010 14:32:37 NAYA TA0022 TA0000 20000001;20060612;99991231;K4;02;3 20000008;20080624;99991231;K4;02;3 20000026;19840724;99991231;KK;01;3 20000027;19840724;99991231;KK;01;3... (6 Replies)
Discussion started by: srk409
6 Replies

8. Shell Programming and Scripting

Splitting & reformating a single file

I have a bif text file with the following format: d1_03 fr:23 d1_03 fr:56 d1_03 fr:67 d1_03 fr:78 d1_01 fr:35 d1_01 fr:29 d1_01 fr:45 d2_09 fr:34 d2_09 fr:78 d3_98 fr:90 d3_98 fr:104 d3_98 fr:360 I have like thousands of such lines I want to reformat this file based on column 1... (3 Replies)
Discussion started by: Lucky Ali
3 Replies

9. UNIX for Advanced & Expert Users

Splitting the single csv file

Hi, I have a requiement where in i will get a single file but there will be mutiple headers. Suppose say for eg: Header1 Data... Data... Header2 Data.. Data.. Header3 Data.. Data.. I want to split each with the corresponding data into a single file. Please let me know how... (1 Reply)
Discussion started by: weknowd
1 Replies

10. Shell Programming and Scripting

Splitting a single line into multiple lines

I have a case where, I need to look into a file. Go to each line of the file, find the length of the line, if the length of the line is more than 75 chars, I need to split the line into multiple lines of 75chars max. If the length of the line is less than 75, we need not do anything. So at the... (4 Replies)
Discussion started by: thanuman
4 Replies
Login or Register to Ask a Question