0

I have a file which looks like this

@
0 60
0 60
0 1
0 1
0 3
0 0
@
0 0
0 0
0 0
0 0
@
.
.
.
@

and from this I want to create file1 with the values/rows from the first @ to the second @ and then a next file2 with the values/rows from the second @ to the third @ so the file 1 should have the following output

0 60
0 60
0 1
0 1
0 3
0 0

file 2 should have the following output

0 0
0 0
0 0
0 0
Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255

3 Answers3

2

That's what csplit is for. With the GNU implementation:

csplit -f file --suppress-matched -z input.txt '/^@/' '{*}'
0

As primitive as possible:

i=0; while read NN; do if [[ $NN == "@" ]] ;then i=$(($i+1)) ; else echo $NN >> file$i; fi done < file

Hi

schweik
  • 1,250
0

You can use awk :

awk -v 'f=file' '/^@/{i++;close(f i);fg=1;next}!fg{next}{print > f i}' infile

If you want only 2 files :

awk -v 'f=file' -v 'nb=2' '/^@/{i++;close(f i);fg=1;next}!fg{next}i>nb{exit}{print > f i}' infile
ctac_
  • 1,960