473,465 Members | 1,960 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

89 New Member
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 37130
Icecrack
174 Recognized Expert New Member
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 Recognized Expert Specialist
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 New Member
oops! I didn't realise the var component. Thanks.
Apr 23 '09 #4
ghostdog74
511 Recognized Expert Contributor
you can use re extension [:cntrl:]
Apr 23 '09 #5

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

Similar topics

4
by: Alex Hunsley | last post by:
Very very simple problem here, can't find out how to do it... I want to print something without a new line after it, so I can later output something right after it on the same line. If I do: ...
14
by: Marcin Ciura | last post by:
Here is a pre-PEP about print that I wrote recently. Please let me know what is the community's opinion on it. Cheers, Marcin PEP: XXX Title: Print Without Intervening Space Version:...
28
by: Andreas Prilop | last post by:
Jukka reports on http://www.cs.tut.fi/~jkorpela/chars/spaces.html that Internet Explorer 6 fails on the "zero width space" U+200B ​ Is this observation still valid? For which versions of MS...
5
by: vMike | last post by:
Is there any benefit to stripping all the space from the page before rendering by overriding the page render and using the htmltextwriter and stringbuilder to strip linefeeds, tabs and extra space...
29
by: runningdog | last post by:
Hi, I would like to be able to embed a newline in a text string. Is there any convienent notation to do this TIA Steve
3
by: Li Pang | last post by:
Hi, I want to know the easiest way to remove the last line in a text file, or how to catch the last line of a text file. Thanks in advance
3
by: Paul | last post by:
Hi, My RichTextBox has multiple lines of text. Most of the lines unfortunately end with a space. Is it possible to replace the space and NewLine/Line Feed with just the NewLine/LineFeed? So...
6
by: Daniel Mark | last post by:
Hello all: I have the following snippet: In : fileName = 'Perfect Setup.txt\n' In : fileName = fileName # remove the '\n' character In : fileName Out: 'Perfect Setup.txt'
4
by: Richard | last post by:
How can I remove a line break/enter from a piece of text. Thx in advance
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.