I have to write a script that can be used to do search and replaces on files, and must include the ability to work with tabs, carriage returns, etc etc.
I've run into the problem of that I can match using \t, \n, etc etc, but for some reason when I put \n, \t in the replace part of the regular expression it does not interpet the commands and there, and just dumps a \t and a \n into my text. This is using Perl on Slackware Linux, Perl version 5.6.0.
Below is the script and the file it reads the terms from.
#!/usr/bin/perl
$filename = "test.txt";
$termsfile = "test.list";
open filename or die "Can't find file $filename: $!\n";
@terms = `cat test.list`;
foreach (@terms)
{
chomp;
}
$count = 1;
while (read filename, $buf, 16384)
{
@buf1[$count] = $buf;
$count++;
}
foreach $term (@terms)
{
($search, $replace, $junk) = split /\#\#SPLIT\#\#/, $term;
print STDOUT $search;
print STDOUT $replace;
print "\n";
foreach $buf2 (@buf1)
{
$buf2 =~ s/$search/$replace/g;
}
}
print STDOUT @buf1;
close filename;
The file it reads the terms from looks like.
.END##SPLIT##.ENDTAGHERE\t
\t##SPLIT##TAGMARKHERE
The script will go through a file and convert all 'tabs' to TABMARKHERE. But it converts all .END to the exact text of .ENDTAGHERE\t.
Can someone please help me figure out this problem?
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.