mikevanhoff has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: Strip line feeds from file
by thelenm (Vicar) on Jun 09, 2003 at 20:14 UTC

    Something like this might be what you're looking for:

    perl -pi.bak -e 's/[\n\r]+//g' myfile

    This will strip all the carriage returns and linefeeds and then overwrite the original file. A backup file will be created with a .bak extension, just in case.

    -- Mike

    --
    just,my${.02}

Re: Strip line feeds from file
by cciulla (Friar) on Jun 09, 2003 at 20:16 UTC

    update: thelenm's solution is more elegant, but here's my (more or less) standard solution:

    $_ = $ARGV[0]; $outFile = "$_.1"; open(OUTFILE, ">$outFile") or die "Can't open '$outFile' for writing: +$!"; open(FILE, $_) or die "Can't open '$_': $!"; while (<FILE>) { chomp; print OUTFILE $_; } close(FILE); close(OUTFILE);

Re: Strip line feeds from file
by TomDLux (Vicar) on Jun 09, 2003 at 20:18 UTC

    I doubt you really want to do it the way you describe ... removing all the \n before doign anything else. Much better to process one line at a time, unless you really need access tot he whole file.

    open FILE, "<", $filename; while(<FILE>) { chomp; # mode processing goes here }
Re: Strip line feeds from file
by cbro (Pilgrim) on Jun 09, 2003 at 20:20 UTC
    Make a perl script (yourscript.pl) that contains the line:
    s/[\n\r\f]//g
    Then run:
    perl -i.tmp -p yourscript.pl file.txt
    against your file...say file.txt.
    That is how you edit a file in-place. You will have your old file as file.txt.tmp to do a comparison and confirm it worked if you want.
    You could also do this for each line if you read the file into an array.
    That is...
    open (F,"file.txt"); @ar = <F>; close(F); foreach (@ar) { s/[\n\r\f]//g; # do what you want with the line. }

    Update:
    Well, it seems I was a little slow on the typing, other than the other posts do not contain the '\f'. You may have wanted to include this as a reply on your last post cuz now that one is going to get unneeded attention.
Re: Strip line feeds from file
by mikevanhoff (Acolyte) on Jun 09, 2003 at 20:40 UTC
    Thank to everyone for their help. It all seems so easy when you talk to experts like you all. Thanks again.