473,324 Members | 2,581 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Unix To Win32 issue. - cat | sed

3
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.
Aug 14 '07 #1
4 1443
numberwhun
3,509 Expert Mod 2GB
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:

Expand|Select|Wrap|Line Numbers
  1. open(FILE, "<./file.txt");
  2.  
  3. while(<FILE>)
  4. {
  5.     if($_ =~ m/matching regex/)
  6.     {
  7.         $_ =~ s/pattern/substitute/
  8.     }
  9.  
  10. }
  11.  
  12.  
As I said, that is one way you could do it. Hope that helps.

Regards,

Jeff
Aug 14 '07 #2
miller
1,089 Expert 1GB
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.

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2.  
  3. my $infile = shift || 'file1.txt';
  4. my $outfile = shift || 'file3.txt';
  5.  
  6. open(IN, $infile) or die "Can't open $infile: $!";
  7. open(OUT, '>>', $outfile) or die "Can't open $outfile: $!";
  8.  
  9. while (<IN>) {
  10.     # regexs from file 2
  11.     s/something/plus somethingmore/g;
  12.     s/somethingelse/plus somemore/g;
  13.     s/anotherstring/plus a bit more/g;
  14.     print OUT $_;
  15. }
  16.  
  17. close(IN);
  18. close(OUT);
  19.  
- Miller
Aug 14 '07 #3
Degz
3
Jeff and Miller thanks very much.

Nice to bounce ideas round sometimes :)

Derek.
Aug 15 '07 #4
miller
1,089 Expert 1GB
Absolutely,

- M
Aug 15 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

3
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"...
2
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...
4
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...
6
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++.
22
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...
82
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...
14
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...
9
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...
7
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....
223
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
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...
1
isladogs
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...
0
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...
1
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)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
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
0
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...
0
isladogs
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.