Visit Our UNIX and Linux User Community


How can i break a text file into parts that occur between a specific pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How can i break a text file into parts that occur between a specific pattern
# 1  
Old 11-11-2009
How can i break a text file into parts that occur between a specific pattern

How can i break a text file into parts that occur between a specific pattern?

I have text file having various xml many tags like which starts with the tag "<?xml version="1.0" encoding="utf-8"?>" . I have to break the whole file into several xmls by looking for the above pattern.

All the tags occuring in between the above pattern will form a seperate xml to be created.


Please Help,

Thanks in advance Smilie
# 2  
Old 11-11-2009
Pls post the input data and desired output expecting.
# 3  
Old 11-11-2009
Inpurt is >>
<fileName>Name1<fileName><?xml version="1.0" encoding="utf-8"?><Name> Abhinav</Name>
<Age>12</Age>
<fileName>Name2<fileName><?xml version="1.0" encoding="utf-8"?><Name> Abhinav</Name>
<Age>15></Age>

Desired output will be >>

Two xml files with the name Name1 and Name2 as given in the input and having contents as coming in the input...

1st file
<?xml version="1.0" encoding="utf-8"?><Name> Abhinav</Name>
<Age>12</Age>

2nd file
<?xml version="1.0" encoding="utf-8"?><Name> Abhinav</Name>
<Age>15></Age>


Actually we can search for the file Name tag >>>

i am new to UNIX so helpless
# 4  
Old 11-11-2009
something like this :
Code:
awk '/<fileName>/ {co++; print >> "File_"co ;next} { print >> "File_"co }'  File_name.txt

# 5  
Old 11-11-2009
In your reply File_Name.txt will be the input file or the output

---------- Post updated at 08:13 AM ---------- Previous update was at 08:00 AM ----------

Hey panyam ... that was qute useful..
thanks,

BUt i need the names provided in the fileName tag as the names of the different files created...

ex: Name1.xml and Name2.xml
# 6  
Old 11-11-2009
Try this:

Code:
awk -F"[<>]" '/fileName/{f=$3".xml"}{print > f}' file

# 7  
Old 11-11-2009
thanks franklin....

Great help...

I being new dont understand the script you wrote...


Can you please explain a bit... or send a mail .. my mail id is

Last edited by Franklin52; 11-11-2009 at 11:46 AM.. Reason: email address removed, no answer by email

Previous Thread | Next Thread
Test Your Knowledge in Computers #326
Difficulty: Medium
Time on Computers is commonly referred to as GNU Time.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Rename text file with a specific pattern in directory

I am trying to rename all text files in a directory that match a pattern. The current command below seems to be using the directory path in the name and since it already exists, will not do the rename. I am not sure what I am missing? Thank you :). Files to rename in... (3 Replies)
Discussion started by: cmccabe
3 Replies

2. Shell Programming and Scripting

Match all lines in file where specific text pattern is less than

In the below file I am trying to grep or similar, all lines where only AF= is less than 0.4.. Thank you :). grep grep "AF=" ,+ .4 file file 12 112036782 . T C 34.0248 PASS ... (3 Replies)
Discussion started by: cmccabe
3 Replies

3. Shell Programming and Scripting

Extract specific line in an html file starting and ending with specific pattern to a text file

Hi This is my first post and I'm just a beginner. So please be nice to me. I have a couple of html files where a pattern beginning with "http://www.site.com" and ending with "/resource.dat" is present on every 241st line. How do I extract this to a new text file? I have tried sed -n 241,241p... (13 Replies)
Discussion started by: dejavo
13 Replies

4. Shell Programming and Scripting

Delete specific parts in a .txt file

Hi all, I desperately need a small script which deletes everything in a particular .txt file when "Abs = {" appears till "},", and also when "B-1 = {" appears till "}," I would like all the text in between of those instances to be deleted, however, other text to be unedited (kept as it is).... (12 Replies)
Discussion started by: c_lady
12 Replies

5. UNIX for Dummies Questions & Answers

Help with deleting some parts from text file

Hi all, I have a fat file which contains something like this: ************************************************ blahblahblah blahblahblah Myobject1 HOME ( homecontents01 ( some junk; ) home contents02( some junk; ) ... (7 Replies)
Discussion started by: newboy
7 Replies

6. Shell Programming and Scripting

how do I break line in a file when a pattern is matched ?

Hi All, I am stuck for quite sometime now. Below is a line in my file - GS|ED|001075|001081|20110626|1806|100803|X|004010ST|130|100803001 This line occurs only once and it is the second line. I have to break this line into two lines from ST (bold) such that it looks like -... (5 Replies)
Discussion started by: ihussain
5 Replies

7. Shell Programming and Scripting

[ask]break line number into several parts

hlow all, i have file with wc -l file.txt is 3412112 line number so I want to break these files into several parts with assumsi line 1-1000000 will be create part1.txt and 1000001-2000000 will create part2.txt and 2000001-3000000 will create part3.txt and 3000001-3412112 will create... (5 Replies)
Discussion started by: zvtral
5 Replies

8. Shell Programming and Scripting

Help with remove last text of a file that have specific pattern

Input file matrix-remodelling_associated_8_ aurora_interacting_1_ L20 von_factor_A_domain_1 ATP_containing_3B_ . . Output file matrix-remodelling_associated_8 aurora_interacting_1 L20 von_factor_A_domain_1 ATP_containing_3B . . (3 Replies)
Discussion started by: perl_beginner
3 Replies

9. UNIX for Dummies Questions & Answers

Help with copying specific parts of a file structure

Hello. I need help with copying part of a file structure to another directory while still keeping the structure. For example look below: ../folder1/sub1/txt.txt ../folder1/sub2/pic.png ../folder2/sub1/pic.png ../folder2/sub2/txt.txt So in this I would like to copy only the directories and... (3 Replies)
Discussion started by: the
3 Replies

10. UNIX for Dummies Questions & Answers

to break a file into 2 files after matching a pattern.

Hi, i need to break a file into 2 files afetr matching a pattern for ex. there is a fil, file .txt which contains here i need to look for mat $ demon if it matches then i need to transfer the data into another file till the line in which a "d6s" comes,and i have to delete tat line... (3 Replies)
Discussion started by: manit
3 Replies

Featured Tech Videos