File Separation


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting File Separation
# 1  
Old 04-01-2015
File Separation

Hi I have an XML File with default header($ lines of data) and default tail (Two Lines) and the body has occurrence of start<Folder> and ends with </Folder>.

Now i want to split each occurrence i mean each start and end in to separate files with header and tail.

For example:

Code:
header line 1 
header line 2
header line 3
header line 4
<folder>
Hi this is nithin
<\folder>
<folder>
Hi this is sarath
<\folder>
<folder>
Hi this is barath
<\Folder>
tail line 1
tail line 2

Output Should be
file 1
-----------------------------------------------------
Code:
header line 1 
header line 2
header line 3
header line 4
<folder>
Hi this is nithin
<\folder>
tail line 1
tail line 2

------------------------------------------------------------------
file 2
----------------------------------------------------------------

Code:
header line 1 
header line 2
header line 3
header line 4
<folder>
Hi this is sarath
<\folder>
tail line 1
tail line 2

--------------------------------------------------------------------
file 3
----------------------------------------------------------------
Code:
header line 1 
header line 2
header line 3
header line 4
<folder>
Hi this is barath
<\folder>
tail line 1
tail line 2


Last edited by Don Cragun; 04-01-2015 at 11:26 PM.. Reason: Add CODE and ICODE tags.
# 2  
Old 04-01-2015
What's been tried so far and where exactly are you stuck?
# 3  
Old 04-01-2015
so far i tried to get following but it slits only one entry

Code:
sed -n '/<FOLDER NAME="Prudhvi"/,/<\/FOLDER>/p;/<\/FOLDER>/q' test.xml


Last edited by Don Cragun; 04-01-2015 at 11:27 PM.. Reason: Add CODE tags.
# 4  
Old 04-02-2015
Sure you have back slashes and upper case F in the closing tags? Be more careful when specifying!

Try
Code:
awk     'BEGIN          {CLHD=1}
         /<folder>/     {CLHD=0; CLTR=0; FN++}
         CLHD           {HD=HD?HD ORS $0:$0; next}
         CLTR           {TR=TR?TR ORS $0:$0; next}
                        {P[FN]=P[FN]?P[FN] ORS $0:$0}
         /<\\[Ff]older>/        {CLTR=1}
         END            {for (p in P) print HD ORS P[p] ORS TR > "FILE" p}
        ' file

# 5  
Old 04-02-2015
Hi RudiC, 1000 thanks to you, the issue is resolved and you seem to be perfect ! Sorry next time will be careful with case sensitive issue, it was a typo

---------- Post updated at 08:19 AM ---------- Previous update was at 08:10 AM ----------

Hi Rudic, one last question please .....
my code tag starts with something like this and ends as below

Code:
<FOLDER NAME="nithin">
*****
</FOLDER>

So can you help me modify the code to just pass $1 for instead of 'nithin', so it should just grab the piece which i really wanted . like i may change the developer and the code should return that particular code from whole xml file. in the above example it should drag the code between the tags mentioned

---------- Post updated at 08:20 AM ---------- Previous update was at 08:19 AM ----------

<FOLDER NAME="nithin" GROUP="" OWNER="girish" SHARED="NOTSHARED"


this is how its looks like in the file
# 6  
Old 04-02-2015
Sorry, I don't understand. If you want a different intro tag as indicated, write all upper case FOLDER and remove the "larger than" sign at the end.
# 7  
Old 04-02-2015
Hello Rudic, This is the actual code i am working on, Please shed some light on this issue, it will be great help

Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE POWERMART SYSTEM "powrmart.dtd">
<POWERMART CREATION_DATE="04/01/20151">
<REPOSITORY NAME="INFA""Oracle">
<FOLDER NAME="zvronges" GROUP=""r"
<TABLEATTRIBUTE NAME ="Sql Query" VALUE =""/>
</FOLDER>
<FOLDER NAME="AAAETL_UTILITY_SH" GROUP="" OWNER="Administrator"
PRECISION ="30" SCALE ="0"/>
</FOLDER>
<FOLDER NAME="zvsandineni" GROUP="" OWNER="Administrator">
<SOURCE BUSINESSNAME =">
</FOLDER>
</REPOSITORY>
</POWERMART>

These are the outputs i am expecting, This should give different files based on the folder name, if the
folder name starts with "Z" , this is going to separate file, if not we shall ignore the folders



File_1
----------

Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE POWERMART SYSTEM "powrmart.dtd">
<POWERMART CREATION_DATE="04/01/20151">
<REPOSITORY NAME="INFA""Oracle">
<FOLDER NAME="zvronges" GROUP=""r"
<TABLEATTRIBUTE NAME ="Sql Query" VALUE =""/>
</FOLDER>
</REPOSITORY>
</POWERMART>

File_2
----------

Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE POWERMART SYSTEM "powrmart.dtd">
<POWERMART CREATION_DATE="04/01/20151">
<REPOSITORY NAME="INFA""Oracle">
<FOLDER NAME="zvsandineni" GROUP="" OWNER="Administrator">
<SOURCE BUSINESSNAME =">
</FOLDER>
</REPOSITORY>
</POWERMART>


Last edited by jim mcnamara; 04-02-2015 at 10:30 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sum up formatted numbers with comma separation

I need to sum up the values in field nr 5 in a data file that contains some file listing. The 5th field denotes the size of each file and following are some sample values. 1,775,947,633 4,738 7,300 16,610 15,279 0 0 I tried the following code in a shell script. awk '{sum+=$5} END{print... (4 Replies)
Discussion started by: krishmaths
4 Replies

2. Shell Programming and Scripting

Read xml file till script finds separation and run again for next input and so on

Hi All, I have one query, I managed to run script with user inputs through command line or with 1 file. But I need to read a txt file/xml file in which user can mention multiple sets of answers and script should run for each set till it reach the EOF. Thanks in advance for example, the file... (3 Replies)
Discussion started by: rv_champ
3 Replies

3. Shell Programming and Scripting

How to determine column separation format?

Hi there, say I have a line with multiple columns but with different separation formats: spaces, tabs.. Is it possible to have AWK print the separation format between each column? (10 Replies)
Discussion started by: la2015
10 Replies

4. Shell Programming and Scripting

metacharacters separation

I have prepared a script to submit a string in a txt file. However there are somethings that I have to check before submitting the string in the txt file. One of those checks is to determine whether the string entered contains any metacharacters. I have tried sth like; echo "string" | grep -v ... (3 Replies)
Discussion started by: ozum
3 Replies

5. Shell Programming and Scripting

nawk -- separation of records on basis of number of fields

A file file1.txt exists having records like The delimiter being "|" X|_|Y|_|Z|_| (number of fields 7) A|_|B|_| (number of fields 5) X|_|Z|_|H|_| (number of fields 7) A|_|D|_|S|_| (number of... (4 Replies)
Discussion started by: centurion_13
4 Replies

6. Shell Programming and Scripting

Help need to write a script on column separation for syslog output in perl

HI Pros, I have a issue.I need to write a script to parse the logs got from syslog server and update the same in my database.I need the following output.I donot know perl and I heard it very easy to write in perl I have the sample log I need each column seperated by commas and all equals... (0 Replies)
Discussion started by: iron_michael86
0 Replies

7. Shell Programming and Scripting

Unwanted field separation in awk

Hi everyone, My problem is strange, I cannot think of why this is happening. I have a set of data that looks like this: Although it does not look it, the fields are tab delimited. I have made sure of this, and awk does recognize them as such. However, it divides what I would expect... (2 Replies)
Discussion started by: ccox85
2 Replies

8. Shell Programming and Scripting

Joining three lines with comma separation

I have a file that looks like this: G. KRESSLAR 9618 W. APPALOOSA DRIVE SUN CITY, AZ 85373 SHIRLEY ALLEN 7272 W. VIA MONTOYA DRIVE GLENDALE, AZ 85310 LOUIS VALDEZ 244441 N. 86TH AVENUE PEORIA, AZ 85383 DONNA NEWBON 3231 W. DENTON #D PHOENIX, AZ 85017 SARAH WILSON 6534 W. PALO... (3 Replies)
Discussion started by: BCarlson
3 Replies
Login or Register to Ask a Question