Web Hosting, Design, & Coding forum

Question

Batch file to truncate lines of text?

by jeffsgp / May 21, 2013 3:45 PM 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
You are posting a reply to: Batch file to truncate lines of text?
The posting of advertisements, profanity, or personal attacks is prohibited. Please refer to our CNET Forums policies for details. All submitted content is subject to our Terms of Use.
Track this discussion and email me when there are updates

If you're asking for technical help, please be sure to include all your system info, including operating system, model number, and any other specifics related to the problem. Also please exercise your best judgment when posting in the forums--revealing personal information such as your e-mail address, telephone number, and address is not recommended.

You are reporting the following post: Batch file to truncate lines of text?
This post has been flagged and will be reviewed by our staff. Thank you for helping us maintain CNET's great community.
Sorry, there was a problem flagging this post. Please try again now or at a later time.
If you believe this post is offensive or violates the CNET Forums' Usage policies, you can report it below (this will not automatically remove the post). Once reported, our moderators will be notified and the post will be reviewed.

All Answers

Collapse -
Answer
Re: truncate
by Kees_B Forum moderator / May 21, 2013 5:22 PM 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
by jeffsgp / May 22, 2013 12:12 AM PDT
In reply to: Re: truncate

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
by Kees_B Forum moderator / May 22, 2013 12:34 AM PDT
In reply to: tnx

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
by jeffsgp / May 22, 2013 2:34 AM PDT
In reply to: Re: batch process

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
by Kees_B Forum moderator / May 22, 2013 3:53 AM PDT
In reply to: Run from Bat

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
by martinmarty / January 7, 2015 10:33 AM 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.

Popular Forums
icon
Computer Newbies 10,686 discussions
icon
Computer Help 54,365 discussions
icon
Laptops 21,181 discussions
icon
Networking & Wireless 16,313 discussions
icon
Phones 17,137 discussions
icon
Security 31,287 discussions
icon
TVs & Home Theaters 22,101 discussions
icon
Windows 7 8,164 discussions
icon
Windows 10 2,657 discussions

GIVEAWAY

Turn up the volume with our Apple Byte sweeps!

Two lucky winners will take home the coveted smart speaker that lets Siri help you around your connected house. This sweepstake ends Feb. 25, 2018.