1

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.

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

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

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

.... Continue
ABC_99: 
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.

αғsнιη
  • 40,939
  • 15
  • 71
  • 114
Aniket
  • 11
  • 2
  • I assume (?) you want the contents of new files to be the intervening lines? Or empty files? – Jeff Schaller May 05 '18 at 12:04
  • With duch questions you should always provide both example input and example output. – Hauke Laging May 05 '18 at 13:23
  • Possible duplicate of [text processing rows to columns for a block](https://unix.stackexchange.com/questions/390918/text-processing-rows-to-columns-for-a-block) – αғsнιη May 05 '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](https://unix.stackexchange.com/a/390928/72456) in flagged question except you no need paste them which you can ignore it? – αғsнιη May 06 '18 at 08:25

2 Answers2

2

With csplit

csplit -s -b %d.txt -f ABC_ Log.txt /ABC_/ {*}
ctac_
  • 1,960
  • 1
  • 6
  • 14
1

Awk solution:

awk '/^ABC_/{
         if (fn) close(fn); sub(":", "", $1);
         fn = $1".txt"; next
    }
    { print > fn }' Log.txt
RomanPerekhrest
  • 29,703
  • 3
  • 43
  • 67