Register
It is currently Sat Oct 25, 2014 2:45 am

Splitting a Single File and Generating Multiple files


All times are UTC - 6 hours


Post new topic Reply to topic  [ 2 posts ] 
Author Message
 PostPosted: Wed Oct 03, 2012 6:26 am   

Joined: Wed Oct 03, 2012 6:13 am
Posts: 1
Hi All,

I am tryting to Spilt a File into number of files depending on the number of ouucrances of the Unique Tag.

Code:
<ns:Address>
<ns:StreetNumber>XXXXX</ns:StreetNumber>
<ns:CityName>XXXXXX</ns:CityName>
<ns:StateProvNamePA</ns:StateProvName>
<ns:ZIP>XXXX</ns:ZIP>
<ns:AddressID>
<ns:ID SystemCode="CODE">10</ns:ID>
</ns:AddressID>
</ns:Address>
<ns:Address>
<ns:StreetNumber>XXXXX</ns:StreetNumber>
<ns:CityName>XXXXXX</ns:CityName>
<ns:StateProvNamePA</ns:StateProvName>
<ns:ZIP>XXXX</ns:ZIP>
<ns:AddressID>
<ns:ID SystemCode="CODE">10</ns:ID>
</ns:AddressID>
</ns:Address>
<ns:Address>
<ns:StreetNumber>XXXXX</ns:StreetNumber>
<ns:CityName>XXXXXX</ns:CityName>
<ns:StateProvNamePA</ns:StateProvName>
<ns:ZIP>XXXX</ns:ZIP>
<ns:AddressID>
<ns:ID SystemCode="CODE">10</ns:ID>
</ns:AddressID>
</ns:Address>
<ns:Address>
<ns:StreetNumber>XXXXX</ns:StreetNumber>
<ns:CityName>XXXXXX</ns:CityName>
<ns:StateProvNamePA</ns:StateProvName>
<ns:ZIP>XXXX</ns:ZIP>
<ns:AddressID>
<ns:ID SystemCode="CODE">10</ns:ID>
</ns:AddressID>
</ns:Address>


Like this I have 1200 entries of Files. I want to Extact the data between the <ns:Address> and </ns:Address> and create a new file for every ten such entires.

output:
Address 1 to Address 10 --> ADDRESS_10.TXT
Address 11 to Address 20 --> ADDRESS_20.TXT

Can anyone help me with this. I am new to UNIX so any kind of help is helpful

Thanks,
Venkat.


Top
 Profile  
 PostPosted: Fri Oct 12, 2012 8:18 am   

Joined: Tue Sep 27, 2011 12:45 pm
Posts: 8
Here is one way to do it.

Code:
#!/bin/bash

function _fileSplitter ()
{
InputFile=$1
lineNumbers=(`egrep -n "<ns:Address>|</ns:Address>" $InputFile |sed -e '$!N;s/\n/ /' -e 's/<ns:Address> //' |awk -F':' '{print $1","$2}'`)
numEntries=${#lineNumbers[@]}
c=0

while [ "$c" -le "$numEntries" ]
do
        if [ "$c" -eq "0" ]
        then
                firstLine=`echo "${lineNumbers[$c]}" |awk -F',' '{print $1}'`
        else
                c=`expr $c + 1`
                firstLine=`echo "${lineNumbers[$c]}" |awk -F',' '{print $1}'`
        fi

        firstCount="$c"
        c=`expr $c + 10`
        lastLine=`echo "${lineNumbers[$c]}" |awk -F',' '{print $NF}'`

        sed -n ''${firstLine}','${lastLine}'p' $InputFile > ${InputFile}_entries_${firstCount}-${c}.txt
done
}

_fileSplitter $1 2> /dev/null


Just pass the script the file containing your entries. There isn't any error checking or anything fancy. You could always add that stuff later if you wanted to.


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Google [Bot] and 7 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP