[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Splice Line Occurrences In A Text File

Posted on 2010-04-05
4
Medium Priority
?
359 Views
Last Modified: 2012-05-09
I need to splice all line occurrences in a text file with these variables:
/^REM DATE .*?$/
/^REM GENRE .*?$/
but I don't know how to use the splice function when not exact values.
0
Comment
Question by:thebourneid
  • 2
4 Comments
 
LVL 6

Accepted Solution

by:
zlobcho earned 2000 total points
ID: 29770507
Try this:
#!/usr/bin/perl

 use strict;
 open (DATA,"yourfile.txt") or die "can not open the inputfile\n";
 my $lines;
 while(<DATA>){
  if (($_!~/^REM DATE .*?$/)||($_!~/^REM GENRE .*?$/)){
   $lines.=$_;
  }
  if (($_=~/^REM DATE .*?$/)||($_=~/^REM GENRE .*?$/)) {
    print $lines;
    print "\n\n\n\n";
    undef $lines;
  }
}
close (DATA);

Open in new window

0
 
LVL 6

Expert Comment

by:zlobcho
ID: 29770721
Or:
#!/usr/bin/perl
 use strict;
 open (DATA,"yourfile.txt") or die "can not open the inputfile\n";
 my $lines; my @array;
 while(<DATA>){
  if (($_!~/^REM DATE .*?$/)||($_!~/^REM GENRE .*?$/)){
   $lines.=$_;
  }
  if (($_=~/^REM DATE .*?$/)||($_=~/^REM GENRE .*?$/)) {
    push (@array,$lines);
    undef $lines;
  }
}
close (DATA);
foreach (@array){
print "$_\n\n";
}

Open in new window

0
 
LVL 48

Expert Comment

by:Tintin
ID: 29777970
Could you please describe what you are trying to do in non-perl terms, as the splice function is for removing elements from an array and your "variables" are actually regular expressions, so it is not obvious what you are trying to achieve.
0
 

Author Comment

by:thebourneid
ID: 29809107
I'm trying to remove every line oocurrences with the variables from a text file without leaving blank spaces.
I'm using the "tie:file" function as a part of a bigger script and this substitution s/^\n//; dosen't work to delete empty lines.
is it possible to integrate zlobcho's suggestion in this script


my @ln;      
       tie @ln, 'Tie::File', $filename or die "could not tie $fn: $!"; 
       foreach (@ln) {
	/some substitution process/..../;   
       } 
       untie @ln; 

Open in new window

0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Article by: Tammy
MySQLTuner is a script written in Perl that allows you to review a MySQL installation quickly and make adjustments to increase performance and stability. The current configuration variables and status data is retrieved and presented in a brief forma…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

608 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question