Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Removing leading whitespace from a file?

by Anonymous Monk
on Jul 09, 2002 at 04:25 UTC ( #180403=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello fellow monks!

This is a simple question but since I am new here, I felt inclined to ask ( after 5 attempts ). How do I remove leading whitespace from every line in a file then 're-write' the 'new' version back? My 5th attempt is here:

#!/usr/bin/perl -w use strict; use fatal; open( FH, "<test.txt" ); open( FH2, ">test.txt" ); while ( <FH> ) { s/^(\s+)//g; print FH2 $_; } close FH; close FH2;

Warning: This sample does not work.
Thank you,
Alicia

Replies are listed 'Best First'.
Re: Removing leading whitespace from a file?
by atcroft (Abbot) on Jul 09, 2002 at 04:34 UTC

    Does perl -p -i.bak -e "s/^\s+//g" filename not do it (also giving a backup file named filename.bak)?

Re: Removing leading whitespace from a file?
by Zaxo (Archbishop) on Jul 09, 2002 at 04:34 UTC

    You open the same file twice. The open to write will clobber the file, so it's empty when it's read.

    Instead, open a new filename to write, and rename it after you're done.

    After Compline,
    Zaxo

Re: Removing leading whitespace from a file?
by greenFox (Vicar) on Jul 09, 2002 at 05:11 UTC
Re: Removing leading whitespace from a file?
by Juerd (Abbot) on Jul 09, 2002 at 05:44 UTC

    An alternative:

    use Tie::File; tie @file, 'Tie::File', 'test.txt'; s/^\s+// for @file;
    Not the most efficient solution, but I'd prefer it to setting $^I, if it were in a larger program.

    The easiest solution was already mentioned:

    perl -i -pe's/^\s+//' test.txt

    - Yes, I reinvent wheels.
    - Spam: Visit eurotraQ.
    

Re: Removing leading whitespace from a file?
by abstracts (Hermit) on Jul 09, 2002 at 04:40 UTC
    You shouldn't read and write to the file at the same time; instead make a new file, then copy over:
    #!/usr/bin/perl -w use strict; use fatal; use File::Copy; open( FH, "<test.txt" ); open( FH2, ">test.txt.tmp" ); while ( <FH> ) { s/^(\s+)//g; print FH2 $_; } close FH; close FH2; move("test.txt.tmp", "test.txt");
    Hope this helps,,,

    PS. check out perldoc perlrun to learn how to do this in one line.

    update: atcroft, runrig and Zaxo: are you guys on a race or something?

Re: Removing leading whitespace from a file?
by Mr. Muskrat (Canon) on Jul 09, 2002 at 04:41 UTC

    test.txt:

    0 1 2 3

    test.pl:

    #!/usr/bin/perl use strict; use warnings; open(FH, "<", "test.txt"); open(FH2, ">", "test2.txt" ); while(<FH>) { s/^(\s+)//g; print FH2 $_; } close FH2; close FH;

    test2.pl:

    #!/usr/bin/perl use strict; use warnings; open(FH, "<", "test.txt"); my @file = <FH>; close(FH); open(FH, ">", "test.txt" ); foreach (@file) { s/^(\s+)//g; print FH $_; } close FH;

    I'm sure there are more ways to do this...

    Update: man, I guess I was a little slow to type. When I started the reply, there was only the original node.


    My thoughts are mine and mine alone.
    They did not originate with the voices in my head.
    Do not listen to what they say, it's all lies.

Re: Removing leading whitespace from a file?
by runrig (Abbot) on Jul 09, 2002 at 04:34 UTC
    You might want to look at perldoc:perlrun and the -p and -i options...also, you can't write to the same file you're reading from like that...

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2023-02-04 18:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer not to run the latest version of Perl because:







    Results (31 votes). Check out past polls.

    Notices?