How can I extract and save text using Perl?

Also, the second line in every Perl script you write should be: use strict; It will make Perl catch such mistakes and tell you about them, rather than silently ignoring them.

... and the third should be use warnings. – Brad Gilbert Oct 17 '08 at 19:20 He already has -w on the first line. – raldi Oct 17 '08 at 19:42 Well why doesn't he just add -Mstrict to the first line?

– Brad Gilbert Mar 18 '09 at 21:45.

While () { if (m/^(EX|ex)\d. */) { print OUT "$_"; print $_; } }.

Output of all lines in the input file, you can reduce this to the one-liner perl -ne 'print if /^(EX|ex)\d/' – Jouni K. Seppänen Oct 17 '08 at 7:30 perl golf has its place, but I'd rather people put readable code into production. – John Ferguson Oct 17 '08 at 9:00 This is simple enough to use a one-liner.

– Brad Gilbert Mar 18 '09 at 21:47.

Bleh! "use strict;" "use warnings;". Lexical-filehandles.

Three-args-open.

Sorry if this seems like stating the bleeding obvious, but what's wrong with grep -i ^ex data2. Txt ... or if you really want to do it in perl (and there's nothing wrong with that): perl -ne '/^ex/i && print' data2. Txt This assumes the purpose of the request is to find lines that start with EX, with case-insensitivity.

When I run your code, but name the input file My1. Txt instead of MyFile. Txt I get the desired output - except with empty lines, which you can remove by removing the , "\n" from the print statement.

Oh sorry I forgot to edit My1.txt. It should be MyFile.txt. – Shiel Oct 18 '08 at 2:56.

The filenames don't match. Open(my $inhandle, '', $outfile) or die "Cant open $outfile: $! "; while(my $line = ) { # Assumes that ex, Ex, eX, EX all are valid first characters if($line =~ m{^ex}i) { # or if(lc(substr $line, 0 => 2) eq 'ex') { print { $outhandle } $line; print $line; } } And yes, always always use strict; You could also chomp $line and (if using perl 5.10) say $line instead of print "$line\n".

Print { $outhandle } $line; – raldi Oct 17 '08 at 19:43 It helps avoid mistakes like... print $outhandle, $line; (the comma means print won't recognise $outhandle as a file handle). Its a recommendation from "Perl Best Practises" by Damian Conway. – draegtun Oct 17 '08 at 21:39 I didn't realize that would work.

– Brad Gilbert Mar 18 '09 at 21:49.

Using a regexp is overkill - you're much better off just checking the first two letters.In python: from __future__ import with_statement class converter(object): def __init__(self, inFile, outFile): self. InFile, self. OutFile = inFile, outFile def main(self): with open(self.

InFile, 'r') as infsock: with open(self. OutFile, 'w') as outfsock: for line in infsock: self. DoReplace(line, outfsock) def doReplace(self, line, outsock): if ''.

Join(line:2).upper() == "EX": outsock. Write(line) if name == 'main': import sys ZeConverter = converter(sys. Argv1, sys.

Argv2) ZeConverter.main() Sub out doReplace if you need a different replacement method.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions