Thank you for being a valued part of the CNET community. As of December 1, 2020, the forums are in read-only format. In early 2021, CNET Forums will no longer be available. We are grateful for the participation and advice you have provided to one another over the years.

Thanks,

CNET Support

Question

Batch file to truncate lines of text?

May 21, 2013 3:45PM PDT

Hello,

I'm a complete noob. but am in desperate need of a batch file which will truncate all lines within a text file: 40 characters from the left.
.. and if possible, to allow me to designate the directory path?

As an example

from this:

Coding & scripting forum is the ideal place for newcomers and experienced developers to share ideas.
Discuss programming logic, debug code, or talk about best practices for keeping scripts.
Any programming language goes, so whether you need help with web applications, services, or software, we're ready to help!
etc.

to this:

Coding & scripting forum is the ideal pl
Discuss programming logic, debug co
Any programming language goes, so w
etc.

Thanks for any help with this

Jeff

Discussion is locked

- Collapse -
Answer
Re: truncate
May 21, 2013 5:22PM PDT

Although such a program is easy to write in any programming language (including a macro in MS Excel), the real command line freak downloads sed for Windows (a Unix uitility originally) from http://gnuwin32.sourceforge.net/packages/sed.htm and uses that.

Kees

- Collapse -
tnx
May 22, 2013 12:12AM PDT

Thanks for the reply Kees_B

And thanks for the program link, however It would be preferable to get this process as a batch command.

My request could also be done easily within Excel, but I have a series batch commands, in which I would like to keep this final one as part of that process.

Any ideas to turn this into batch process?

Thanks

Jeff

- Collapse -
Re: batch process
May 22, 2013 12:34AM PDT

You can run sed in a .bat file.
You can run an Excel with a startup macro in a .bat file.
You can run a vbscript in a .bat file.
In fact, you can run any program in a .bat file. it's up to you to chose your preferred programming language.

Maybe even it can be done in Powershell. I have never used it, so I can't tell.

Kees

- Collapse -
Run from Bat
May 22, 2013 2:34AM PDT

Thanks Kees_B

I can understand how to open a program /file with a bat file, but once the program is open how can It run a specific task within that program.
As in the case of using SED with a bat file?

thanks

Jeff

- Collapse -
Re: program
May 22, 2013 3:53AM PDT

The program opens and starts doing what you programmed it to do. If that's a specific task, it's doing that.
Maybe hire a programmer?

Kees

- Collapse -
Answer
Re: truncate
Jan 7, 2015 10:33AM PST

I realize it's been a long time since this question was posted, but I was searching for the same thing tonight so maybe it will still be useful to someone else.

I needed to truncate all lines of an input text file as jeffsgp described, except truncate to 35 characters.

After installing sed from the link provided by Kees_B (thanks) I created the following BAT/CMD file for Win7:

truncate.cmd:

@echo off
rem
rem Usage: Supply 2 command line arguments, 1=input file, 2=output file
rem example:
rem truncate myinputfile.txt myoutputfile.txt
rem
@echo Input file is %1
@echo Ouput file is %2
@echo Processing starting.
type %1 | "c:\Program Files (x86)\GnuWin32\bin\sed" "s/\(.\{1,35\}\).*/\1/" > %2
@echo Processing complete.

Note: Input and output file must be different names.

Incidentally, it looks like the cut command would have been easier than sed, but by the time I found that out I had already installed sed.