×
Create a new article
Write your page title here:
We currently have 3,189 articles on s23. Type your article name above or create one of the articles listed here!



    s23
    3,189Articles

    Perl/Useful hacks: Difference between revisions

    Content added Content deleted
    imported>Kunda
    mNo edit summary
     
    imported>Kunda
    mNo edit summary
     
    Line 1: Line 1:
    ===Quote for s23 wiki===
    <pre>
    #!/usr/bin/perl
    #
    # Will put pre-existing text in wiki markup for presentation
    # example:
    # <center>
    # <whitespace>'''Lyrics
    # <whitespace>'''Lyrics
    # </ center>

    $prefix =" '''"; # to prefix each sentence in file

    $file = "$ARGV[0]";
    open(TEMP, $file) || die "Needs file name\n";
    @raw=(TEMP);
    close(TEMP);


    foreach $line (@raw)
    {
    $wholeline = "$prefix" . "$line";
    }
    </pre>


    ===Rename the contents of directory===
    ===Rename the contents of directory===
    * How to rename everything that ends with ".old " to the same name with ".new". Here's how to do it in Perl nicely:
    * How to rename everything that ends with ".old " to the same name with ".new". Here's how to do it in Perl nicely:

    Latest revision as of 14:44, 5 November 2005

    Quote for s23 wiki[edit]

    #!/usr/bin/perl
    #
    # Will put pre-existing text in wiki markup for presentation
    # example:
    #     <center>
    #     <whitespace>'''Lyrics
    #     <whitespace>'''Lyrics
    #     </ center>
    
    $prefix =" '''";    # to prefix each sentence in file
    
    $file = "$ARGV[0]";
    open(TEMP, $file) || die "Needs file name\n";
    @raw=(TEMP);
    close(TEMP);
    
    
    foreach $line (@raw)
    {
        $wholeline = "$prefix" . "$line";
    }
    


    Rename the contents of directory[edit]

    • How to rename everything that ends with ".old " to the same name with ".new". Here's how to do it in Perl nicely:
    foreach my $file (glob "*.old") {
      my $newfile = $file;
      $newfile =~ s/\.old$/.new/;
      if (-e $newfile) {
        warn "can't rename $file to $newfile: $newfile exists\n";
      } elsif (rename $file, $newfile) {
        ## success, do nothing
      } else {
        warn "rename $file to $newfile failed: $!\n";
      }
    }
    

    Thought there is much faster way to do this in bash i bet (link from here if you know of one)

    Cookies help us deliver our services. By using our services, you agree to our use of cookies.
    Cookies help us deliver our services. By using our services, you agree to our use of cookies.