in reply to Extracting data from a pipe delimited text file

There are dozens of ways I imagine. I'd probably choose a lookahead: $line =~ s/\s+(?=\|)//g. Hrm, I just noticed there's space after one of them. A look-ahead is probably the wrong choice then. I'd probably choose $line =~ s/\s*\|\s*/\|/g instead — there may be a more efficient way though.

I won't guarantee it's use in a production environment though. I've never tested the above.

-Paul

Replies are listed 'Best First'.
Re^2: Extracting data from a pipe delimited text file
by Ionitor (Scribe) on Mar 29, 2007 at 19:13 UTC

    And in that vein, the following command should scan through a file and perform the operation on every line, leaving a backup with .bak appended. If this is Windows, change the single quotes to double quotes.

    perl -pi.bak -e 's/\s*\|\s*/\|/g' filename