By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,807 Members | 1,995 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,807 IT Pros & Developers. It's quick & easy.

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

P: 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
Share this Question
Share on Google+
4 Replies


Icecrack
Expert 100+
P: 174
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
Expert 2.5K+
P: 4,059
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

P: 89
oops! I didn't realise the var component. Thanks.
Apr 23 '09 #4

Expert 100+
P: 511
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.