in reply to writing all the strings between two particular substrings
It works by setting the input record separator '$/' to 'output', so every line you read will be terminated by 'output', rather than by the usual EOL.use strict; { local $/ = 'output'; while (<DATA>) { chomp; # drop 'output' my (undef, $line) = split /input/; $line =~s/,/;/g; print "$line\n"; } } __DATA__ input CK, n3065gat, n3066gat, n3067gat, n3068gat, n3069gat, n3070gat,n +3100gat, test_si, test_se; output n3104gat, n3105gat, n3106gat, n3107gat, n3108gat, n3109gat, n +3110gat; input CK, n3065gat_2, n3066gat_2, n3067gat_2, n3068gat_2, n3069gat_2, +n3070gat_2,n3100gat_2, test_si, test_se; output n3104gat_2, n3105gat_2, n3106gat_2, n3107gat_2, n3108gat_2, n +3109gat_2, n3110gat_2; input CK, n3065gat_3, n3066gat_3, n3067gat_3, n3068gat_3, n3069gat_3, +n3070gat_3,n3100gat_3, test_si, test_se; output n3104gat_3, n3105gat_3, n3106gat_3, n3107gat_3, n3108gat_3, n +3109gat_3, n3110gat_3; input CK, n3065gat_4, n3066gat_4, n3067gat_4, n3068gat_4, n3069gat_4, +n3070gat_4,n3100gat_4, test_si, test_se; output n3104gat_4, n3105gat_4, n3106gat_4, n3107gat_4, n3108gat_4, n +3109gat_4, n3110gat_4;
chomp removes the 'output'.
Then I split the line just read on 'input' and throw away everything before 'input', leaving me with nothing but what is between 'input' and 'output'. The substitution s/,/;/g takes care of changing commas into semi-colons. There is no need to quotemeta them as they are not special in a regex. The result then gets printed.
BTW: you are opening, printing and closing the file for every line. Not only is this very slow, it is also wrong as each opening of the file for output with '>' will delete everything which was in that file, so you end up with only the result of your last print. Keep your open and close outside of the loop.
CountZero
A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James
Update: fixed typo.
|
|---|