469,366 Members | 2,396 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,366 developers. It's quick & easy.

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

89
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.

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. local $/=undef;
  3. open(FILE, "test.fa") || die ("Error\n");
  4.  
  5. $string = <FILE>;
  6.  
  7. $corrected_string =~ s/\n\r\t\s//g;
  8. print "$corrected_string";
  9.  
  10.  
Apr 22 '09 #1
4 35740
Icecrack
174 Expert 100+
for the regexp to work you need to make sure it has a VAR with a string.

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. local $/=undef;
  3. open(FILE, "test.fa") || die ("Error\n");
  4.  
  5. $string = <FILE>;
  6.  
  7. $string =~ s/\n\r\t\s//g;
  8. print "$string";
Apr 22 '09 #2
KevinADC
4,059 Expert 2GB
you want a character class:

Expand|Select|Wrap|Line Numbers
  1. $string =~ s/[\n\r\s]+//g;
you don't need \t because its included in \s
Apr 22 '09 #3
lilly07
89
oops! I didn't realise the var component. Thanks.
Apr 23 '09 #4
ghostdog74
511 Expert 256MB
you can use re extension [:cntrl:]
Apr 23 '09 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

14 posts views Thread by Marcin Ciura | last post: by
28 posts views Thread by Andreas Prilop | last post: by
5 posts views Thread by vMike | last post: by
29 posts views Thread by runningdog | last post: by
3 posts views Thread by Li Pang | last post: by
6 posts views Thread by Daniel Mark | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.