How to remove the Space at each line?


B

Blue Fish

Hello:

I have a text file and the end of each line have about 20-28 space. It
make my file size large. May I know how can I using a batch file to
analysis the files and remove those space (End of each line)? It was
because I have over 100+ test files and each file have more than 500+ lines.

Thanks!
 
Ad

Advertisements

F

foxidrive

I have a text file and the end of each line have about 20-28 space. It
make my file size large. May I know how can I using a batch file to
analysis the files and remove those space (End of each line)? It was
because I have over 100+ test files and each file have more than 500+ lines.
This creates new files and renames the original files *.$01 so you can
check the results.

@echo off
for /f "delims=" %%a in (
'dir *.txt /a:-d /o:n /b /s') do (
ren "%%a" "%%~na.$01"
sed "s/[ ]*$//" "%%~na.$01">"%%a")


GNUSED can be downloaded for this task.
 
T

Timo Salmi

Blue Fish said:
I have a text file and the end of each line have about 20-28 space. It
make my file size large. May I know how can I using a batch file to
analysis the files and remove those space (End of each line)? It was
because I have over 100+ test files and each file have more than 500+ lines.
One option:

Write the following file and call it TRIMMER.VBS

Do While Not WScript.StdIn.AtEndOfStream
WScript.Echo RTrim(WScript.StdIn.ReadLine)
Loop

The run each of your files through
cscript //nologo trimmer.vbs < myfileOld001.vbs > myfileNew002.txt

Making that loop through all your relevant files is left as an exercise
along the lines given by foxidrive and the FAQ on the last line of my
signature.

All the best, Timo
 
T

Timo Salmi

One option:
Another option if you have access to SED. Simply apply the following on
each of your files

sed -e "s/ *$//"

All the best, Timo
 
A

Al Dunbar

Timo Salmi said:
One option:

Write the following file and call it TRIMMER.VBS

Do While Not WScript.StdIn.AtEndOfStream
WScript.Echo RTrim(WScript.StdIn.ReadLine)
Loop

The run each of your files through
cscript //nologo trimmer.vbs < myfileOld001.vbs > myfileNew002.txt

Making that loop through all your relevant files is left as an exercise
along the lines given by foxidrive and the FAQ on the last line of my
signature.
This is a good illustration of the fact that some scripting languages are
more appropriate for some kinds of tasks than others. I do a fair amount of
simple file manipulation in batch, but when it comes to processing text I
find there are so many exceptions that it is usually easier to switch to
vbscript.

/Al
 
T

Timo Salmi

This is a good illustration of the fact that some scripting languages are
more appropriate for some kinds of tasks than others. I do a fair amount of
simple file manipulation in batch, but when it comes to processing text I
find there are so many exceptions that it is usually easier to switch to
vbscript.
Agreed. And if one wanted to be a batch purist, the above solution
could, of course, easily be enveloped as a traditional batch file.

All the best, Timo
 
Ad

Advertisements

E

Esra Sdrawkcab

another language or utility

vbscript.
Agreed. And if one wanted to be a batch purist, the above solution
could, of course, easily be enveloped as a traditional batch file.
IMO these things are indeed not what Batch is traditionally about.
 
T

Todd Vargo

Esra said:
another language or utility

vbscript.

IMO these things are indeed not what Batch is traditionally about.
On the contrary, some version of BASIC (or other HLL) has been used to
compliment batch scripting since the pre-Windows years.
 
A

Al Dunbar

Esra Sdrawkcab said:
another language or utility
Quite. But while there is wisdom in choosing the most appropriate tool for
the job, there are practical reasons why one might not want to always have
to be learning a new language or utility in order to select the most
appropriate tool. vbscript might not be the best tool for text processing,
but it is, in my case at least, a better one than batch.
vbscript.

IMO these things are indeed not what Batch is traditionally about.
You are probably correct. However the problem space a language is aimed at
or intended for and the problem space where it is useful are not always the
same.

/Al
 
B

billious

Timo Salmi said:
Agreed. And if one wanted to be a batch purist, the above solution
could, of course, easily be enveloped as a traditional batch file.

All the best, Timo

--
Ah - the QBASIC kid philosophy...

So - if It take a batch file and make a C-wrapper for it, it's now a
C-program? And if I make a Java wrapper, it's a Java program? And if I wrap
it in assembler code, then it's an assembler program? And since it's each of
these, then it's on-topic in C, Java and assembler groups?

How does wrapping a *basic program in a batch wrapper make it a batch
program? It's a *basic program with a fancy wrapper. Like getting a bottle
of Evian in a presentation JDs box... :)
 
T

Timo Salmi

Ah - the QBASIC kid philosophy...
More importantly, a generic portability philosophy.
So - if It take a batch file and make a C-wrapper for it, it's now a
C-program? And if I make a Java wrapper, it's a Java program? And if I wrap
it in assembler code, then it's an assembler program? And since it's each of
these, then it's on-topic in C, Java and assembler groups?

How does wrapping a *basic program in a batch wrapper make it a batch
program? It's a *basic program with a fancy wrapper. Like getting a bottle
of Evian in a presentation JDs box... :)
While your comparison is superficially logical, it does not fully tally
with computer maintenance reality.

The essence of a batch program is that it is ensured to run from the
command line for the generic user-base without extra utilities. In XP
cscript (VBS) can be assumed to be generally available to users (or
rather on the users PC) when one writes and presents the batches. Not so
e,g. with the C-compiler or other similar third-party situations.

SED and GAWK are somewhere in the middle ground, but that's for another
discussion (that we've already had).

All the best, Timo
 
Ad

Advertisements

T

Timo Salmi

billious said:
So - if It take a batch file and make a C-wrapper for it, it's now a
C-program? And if I make a Java wrapper, it's a Java program? And if I wrap
it in assembler code, then it's an assembler program? And since it's each of
these, then it's on-topic in C, Java and assembler groups?
What is on-topic in a newsgroup depends on its established practices and
originally its charter. These situations become "culturally" defined.
Not mechanically, alluring as such simplifying might be.

All the best, Timo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top