I have a log.txt file which contains 20k lines. Each line having numbers and there xyz plane coordinates and they are separated by sets. Sets can be identify by there names as ABC_1, ABC_2 and so on in the log.txt

From this log.txt file I want to segregate all sets data in to individual Text files, which should contain all data from set (eg. ABC_1).

My log.txt looks like below.

1, (xyz coordinates)
2, (xyz coordinates)
3, (xyz coordinates)

 .... Continue
101, (xyz coordinates)
102, (xyz coordinates)
103, (xyz coordinates)

 .... Continue
201, (xyz coordinates)
202, (xyz coordinates)
203, (xyz coordinates)

.... Continue
9991, (xyz coordinates)
9992, (xyz coordinates)
9993, (xyz coordinates)

.... Continue

I want to create a script which can give me 99 individual text files from a single log.txt file and which should be named as set name ABC_1.TXT, ABC_2.TXT ... to ABC_99.TXT from log.txt.

closed as unclear what you're asking by l0b0, Jeff Schaller, LinuxSecurityFreak, αғsнιη, Jesse_b May 5 '18 at 15:07

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • I assume (?) you want the contents of new files to be the intervening lines? Or empty files? – Jeff Schaller May 5 '18 at 12:04
  • With duch questions you should always provide both example input and example output. – Hauke Laging May 5 '18 at 13:23
  • Possible duplicate of text processing rows to columns for a block – αғsнιη May 5 '18 at 13:45
  • what else you need more than just awk '/^ABC_/{N++;next} {print >"ABC_"N".txt"}' infile (assuming the bocks are sorted with # in ABC_# otherwise awk -F: '/^ABC_/{fname=$1".txt";next} {print >fname}' infile ) and that you can find in the answer in flagged question except you no need paste them which you can ignore it? – αғsнιη May 6 '18 at 8:25

With csplit

csplit -s -b %d.txt -f ABC_ Log.txt /ABC_/ {*}
  • you need -z option to remove empty file(s) thought. – αғsнιη May 6 '18 at 8:37
  • @αғsнιη yes and no. this way we get a empty file ABC_0.txt and we can remove it. with -z ABC_0.txt contain ABC_1 and so on. – ctac_ May 6 '18 at 9:13

Awk solution:

awk '/^ABC_/{
         if (fn) close(fn); sub(":", "", $1);
         fn = $1".txt"; next
    { print > fn }' Log.txt

Not the answer you're looking for? Browse other questions tagged or ask your own question.