Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Reformat file with empty spaces

by Anonymous Monk
on Jan 11, 2018 at 08:06 UTC ( #1207104=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hej Monks, I have a problem with a file that has been melformated during some operation on a source machine. It now looks like this:
18450 0600060009 25 0 768188519 768188519 + 0030020124 000 000 10 77019 73587 25 0 73165 7307 +40506 0160100051 029 000 74049 25 0 73950 + 1040030047 030 000 14048 14048 25 0 11833 1183 +3 022 000 18450 0600060027 25 0 709948778 709948778 + 0010030064 000 000 10 73330 25 0 0050312875 7702 +0 000 000 18450 0600040020 25 0 703820853 703820853 + 0030020125 000 000 10
And I need it to looks like this
18450 0600060009 25 0 768188519 768188519 + 0030020124 000 000 10 77019 73587 25 0 73165 730740506 + 0160100051 029 000 74049 25 0 73950 + 1040030047 030 000 14048 14048 25 0 11833 11833 + 022 000 18450 0600060027 25 0 709948778 709948778 + 0010030064 000 000 10 73330 25 0 0050312875 77020 + 000 000 18450 0600040020 25 0 703820853 703820853 + 0030020125 000 000 10
Now, what happened is that everything that has been melformated had shifted 5 spaces to the right for some reason. I know that bash SED would be maybe the easiest way to solve it but I just don't know how to do it. I've tried with printf, but my problem is how to read empty spaces into a variable/list. Could you please help ?

Replies are listed 'Best First'.
Re: Reformat file with empty spaces
by poj (Monsignor) on Jan 11, 2018 at 09:09 UTC

    Use substr

    #!/usr/bin/perl use strict; my $infile = 'infile.txt'; my $outfile = 'outfile.txt'; open my $in,'<',$infile or die "Could not open $infile : $!"; open my $out,'>',$outfile or die "Could not open $outfile : $!"; while (<$in>){ chomp; my $spaces = substr($_,39,5); if ( $spaces eq ' '){ substr($_,5,5,''); $_ .= $spaces; } print $out "$_\n"; } close $in; close $out;
    poj
      Thanks a million Poj !
Re: Reformat file with empty spaces
by kcott (Chancellor) on Jan 12, 2018 at 00:47 UTC

    TMTOWTDI. Applying the substitution, s/^[^ ]*\K {5}//, to each line, achieves the realignment you show.

    $ cat pm_1207104_data.txt 18450 0600060009 25 0 768188519 768188519 + 0030020124 000 000 10 77019 73587 25 0 73165 7307 +40506 0160100051 029 000 74049 25 0 73950 + 1040030047 030 000 14048 14048 25 0 11833 1183 +3 022 000 18450 0600060027 25 0 709948778 709948778 + 0010030064 000 000 10 73330 25 0 0050312875 7702 +0 000 000 18450 0600040020 25 0 703820853 703820853 + 0030020125 000 000 10
    $ perl -pe 's/^[^ ]*\K {5}//' pm_1207104_data.txt 18450 0600060009 25 0 768188519 768188519 + 0030020124 000 000 10 77019 73587 25 0 73165 730740506 + 0160100051 029 000 74049 25 0 73950 + 1040030047 030 000 14048 14048 25 0 11833 11833 + 022 000 18450 0600060027 25 0 709948778 709948778 + 0010030064 000 000 10 73330 25 0 0050312875 77020 + 000 000 18450 0600040020 25 0 703820853 703820853 + 0030020125 000 000 10

    — Ken

      Great observation, Much appreciated !
Re: Reformat file with empty spaces
by Laurent_R (Canon) on Jan 11, 2018 at 13:55 UTC
    poj has given you a solution that probably solves the misalignment problem, but pay attention that it appears that the lines where the second field is shifted five spaces to the right also seem to have been truncated on their right end. It may be a wrong impression, or perhaps there is no such problem because it is only due to the fact that you did not show the entire lines, but I thought it was worth pointing out this possible issue.
      Hej Laurent, that was really observant, thanks for that. Basically (as a Perl beginner) I just needed a push and Poj's substr helped me a lot. Of course, I have changed his original script a bit so it suits my needs (including taking care of other columns), but idea was there to help me. Thank you very much also for bringing this point into daylight.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1207104]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (3)
As of 2018-08-14 06:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Asked to put a square peg in a round hole, I would:









    Results (144 votes). Check out past polls.

    Notices?