remove space, tab, newline etc.. from a text file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lilly07
    New Member
    • Jul 2008
    • 89

    remove space, tab, newline etc.. from a text file

    I am trying to remove all white spaces, tabs, newline, carriage return etc.. from a text file to make it as a one continous string. But the following code doesnot work. The file size is relatively smaller and hence I didn't use FILE::slurp. Please let me know the problem. Thanks.

    Code:
    #!/usr/bin/perl
    local $/=undef;
    open(FILE, "test.fa") || die ("Error\n");
    
    $string = <FILE>;
    
    $corrected_string =~ s/\n\r\t\s//g;
    print "$corrected_string";
  • Icecrack
    Recognized Expert New Member
    • Sep 2008
    • 174

    #2
    for the regexp to work you need to make sure it has a VAR with a string.

    Code:
    #!/usr/bin/perl
    local $/=undef;
    open(FILE, "test.fa") || die ("Error\n");
     
    $string = <FILE>;
     
    $string =~ s/\n\r\t\s//g;
    print "$string";

    Comment

    • KevinADC
      Recognized Expert Specialist
      • Jan 2007
      • 4092

      #3
      you want a character class:

      Code:
      $string =~ s/[\n\r\s]+//g;
      you don't need \t because its included in \s

      Comment

      • lilly07
        New Member
        • Jul 2008
        • 89

        #4
        oops! I didn't realise the var component. Thanks.

        Comment

        • ghostdog74
          Recognized Expert Contributor
          • Apr 2006
          • 511

          #5
          you can use re extension [:cntrl:]

          Comment

          Working...