Home: Perl Programming Help: Intermediate:
Need help with regex, spaces and newlines in file



Iconx
Novice

Jul 22, 2014, 8:07 AM


Views: 1965
Need help with regex, spaces and newlines in file

So i have a file that goes something like this:

Code
file.txt 
------------------
this is line one
line two


line five
line six
----------------------

I'm not sure whats in the blank spaces there, could be space, could be cairrage return, dunno.
Is there a good way to detect and clear these?

I've tryed:

if ( $line =~ /^\s*$/ ) {
if($line =~ /^\s+\Z/)
I've even tried various sed commands and they return

sed: -e expression #1, char 14: unterminated address regex

How can I clear all blanks, cairrage returns tabs spaces whatever his holding these spaces in the file.

Iconx


BillKSmith
Veteran

Jul 22, 2014, 8:35 AM


Views: 1963
Re: [Iconx] Need help with regex, spaces and newlines in file

Do you want to 'clear' lines which contain only whitespace?
Consider using the -i option to edit the file in place.

Note: The double quotes below are needed by windows. Most other operating systems require single quotes instead.


Code
>perl -ne"print unless /^\s*$/" file.txt


OUTPUT

Code
this is line one 
line two
line five
line six

Good Luck,
Bill