Results 1 to 3 of 3

Thread: Bash: How to split a file into multiple files according to defined rules

  1. #1
    Join Date
    Mar 2007
    Beans
    680

    Bash: How to split a file into multiple files according to defined rules

    Hi,
    using Ubuntu 12.04 I have the following text file that I get as my input:
    Code:
    Section1
    col1	col2 (many columns)
    100	500
    101	500	
    102	500
    (many rows)
    
    Section2
    col1	col2 (many columns)
    400	400
    401	400	
    402	400
    (many rows)
    
    Section3
    col1	col2 (many columns)
    700	600
    801	700	
    902	800
    (many rows)
    How to brake this file into 3 files according to the "Section" line?
    From Section1 to Section2 should be saved in file file1.
    Code:
    Section1
    col1	col2 (many columns)
    100	500
    101	500	
    102	500
    (many rows)
    From Section2 to Section3 should be saved in file2.
    Code:
    Section2
    col1	col2 (many columns)
    400	400
    401	400	
    402	400
    (many rows)
    and from Section 3 to end of file should be saved in file3.
    Code:
    Section3
    col1	col2 (many columns)
    700	600
    801	700	
    902	800
    (many rows)
    How to split one file into multiple files according to above rules?
    Thanks

  2. #2
    Join Date
    Apr 2012
    Beans
    7,256

    Re: Bash: How to split a file into multiple files according to defined rules

    Are the sections separated by empty lines, exactly as shown in your example? if so then you may be able to use awk with 'empty line' as a record separator, e.g.

    Code:
    awk 'BEGIN {RS=""}; {print > ""$1".txt"}' yourfile
    will produce files Section1.txt, Section2.txt etc. (i.e. using the SectionN header as the output file name) although obviously you cold change that to any sequential naming like file1, file2 with some string processing

    Code:
    $ awk 'BEGIN {RS=""}; {print > ""$1".txt"}' sections.txt
    $
    $ cat Section2.txt
    Section2
    col1    col2 (many columns)
    400     400
    401     400
    402     400
    (many rows)

  3. #3
    Join Date
    Feb 2007
    Location
    West Hills CA
    Beans
    10,044
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Bash: How to split a file into multiple files according to defined rules

    cat will concatenate files. tac will work in reverse. Although I have not used it, it may do what you want. It reads the file backwards, splitting out into separate files when it encounters a keyword.

    Code:
    man tac
    tac --help
    tac -r "section" myfilewithmanysectionsandcolumnsandrows.txt
    As steeldriver suggests, awk should do it as well as mawk, gawk, sed, perl, split and perhaps a few other file manipulation tools.

    A google search using "split file using bash" brings up:

    http://stackoverflow.com/questions/3...ng-bash-script
    http://stackoverflow.com/questions/2...arge-text-file
    http://stackoverflow.com/questions/6...ers-in-columns

    Let us know what your final script looks like.
    Last edited by tgalati4; August 2nd, 2013 at 09:16 PM.
    -------------------------------------
    Oooh Shiny: PopularPages

    Unumquodque potest reparantur. Patientia sit virtus.

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
  •