Every regex tp new file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Every regex tp new file
# 1  
Old 05-15-2012
Every regex tp new file

Hi,

I have a file like this-
Code:
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-
Code:
1
2

file2-
Code:
2
4
5

file3
Code:
4 
6 
8

.


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

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

Thanks a lot.Smilie
# 2  
Old 05-15-2012
Hi

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

Guru.
This User Gave Thanks to guruprasadpr For This Post:
# 3  
Old 05-15-2012
Code:
#! /bin/bash
c=0
while read x
do
    if [ $x == "regex" ]
    then
        ((c++))
    else
        echo $x >> file_$c
    fi
done < inputfile

This User Gave Thanks to balajesuri For This Post:
# 4  
Old 05-15-2012
Thanks guys Smilie

---------- 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.
# 5  
Old 05-15-2012
Code:
# printf "%d",$(echo "scale=3 ; `wc -l <file1` / `wc -l <file2`"|bc)|sed 's/\.//;s/$/&\n/'

# 6  
Old 05-15-2012
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

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

# 7  
Old 05-15-2012
Quote:
Originally Posted by jamie_123
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

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

ok i think like that length of file2 is bigger than file1
Code:
# 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 Smilie
Code:
# awk 'NR==FNR{x=NR}END{print x / FNR}' file1 file2

regards
ygemici
This User Gave Thanks to ygemici For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sendmail K command regex: adding exclusion/negative lookahead to regex -a@MATCH

I'm trying to get some exclusions into our sendmail regular expression for the K command. The following configuration & regex works: LOCAL_CONFIG # Kcheckaddress regex -a@MATCH +<@+?\.++?\.(us|info|to|br|bid|cn|ru) LOCAL_RULESETS SLocal_check_mail # check address against various regex... (0 Replies)
Discussion started by: RobbieTheK
0 Replies

2. UNIX for Beginners Questions & Answers

Using strings in one file as regex to search field of another file

I have a data file, file1.txt, such as the following: 1,2 "TWRTW", "TWRH/" 1,2 "JHGH", "HGJ 1,2 "JWMM", "JWMM" 1,2 "W", "W" 1,2 "LJLH", "LJLH/" 1,3 "W", "W" 1,3 "HJH", "HJJ I have another file, file2.txt, that contains... (3 Replies)
Discussion started by: jvoot
3 Replies

3. Shell Programming and Scripting

Perl, RegEx - Help me to understand the regex!

I am not a big expert in regex and have just little understanding of that language. Could you help me to understand the regular Perl expression: ^(?!if\b|else\b|while\b|)(?:+?\s+){1,6}(+\s*)\(*\) *?(?:^*;?+){0,10}\{ ------ This is regex to select functions from a C/C++ source and defined in... (2 Replies)
Discussion started by: alex_5161
2 Replies

4. Shell Programming and Scripting

Bash script to send lines of file to new file based on Regex

I have a file that looks like this: cat includes CORP-CRASHTEST-BU e:\crashplan\ CORP-TEST /usr/openv/java /usr/openv/logs /usr/openv/man CORP-LABS_TEST /usr/openv/java /usr/openv/logs /usr/openv/man What I want to do is make three new files with just those selections. So the three... (4 Replies)
Discussion started by: newbie2010
4 Replies

5. Shell Programming and Scripting

Need a REGEX to increment the file number of a pdf file

Hello, I have a few thousand .pdf files in various folders each have a naming scheme like this: 006_-_Titled_Document_#34_-_September-25-2011-side-1.pdf In each folder, the number system starts at 001 (as you see on the far left of the file name), and then ends at 999 (maximum .pdf files).... (4 Replies)
Discussion started by: Marcus Aurelius
4 Replies

6. UNIX for Dummies Questions & Answers

read regex from ID file, print regex and line below from source file

I have a file of protein sequences with headers (my source file). Based on a list of IDs (which are included in some of the headers), I'd like to print out only the specified sequences, with only the ID as header. In other words, I'd like to search source.txt for the terms in IDs.txt, and print... (3 Replies)
Discussion started by: pathunkathunk
3 Replies

7. Shell Programming and Scripting

regex matches from lines in file

Hello, I try to script something (bash-script) and can not find a way to get and store a match into a variable from a line in a file. grep isn't useful as the matches are not returned - just colored. I can't get 'expr' to work for me. Is it necessary to use a perl-script with regex instead? ... (7 Replies)
Discussion started by: daWonderer
7 Replies

8. Shell Programming and Scripting

Converting perl regex to sed regex

I am having trouble parsing rpm filenames in a shell script.. I found a snippet of perl code that will perform the task but I really don't have time to rewrite the entire script in perl. I cannot for the life of me convert this code into something sed-friendly: if ($rpm =~ /(*)-(*)-(*)\.(.*)/)... (1 Reply)
Discussion started by: suntzu
1 Replies

9. Shell Programming and Scripting

Matching using Regex inside a file

I need scan through some files, then open the file one by one and scan inside the file using perl to see if it contain a start tag and end tag which the end tag is the mirror image of the start tag, the start tag and end tag only have 5 char. And inside the file there is "http://". It is just a... (5 Replies)
Discussion started by: blueblur
5 Replies

10. Shell Programming and Scripting

SED + Regex + SQL Input file

Here's the problem... I have a mysqldump file and I need to put single quotes around the date/time timestamp. So for example I have a line like: INSERT INTO attachments VALUES (1,182,2004-08-06 09:24:04,'description'... and I need it to become INSERT INTO attachments VALUES... (10 Replies)
Discussion started by: primal
10 Replies
Login or Register to Ask a Question