|
| |||||||||
| Tags: shell script |
![]() |
| | Thread Tools | Search this Thread |
|
#1
| |||
| |||
| 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 |
|
#2
| ||||
| ||||
| 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 |
|
#3
| |||
| |||
| 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. |
|
#4
| ||||
| ||||
| 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. |
![]() |
|
| Thread Tools | Search this Thread |
| |
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 |