Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read SiteMap

Tags:

Sponsored Links



Cutting a file into several files using shell script

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 23-10-2009
Member
 
Join Date: Aug 2009
Posts: 155
Cutting a file into several files using shell script

I have a file person.txt in which there are below lines:

<person>
<id>158</ id>
<name>foo</name>
<city>Paris</city>
</person>

<person>
<id>19,521 </id>
<name>Mama</name>
<city>Mumbai</city>
</person>

<person>
<id> 75,142 </id>
<name>Kaka</name>
<city>Kolkata</city>
</person>

the <id>xxxxx</id> can be any number but there is "<id>xxxxx</id>" = N times. I would split the file into N files: File "person158" which is:
<id>158</id>
<name>foo</name>
<city>Paris</city>

File "person19521" which is
<id>19,521 </id>
<name>Mama</name>
<city>Mumbai</city>
and so on ...

I would like to create .txt (person158.txt, person19521.txt ...) in which
are the corresponding information. In "person158.txt" there will

<id>158</id>
<name>foo</name>
<city>Paris</city>

in "person19521.txt" there will

<id>19,521 </id>
<name>Mama</name>
<city>Mumbai</city>

and so on.

Therefore in the initial file search for <id>xxxxx</id> with these names create "personxxxxx.txt" and "personxxxxx.txt" to copy the lines located in the main file.

I am beginner in shell
Reply With Quote
  #2  
Old 23-10-2009
MindSpace's Avatar
Member
 
Join Date: Feb 2008
Posts: 1,832
re: Cutting a file into several files using shell script

Seen that your input file is very similar to XML, the ideal would be to use an XML parser ...

but otherwise in quick & dirty (and assuming that the entire file is structured like your example in carriage returns and coughed) you can go out with a pretty awk:

Code:
awk 'match($0, "<id>.*</id>" ) {a = $0;
gsub("<id>", "", a);
gsub("</id>", "", a);
system("echo \""$0"\" >> person"a".txt" );
getline;
system("echo \""$0"\" >> person"a".txt" );
getline;
system("echo \""$0"\" >> person"a".txt" )
}' t
Reply With Quote
  #3  
Old 23-10-2009
Member
 
Join Date: Aug 2009
Posts: 155
re: Cutting a file into several files using shell script

Firstly, thank you for your answer, but I actually want N files and not just 3 files. So I need to do a loop and this is where I'm stuck. On the other hand, you spoke of "parse xml", can you give me some examples please? Because this will be the result of my job.
Reply With Quote
  #4  
Old 23-10-2009
MindSpace's Avatar
Member
 
Join Date: Feb 2008
Posts: 1,832
re: Cutting a file into several files using shell script

Did you at least tried my code? Because it works regardless of the number of files (there are 3 lines, three lines corresponding to id, name and city). And if not for the xml parsing, google is your friend. But you will have to drop the language type perl, python, java, etc.
Reply With Quote
Reply

  TechArena Community > Software > Software Development


Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Cutting a file into several files using shell script"
Thread Thread Starter Forum Replies Last Post
Shell script to add the extension to doc and pdf files Tailor Software Development 4 24-02-2010 10:51 PM
Shell script with sed Ameeryan Software Development 5 23-12-2009 02:29 PM
Parsing the file in Unix shell script Macarenas Software Development 3 23-11-2009 11:32 AM
how to get permission for Shell.Application in JScript in local files (file://...html)? Jeff Vista Help 1 24-02-2009 02:15 AM
Script to close open files in file server Gustavo Windows Server Help 2 22-05-2008 07:59 PM


All times are GMT +5.5. The time now is 11:18 AM.