How to continue numbering after a regular expression has been found


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to continue numbering after a regular expression has been found
# 1  
Old 02-10-2011
How to continue numbering after a regular expression has been found

Hello, I have a file starting with:

Code:
fixedStep chrom=chrX start=1 step=1
0.930
0.955
0.972
0.985
0.993
0.995
0.994
0.990
0.984
0.971
0.942
0.944
0.971
fixedStep chrom=chrX start=200 step=1
0.987


The problem is that, apart from the numbers shown, there are these headers found in the file multiple times. I want to construct a program that stores the position and the value, that means:

Code:
1 0.930
2 0.955

etc

whenever you find a line, the numbering continues from the next line, that means:

Code:
201 0.987

I have constructed the following code:
Code:
#!/usr/bin/perl -w

print "Please enter the filename of your file: ";
$dnafilename = <STDIN>;
chomp $dnafilename;

unless ( -e $dnafilename) {

    print "File \"$dnafilename\" doesn't seem to exist!!\n";
    exit;
}

unless ( open(DNAFILE, $dnafilename) ) {

    print "Cannot open file \"$dnafilename\"\n\n";
    exit;
}
while ( <DNAFILE> ) {
            
            unless   (/fixedStep chrom=chrX start=(.+) step=1/) {
            $counter = $1;

        for ($counter = 1; $counter < $. ; $counter++) {
           
            }
             print "$counter\t$0\n";
             
            }
}

close DNAFILE;


exit;

Any help would be greatly appreciated!!!
Moderator's Comments:
Mod Comment
Please use code tags when posting data and code samples!


---------- Post updated at 11:33 AM ---------- Previous update was at 11:29 AM ----------

I am sorry...the numbering starts from where it is indicated by the number stored in the field start, that is start = 200 etc...So this means:

200 0.987
etc

Last edited by vgersh99; 02-10-2011 at 12:32 PM.. Reason: code tags, please!
# 2  
Old 02-10-2011
Code:
nawk -F'[ =]' 'NF>1{start=$5;next}{print start++,$0}' myFile

This User Gave Thanks to vgersh99 For This Post:
# 3  
Old 02-10-2011
Thank you very much mate Smilie

Since I am a newbie in programming, I would like to implement this in Perl...Does anybody have any idea how I could do that, or include this awk command in my Perl code???
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Continue the loop if the value is not found

Dear Help, Is it possible to continue the loop by going to the next available value, if the 'expected value' is not found. I have a list of values which might not get incremented by fixed value and hence the loop could break and the script could terminate. Any suggestion is appreciated. ... (1 Reply)
Discussion started by: Indra2011
1 Replies

2. UNIX for Advanced & Expert Users

sed: -e expression #1, char 0: no previous regular expression

Hello All, I'm trying to extract the lines between two consecutive elements of an array from a file. My array looks like: problem_arr=(PRS111 PRS213 PRS234) j=0 while } ] do k=`expr $j + 1` sed -n "/${problem_arr}/,/${problem_arr}/p" problemid.txt ---some operation goes... (11 Replies)
Discussion started by: InduInduIndu
11 Replies

3. Programming

Perl: How to read from a file, do regular expression and then replace the found regular expression

Hi all, How am I read a file, find the match regular expression and overwrite to the same files. open DESTINATION_FILE, "<tmptravl.dat" or die "tmptravl.dat"; open NEW_DESTINATION_FILE, ">new_tmptravl.dat" or die "new_tmptravl.dat"; while (<DESTINATION_FILE>) { # print... (1 Reply)
Discussion started by: jessy83
1 Replies

4. Shell Programming and Scripting

Would like to print 3 lines after a regular expression is found in the logfile

I would like to print 3 lines after a regular expression is found in the logfile. I'm using the following code: grep -n "$reg_exp" file.txt |while read LINE ;do i=$(echo $LINE |cut -d':' -f1 ) ;sed -n "$i,$(($i+3))p" file.txt ;done The above code things works fine,but sometimes gives erroneous... (3 Replies)
Discussion started by: joachimshaun
3 Replies

5. Shell Programming and Scripting

Integer expression expected: with regular expression

CA_RELEASE has a value of 6. I need to check if that this is a numeric value. if not error. source $CA_VERSION_DATA if * ] then echo "CA_RELESE $CA_RELEASE is invalid" exit -1 fi + source /etc/ncgl/ca_version_data ++ CA_PRODUCT_ID=samxts ++ CA_RELEASE=6 ++ CA_WEEK_NO=7 ++... (3 Replies)
Discussion started by: ketkee1985
3 Replies

6. Shell Programming and Scripting

Regular Expression help

Hi, Example ^www\.+\.host\.com$ I would like to know what the bold parts means. thanks (2 Replies)
Discussion started by: phamp008
2 Replies

7. Linux

Regular expression to extract "y" from "abc/x.y.z" .... i need regular expression

Regular expression to extract "y" from "abc/x.y.z" (2 Replies)
Discussion started by: rag84dec
2 Replies

8. UNIX for Dummies Questions & Answers

regular expression..help!!

need regular expression matches: abc11_efg11_123.xml abc11_123.xml Thank you Andy (1 Reply)
Discussion started by: andy2000
1 Replies

9. Shell Programming and Scripting

Continue Script when File Found

Hello All, I am trying to write a script that will only continue executing my script if a file exits. I know the directory of the file, so its just a matter of seeing if the file exists yet. If the file has not yet been created, I want the script to wait 10 minutes (600 seconds) and try again.... (7 Replies)
Discussion started by: Jose Miguel
7 Replies

10. Shell Programming and Scripting

Regular Expression + Aritmetical Expression

Is it possible to combine a regular expression with a aritmetical expression? For example, taking a 8-numbers caracter sequece and casting each output of a grep, comparing to a constant. THX! (2 Replies)
Discussion started by: Z0mby
2 Replies
Login or Register to Ask a Question