Every regex tp new file

Hi,

I have a file like this-

regex
1
2
regex
2
4
5
regex
4
6
8
regex

how do I split each interval to a new file? something like-
file1-

1
2

file2-

2
4
5

file3

4 
6 
8

.

 sed -n '/regex/,/regex/p' 

seems to produce just groups 1 3 5 and so on.
How do I do it?

Thanks a lot.:wall:

Hi

awk '/regex/{i++;next}{print > "file"i;}' file

Guru.

1 Like
#! /bin/bash
c=0
while read x
do
    if [ $x == "regex" ]
    then
        ((c++))
    else
        echo $x >> file_$c
    fi
done < inputfile
1 Like

Thanks guys :slight_smile:

---------- Post updated at 03:02 PM ---------- Previous update was at 02:32 PM ----------

Another small question, cant seem to figure it out,

say for file 1 and file 2 I want to do wc -l file 1 / wc -l file 2, how do I do this automatically..sounds easy, but I cant get it running straight up.

# printf "%d",$(echo "scale=3 ; `wc -l <file1` / `wc -l <file2`"|bc)|sed 's/\.//;s/$/&\n/'

Thanks ygemici, but for two files of length 56 and 2 it returns a result 0,28000.

If possible could you explain the sed portion of this script and also give ideas on how to do

((wc -l file 1)/(wc -l file1 + wc -l file2))*100

ok i think like that length of file2 is bigger than file1

# printf "%d",$(echo "scale=3 ; 2 / 56"|bc)|sed 's/\.//;s/$/&\n/'
0,035

actually sed portion is not important, it is only remove the `[.]` and append newline to end of output..

best way is that :wink:

# awk 'NR==FNR{x=NR}END{print x / FNR}' file1 file2

regards
ygemici

1 Like

Thanks ygemici :slight_smile: perfect :slight_smile:
What about

((wc -l file 1)/(wc -l file1 + wc -l file2))*100

I think your solutions are for

wc -l file 1 / wc -l file 2