Modify file names containing spaces using perl

4.1.13
Many a times we see some configuration files which have list of paths like:

/abc/123_1/1tvd/fiel.txt
/abc/123 r/1tvd/fie2l.txt
/abc/123 a/1tvd/fie3l.txt
/abc xyz/123/1tvd/fie4l.txt
and some times the spaces create a lot of problems while we use those configuration files.
in such cases we need to make those directory names as
/"abc xyz"/123/1tvd/fie4l.txt
But how do we do it across the complete file?

Below is the perl way to do it:
perl -F"/" -ane 'foreach (@F)
                 {if(/ /){$_="\"".$_."\"";}}
                 print join "/",@F;' config_file
But yes there can be other ways too like in awk.

No comments:

Post a Comment