I have file.txt which contains content that follows this sort of style:

Target 99C - HJ Shove vs CO + SB SQ
[0.02]JdJh, JsJh, JcJh, JsJd, JcJd, JcJs, AhKh, AdKd, AsKs, AcKc[/0.02], [30.30]KdKh, KsKh, KcKh, KsKd, KcKd, KcKs[/30.30], [31.52]QdQh, QsQh, QcQh, QsQd, QcQd, QcQs[/31.52], [61.57]AdKh, AsKh, AcKh, AhKd, AsKd, AcKd, AhKs, AdKs, AcKs, AhKc, AdKc, AsKc[/61.57]

Target 100A - HJ Call vs BU + SB SQ
[0.01]Ah5h, Ad5d, As5s, Ac5c[/0.01], [0.06]KhTh, KdTd, KsTs, KcTc[/0.06], [0.13]JhTh, JdTd, JsTs, JcTc[/0.13], [0.43]AdAh, AsAh, AcAh, AsAd, AcAd, AcAs[/0.43], [0.59]7d7h, 7s7h, 7c7h, 7s7d, 7c7d, 7c7s[/0.59], [0.77]AhJh, AdJd, AsJs, AcJc[/0.77], [2.44]5d5h, 5s5h, 5c5h, 5s5d, 5c5d, 5c5s[/2.44], [7.06]6h5h, 6d5d, 6s5s, 6c5c[/7.06], [7.21]QdQh, QsQh, QcQh, QsQd, QcQd, QcQs[/7.21], [7.52]5h4h, 5d4d, 5s4s, 5c4c[/7.52], [8.69]KhJh, KdJd, KsJs, KcJc[/8.69], [9.89]6d6h, 6s6h, 6c6h, 6s6d, 6c6d, 6c6s[/9.89], [11.61]8d8h, 8s8h, 8c8h, 8s8d, 8c8d, 8c8s[/11.61], [11.73]9d9h, 9s9h, 9c9h, 9s9d, 9c9d, 9c9s[/11.73], [18.21]JdJh, JsJh, JcJh, JsJd, JcJd, JcJs[/18.21], [24.60]TdTh, TsTh, TcTh, TsTd, TcTd, TcTs[/24.60], [29.64]KhQh, KdQd, KsQs, KcQc[/29.64], AhQh, AdQd, AsQs, AcQc

Target 100B - HJ 4B to 29bb vs BU + SB SQ
[0.07]Ah6h, Ad6d, As6s, Ac6c[/0.07], [0.24]AhTh, AdTd, AsTs, AcTc[/0.24], [5.89]TdTh, TsTh, TcTh, TsTd, TcTd, TcTs[/5.89], [7.59]Kh9h, Kd9d, Ks9s, Kc9c[/7.59], [10.18]Ah3h, Ad3d, As3s, Ac3c[/10.18], [12.39]AdQh, AsQh, AcQh, AhQd, AsQd, AcQd, AhQs, AdQs, AcQs, AhQc, AdQc, AsQc[/12.39], [22.67]Kh7h, Kd7d, Ks7s, Kc7c[/22.67], [25.08]Kh6h, Kd6d, Ks6s, Kc6c[/25.08], [25.74]Kh8h, Kd8d, Ks8s, Kc8c[/25.74], [28.31]KhTh, KdTd, KsTs, KcTc[/28.31], [48.85]AdKh, AsKh, AcKh, AhKd, AsKd, AcKd, AhKs, AdKs, AcKs, AhKc, AdKc, AsKc[/48.85], [49.25]Ah5h, Ad5d, As5s, Ac5c[/49.25], [55.40]QdQh, QsQh, QcQh, QsQd, QcQd, QcQs[/55.40], [61.48]JdJh, JsJh, JcJh, JsJd, JcJd, JcJs[/61.48], [91.31]KhJh, KdJd, KsJs, KcJc[/91.31], [96.32]AhJh, AdJd, AsJs, AcJc[/96.32], [99.57]AdAh, AsAh, AcAh, AsAd, AcAd, AcAs[/99.57], [99.89]AhKh, AdKd, AsKs, AcKc[/99.89], [99.99]KdKh, KsKh, KcKh, KsKd, KcKd, KcKs[/99.99]

Target 100C - HJ Shove vs BU + SB SQ
[0.01]KdKh, KsKh, KcKh, KsKd, KcKd, KcKs[/0.01], [0.11]AhKh, AdKd, AsKs, AcKc[/0.11], [20.31]JdJh, JsJh, JcJh, JsJd, JcJd, JcJs[/20.31], [37.39]QdQh, QsQh, QcQh, QsQd, QcQd, QcQs[/37.39], [51.15]AdKh, AsKh, AcKh, AhKd, AsKd, AcKd, AhKs, AdKs, AcKs, AhKc, AdKc, AsKc[/51.15]

Target 101A - HJ Call vs CO + BU SQ
[0.08]5h4h, 5d4d, 5s4s, 5c4c[/0.08], [0.13]6h5h, 6d5d, 6s5s, 6c5c[/0.13], [0.56]TdTh, TsTh, TcTh, TsTd, TcTd, TcTs[/0.56], [0.71]QdQh, QsQh, QcQh, QsQd, QcQd, QcQs[/0.71], [1.75]8d8h, 8s8h, 8c8h, 8s8d, 8c8d, 8c8s[/1.75], [2.18]7d7h, 7s7h, 7c7h, 7s7d, 7c7d, 7c7s[/2.18], [3.68]9d9h, 9s9h, 9c9h, 9s9d, 9c9d, 9c9s[/3.68], [5.68]KhQh, KdQd, KsQs, KcQc[/5.68], [10.56]JdJh, JsJh, JcJh, JsJd, JcJd, JcJs[/10.56], [17.73]AhQh, AdQd, AsQs, AcQc[/17.73]

This file goes on for a while, containing a total of 300 'targets'. I want to split this file into ~300 files, so there will be a file named "HJ Call vs CO + BU SQ.txt" which contains all the content from "[0.08]" to "[/17.73]" inclusive.

I have looked at using csplit but this will not work as I'm pretty sure the names cannot be changed variably, I am considering grep & tee but again, I'm not sure this is the right approach. Is anyone familiar with a simple way to tackle this problem?

I am using WSL (Windows Subsystem for Linux).


  • 2
    Please edit your question and show us an actual example from your file. Make sure to include a few different cases. We need to know what the real format is in order to help. For example, can targets have more than one line of content? Can the word Target appear as the first word of a line that is not a header? Does the - appear in any lines other than the header? We need to know what is constant and what is variable in your file in order to help you parse it. Finally, please also tell us what operating system you are using, that is very relevant.
    – terdon
    Jul 14, 2021 at 12:46
  • Edited to include actual example, turns out the content is all just in one line which makes things a lot easier. I am using Windows 10 but I have Linux for Windows with Ubuntu and access to whatever VMs needed, but yeah - I have linux and windows CLI.
    – user481575
    Jul 14, 2021 at 12:51

2 Answers 2


You can use awk:

awk -F ' - ' '/^Target/{fn=$2} !/^Target|^$/{print >> fn}' file.txt
  • -F ' - ' will let awk split on dash with surrounded spaces.
  • /^Target/{fn=$2} Will set fn to the filename (second field)
  • !/^Target|^$/{print >> fn} Will print contents of lines that do not match ^Target or empty line (^$) to the filename.

As you use Windows, you might need to take care that your file has Unix line endings.

  • Thank you - this worked really well aside from a few small issues.
    – user481575
    Jul 14, 2021 at 13:20
  • @user481575 If one of the answers here solved your issue, please take a moment and accept it by clicking on the checkmark on the left. That is the best way to express your thanks on the Stack Exchange sites.
    – terdon
    Jul 14, 2021 at 13:35
  • One quick thing with @pLumo answer, the files when saved go along the format of: "BU Call CO Open and Call SB SQ" or "BB Call vs UTG 2.5bb" - this saves as a "5bb" file. Do you know how I could save all of these as just .txt's?
    – user481575
    Jul 14, 2021 at 13:58
  • they're probably windows text files, with line-endings of CR+LF. Unix text files have lines ending in LF only. Try adding -v RS='\r\n' to the awk command line. If you want windows style text output, add -v ORS='\r\n' too.
    – cas
    Jul 14, 2021 at 14:05
  • That is what I meant with the last sentence ;-) Thanks @cas
    – pLumo
    Jul 14, 2021 at 14:06

python along with the groupby method from the itertools module. The groupby method takes two inputs , an iterator (in our case a file handle on the input file) and a function that tells groupby what to group on (in our case a lambda function that looks for empty lines)

dos2unix file

python3 -c 'import sys, itertools as it
ifile = sys.argv[1]
with open(ifile) as f:
  for k,igrp in it.groupby(f, lambda x: x == "\n"):
    if not k:
      G = list(igrp)
      outfile = G.pop(0).rstrip().split("-")[1].lstrip()+".txt"
      with open(outfile,"w") as fp:
' file

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .