Hi Guys,
I'm currently porting some shell scripts over to a windows box (in perl)
Can anyone help me with this one. ?
I have a sed command taking input from cat, and substituting based on a multi line script file. so...
cat file1.txt | sed -f file2.txt >> file3.txt
file2.txt looks something like this:
s/something/plus somethingmore/g
s/somethingelse/plus somemore/g
s/anotherstring/plus a bit more/g
What i'm looking for is an efficient way to do this in perl. I'm sure I could do this with multiple passes over every line, but that doesn't feel right.
ie
my @substitution_lines_array;
open FILE "<file2.txt";
while (<FILE>) {
push (@substitution_lines_array);
}
Then I could iterate over all the lines of the file that *may* need substitution on them
I've seen psed, but i'm not sure how I can give it an input file, like the above "cat" command. Or even if this is really the best way to do it.
I'm sure there must be a better way, since perl is the master of search and replace on files. :)
Many Thanks
Derek.
4 1443
This actually doesn't sound like it would be that hard. Basically, you want to open a file and then cycle through it one line at a time using a while loop. Then, in the while loop, you can test, using a substitution regular expression.
So, you could have something like this: -
open(FILE, "<./file.txt");
-
-
while(<FILE>)
-
{
-
if($_ =~ m/matching regex/)
-
{
-
$_ =~ s/pattern/substitute/
-
}
-
-
}
-
-
As I said, that is one way you could do it. Hope that helps.
Regards,
Jeff
What i'm looking for is an efficient way to do this in perl. I'm sure I could do this with multiple passes over every line, but that doesn't feel right.
Hi Derek,
Honestly, don't worry about efficiency. Just create a perl script to do this translation simply, and see how fast it is. I'm betting that it will be plenty fast enough for your purposes. -
use strict;
-
-
my $infile = shift || 'file1.txt';
-
my $outfile = shift || 'file3.txt';
-
-
open(IN, $infile) or die "Can't open $infile: $!";
-
open(OUT, '>>', $outfile) or die "Can't open $outfile: $!";
-
-
while (<IN>) {
-
# regexs from file 2
-
s/something/plus somethingmore/g;
-
s/somethingelse/plus somemore/g;
-
s/anotherstring/plus a bit more/g;
-
print OUT $_;
-
}
-
-
close(IN);
-
close(OUT);
-
- Miller
Jeff and Miller thanks very much.
Nice to bounce ideas round sometimes :)
Derek.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Ben Kial |
last post by:
I need to write a program to "copy" files and preserved the files'
atime, mtime, ctime, file permissions. I also need to have this
program working in Unix and Windows, where the "Archive"...
|
by: GujuBoy |
last post by:
i have the following code...which works fine in UNIX but then i move it
over to WINDOWS XP and check the sum on the same file that i tested on
unix and i get different results.
def...
|
by: Jonathan Li |
last post by:
I have posted the question before but nobody have given me a solution. I
would like to rephrase my requirement and want to hear your advice.
We are developing application packages for logistics...
|
by: Matthew |
last post by:
How would I go about creating a simple "hello world" program that will
run in Unix. I am using MS Visual C++.
|
by: Ryan M |
last post by:
I've been programming for a while, but most of my experience is on unix.
How do C compilers work on operating systems that weren't written in C?
And that have no libc?
Compiling C on unix seems...
|
by: zardoz |
last post by:
I've got this problem:
unsigned long long lTemp;
char cLargeNum="1324567890";
sscanf(clargeNum,"%llu",&lTemp);
which under Win32 isn't working*. My program needs to compile under
posix so...
|
by: **--> That Guy Downstairs |
last post by:
What files are needed to be #included to be able to copy files to a new
directory and be portable?
ie. use it in Unix (SGI and Linux) or Windows 2000.
#ifdefs Ok.
using dirent.h on SGI, but...
|
by: collection60 |
last post by:
I've been developing some Unix based shell tools.
They work fine on Linux and MacOSX.
I want to compile them on Win32. But I can't get hash_map to compile. I
tried downloading stl (and...
|
by: yang__lee |
last post by:
Hi,
I have been programming c, c++ on windows.
What is the difference in programming on Unix.. are there different
syntaxes, functions on Unix.
what are mandatory different steps on unix....
|
by: Pilcrow |
last post by:
Given that UNIX, including networking, is almost entirely coded in C,
how come so many things are almost impossible in ordinary C? Examples:
Network and internet access, access to UNIX...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |