I am currently facing a problem that seems easy to deal with but I haven’t been able to solve it so far. I have a “pipe separated value” file and I would like to remove all leading and trailing spaces on all values on every line. This file may contain several lines.
Example:
1| | Field2 | Field3
2| 123 | Field2 |
3| | | Field3
What I want after removing the spaces:
1||Field2|Field3
2|123|Field2|
3|||Field3
What I have so far works as long as the last field (in this case Field3) is not NULL. This correspond to line 2 on the example above. When the last field is NULL it concatenates that line with the next one creating a bigger line than what is expected.
My code so far is this one:
$res =~ s/\s*\|\s*/\|/g;
As mentioned previously this works as long as the last field on all lines is not NULL. I have tried to come up with a regex that matches all occurrences of a pipe except the last one but without success so far.
Any idea how I could remove all spaces and maintain the line integrity?
Thanks in advance,
João
The regex
/\s/matches carriage-returns and newlines (amongst other things) as well as spaces and tabs, so your substitution will delete the trailing newline on records with null final fields.Try
or
if you’re not interested in removing tab characters.
(The pipe in the replacement string doesn’t need escaping.)