Results 1 to 4 of 4

Thread: Cutting a file into several files using shell script

  1. #1
    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

  2. #2
    Join Date
    Feb 2008
    Posts
    1,852

    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. #3
    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.

  4. #4
    Join Date
    Feb 2008
    Posts
    1,852

    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.

Similar Threads

  1. Shell script to add the extension to doc and pdf files
    By Tailor in forum Software Development
    Replies: 4
    Last Post: 24-02-2010, 10:51 PM
  2. Shell script with sed
    By Ameeryan in forum Software Development
    Replies: 5
    Last Post: 23-12-2009, 02:29 PM
  3. Parsing the file in Unix shell script
    By Macarenas in forum Software Development
    Replies: 3
    Last Post: 23-11-2009, 11:32 AM
  4. Unix Shell Script
    By BlackSunReyes in forum Software Development
    Replies: 3
    Last Post: 12-08-2008, 01:57 PM
  5. Script to close open files in file server
    By Gustavo in forum Windows Server Help
    Replies: 2
    Last Post: 22-05-2008, 06:59 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,261,025.78823 seconds with 17 queries