Split a File Based on Line Pattern

csplit command can be used to split a file on line pattern. Below is a sample XML file and csplit command to create one file for each folder element:

<folder>
	folder1 - line 1
	folder1 - line 2
</folder>

<folder>
	folder2 - line 1
</folder>

<folder>
	folder3 - line 1
</folder>

<folder>
	folder4 - line 1
</folder>

csplit.exe --prefix=folder --suffix-format=%05d.txt --elide-empty-files folder.txt '/[:space:]*<\/folder>/+1' {*}
  • –prefix=folder: files created will have the filename prefix ‘folder’
  • –suffix-format=%05d.txt: files created will have suffix five digits (%05d) and ‘.txt’ (e.g. folder00000.txt)
  • –elide-empty-files: remove empty files (an empty file is created if there is an empty line after the last folder element)
  • –elide-empty-files: remove empty files (an empty file is created if there is an empty line after the last folder element)
  • ‘/[:space:]*/+1’: regular expression to match “zero or more spaces followed by ‘. +1 is used to output the matching line (i.e., also)
  • csplit - split file based on line pattern
    csplit - split file based on line pattern
    Advertisements

One thought on “Split a File Based on Line Pattern

  1. I Think that blog post, “Split a File Based on Line
    Pattern Satish Devarapalli” was really good!
    I personallycouldn’t agree along with you even more! At last looks like I actuallycame across a web-site well worth reading through. Thank you, Leanne

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s