fork download
  1. #!/bin/bash
  2.  
  3. # ideone boilerplate: run in temp dir
  4. t=$(mktemp -d -t ideone.XXXXXXXX) || exit
  5. trap 'rm -rf "$t"' ERR EXIT
  6. cd "$t"
  7.  
  8. cat <<\: >MyInitialTextFile.txt
  9. <p><nsup></nsup> <b>Abc 1:2<sup>varied text
  10. <p><nsup></nsup> <b>Abc 1:2<sup>varied text
  11. <p><nsup></nsup> <b>Abc 1:3<sup>varied text
  12. <p><nsup></nsup> <b>Abc 1:4<sup>varied text
  13. <p><nsup></nsup> <b>Abc 1:4<sup>varied text
  14. <p><nsup></nsup> <b>Abc 1:4<sup>varied text
  15. :
  16.  
  17. awk '/^<p><nsup><\/nsup> <b>/ {
  18. str = substr($0, 21); split(str, n, /<sup>/);
  19. if (n[1] != id) {
  20. if (d) { printf "\n" >d; close(d) }
  21. d = n[1] ".txt"
  22. id = n[1]
  23. sep = ""
  24. }
  25. printf "%s%s", sep, $0 >d
  26. sep = " "
  27. }
  28. END { if (d) printf "\n" >d }' MyInitialTextFile.txt
  29.  
  30. tail *
Success #stdin #stdout 0.01s 5572KB
stdin
Standard input is empty
stdout
==> Abc 1:2.txt <==
<p><nsup></nsup> <b>Abc 1:2<sup>varied text     <p><nsup></nsup> <b>Abc 1:2<sup>varied text

==> Abc 1:3.txt <==
<p><nsup></nsup> <b>Abc 1:3<sup>varied text

==> Abc 1:4.txt <==
<p><nsup></nsup> <b>Abc 1:4<sup>varied text     <p><nsup></nsup> <b>Abc 1:4<sup>varied text     <p><nsup></nsup> <b>Abc 1:4<sup>varied text

==> MyInitialTextFile.txt <==
<p><nsup></nsup> <b>Abc 1:2<sup>varied text
<p><nsup></nsup> <b>Abc 1:2<sup>varied text
<p><nsup></nsup> <b>Abc 1:3<sup>varied text
<p><nsup></nsup> <b>Abc 1:4<sup>varied text
<p><nsup></nsup> <b>Abc 1:4<sup>varied text
<p><nsup></nsup> <b>Abc 1:4<sup>varied text