Active State Perl with Unix-line endings -- Help

Discussion in 'Perl Misc' started by Brian & Colleen, Oct 16, 2004.

  1. I'm using Active State Perl on a WinXP machine. I also run Cygwin and I
    decided I wanted a utility to clean my .bash_history file to remove some
    commands and duplicate lines.

    The line endings are driving me nuts because I think Perl assumes it's on a
    Win Platform but bash is writing Unix-style line endings. I keep ending up
    with spurious ^M's in my .bash_history. I'm trying to force things with $/
    and $\ but it isn't working. As you can tell from the code, I'm new to Perl
    so it's probably something really simple.
    ----------------------------
    use strict;

    $/ = "\012"; # force use of Unix-style line endings as created
    $\ = "\012"; # bash. Strip them on input and restore on output.

    my $delstr = qr /exit|cd|ls|rm|cat|pwd|which/ ;
    my $oldline = "zzzzzzz";
    my $line = "";
    open(INFILE,"<.bash_history");
    open(OUTFILE,">.bash_history2");

    while ($line=<INFILE>){
    chomp $line;
    if (($line !~ /$delstr/) && ($line ne $oldline)){
    print OUTFILE "$line";
    $oldline = $line;
    }
    }
    close INFILE;
    close OUTFILE;

    if (rename ".bash_history2",".bash_history") {
    print "Done.";
    }
    ----------------

    Thanks.

    Brian
     
    Brian & Colleen, Oct 16, 2004
    #1
    1. Advertising

  2. Good point both Tim and Bob made about using Unix tools to solve Unix
    problems. I updated my Cygwin to include Perl and that seems to take care of
    the confusion over line endings. What I thought should be a good little
    "learning" project turned out to be more educational than I bargained for.

    Thanks for the suggestions.

    Brian
     
    Brian & Colleen, Oct 16, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Fuzzyman
    Replies:
    4
    Views:
    573
    Fuzzyman
    Feb 7, 2006
  2. Fuzzyman

    Detecting line endings

    Fuzzyman, Feb 6, 2006, in forum: Python
    Replies:
    18
    Views:
    590
    Fuzzyman
    Feb 8, 2006
  3. Ant
    Replies:
    6
    Views:
    398
    Fredrik Lundh
    Dec 5, 2006
  4. Replies:
    5
    Views:
    396
    Marc 'BlackJack' Rintsch
    May 3, 2007
  5. towers
    Replies:
    7
    Views:
    299
    =?ISO-8859-1?Q?Ricardo_Ar=E1oz?=
    Aug 17, 2007
Loading...

Share This Page