0

This question already has an answer here:

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

marked as duplicate by αғsнιη, RalfFriedl, JigglyNaga, schily, roaima Oct 25 '18 at 8:31

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2

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

csplit -f file --suppress-matched -z input.txt '/^@/' '{*}'
  • Quite correct unix. If only I had such knowlwdge. Thanks – schweik Oct 24 '18 at 8:23
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

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

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