Batch Script to Trim lines in text to first 30 or 50 characters only

8,025

This does it

@echo off

setlocal enableextensions enabledelayedexpansion

set /p num=Enter chars to show between quotes: 
set /a num=%num%+1

for /f "delims=" %%f in (a.txt) do (
set a=%%f

echo !a:~0,%num%!^"
)

So, using your example, that's in a.txt So each line starts with a quote, and there are spaces too though it works if there aren't spaces as well.

"hey what is going on @mike220. I am working on your car. Its engine is in very bad condition" 
"Because if you knew, you'd get shredded and do it with certainty"
"@honey220 Do you know someone who has busted their ass on a diet only for results to come to a screeching halt after a few weeks"

And running the batch script

C:\blah>a.bat
Enter chars to show between quotes: 3
"hey"
"Bec"
"@ho"

C:\blah>a.bat
Enter chars to show between quotes: 50
"hey what is going on @mike220. I am working on you"
"Because if you knew, you'd get shredded and do it "
"@honey220 Do you know someone who has busted their"

C:\blah>

I have now added two lines, to make it output to a file def.txt

@echo off


setlocal enableextensions enabledelayedexpansion

del def.txt 2>nul


set /p num=Enter chars to show between quotes: 
set /a num=%num%+1

for /f "delims=" %%f in (a.txt) do (
set a=%%f

echo !a:~0,%num%!^"
echo !a:~0,%num%!^" >>def.txt
)


C:\crp\dlsnex>a
Enter chars to show between quotes: 2
"he"
"Be"
"@h"

C:\crp\dlsnex>type def.txt
"he"
"Be"
"@h"

C:\crp\dlsnex>

for statements in cmd are a bit tricky. To understand this one, just start off trying to write a for statement that prints every line of a file, without trimming, and use a simple file like one with two lines abcdefg and hijklmn. Then this for statement for /f %f in (a.txt) do @echo %f then you build it up. But that is where you start, if you are to figure out how to do it. Once you have that, add the "delims=" see what effect it has(you might need some spaces in the lines of the file, to see the effect). Then look at how you can get a substring in batch. set a=abcdefg echo %a:~0,2% (prints ab).

Share:
8,025

Related videos on Youtube

SuperUserMan
Author by

SuperUserMan

Internet life-liner

Updated on September 18, 2022

Comments

  • SuperUserMan
    SuperUserMan over 1 year

    I am now new to scripts but i find it really difficult understanding "for" command (especially with that tokens and delimiters etc) . Saying so, i think that for command can be used to do what i am doing. If its not and there is an easier way, ignore my ignorance :(

    Say i have multiple lines in a text file abc.txt with each line starting and ending with " (quotes) E.g. a file of 3 lines

    "hey what is going on @mike220. I am working on your car. Its engine is in very bad condition" 
    "Because if you knew, you'd get shredded and do it with certainty"
    "@honey220 Do you know someone who has busted their ass on a diet only for results to come to a screeching halt after a few weeks"
    

    How can i trim each line, within the quotes, to a Fixed length say 30 or 50 or 100 characters (including spaces) I want to enter the number of character in batch and it can trim accordingly and produce a file def.txt with trimmed lines within quotes.

    Say i enter 50, results of above example should be

    "hey what is going on @mike220. I am working on you"
    "Because if you knew, you'd get shredded and do it"
    "@honey220 Do you know someone who has busted their"
    

    Thanks

    P.S. if you use For command, kindly please explain the command.

    EDIT: Though the answer provided worked, there is an issue with non english text. I am getting garbled text in Output file for non english text in input file . Any help @barlop here is the nonenglish text ( 1 line)

    "फाइल है इसको ना पढ़े आपको कोई मतलांब नही"
    
    • Paul
      Paul almost 10 years
      You start by explaining you struggle to understand for but then ask for a command to trim characters. Perhaps show us what you have so far and where you are stuck.
    • Ould Abba
      Ould Abba almost 10 years
      What do you want to do with charterers after 30? just remove or add in a new line?
    • barlop
      barlop almost 10 years
      Well why don't either of you e.g. @Paul or ould, say how to make a for loop that tokenizes by character
    • barlop
      barlop almost 10 years
      @OuldAbba add a new line?! He said nothing about adding a new line! He said trim. and create a file with the trimmed lines. if you know then it'd benefit the questioner and anybody reading, to see your answer.
    • barlop
      barlop almost 10 years
      this line may help cmd /v:on then for /f %f in (a.txt) do @(set a=%f & echo !a:~0,3!) though not there yet.. but it gives the first 3 char of each line. This variation trims some characters off for /f %f in (a.txt) do @(set a=%f & echo !a:~1,-2!)
    • Paul
      Paul almost 10 years
      @barlop Because it is always more valuable to the site and user if there is a starting point and something to overcome.
    • Paul
      Paul almost 10 years
      @SuperUserMan - are you using Windows?
    • SuperUserMan
      SuperUserMan almost 10 years
      @paul i just dont understand the For command token and delimiter thing. I though that may be used to trim each line accordingly @ OuldAbba I just want all character after 30 or 50 to be deleted and ofcourse last quote i.e. " left in each line @ Paul i am on windows yes and i need something which works on windows Xp and above (plain batch mainly)
    • SuperUserMan
      SuperUserMan almost 10 years
      @barlop i need to trip > 30 characters. may be even 100 . so thats why i said i need to enter that in batch . Let me give you an example in main question. Thanks for writing here. I know you love batch. U helped me before too :)
    • SuperUserMan
      SuperUserMan almost 10 years
      @everybody let me clarify once again that i just thought "FOR" Command may be used to do what i plan to do i.e trim lines. If there is an easier way or other way, you just simple help me with that. Thats all. Thanks
    • barlop
      barlop almost 10 years
      @SuperUserMan I know an easy way not in batch.. which is you get gnuwin32 which gives you utilities that linux users use, and look for the cut command. echo abc | cut -c 2-2 gives you 'b'. (obviously gives from character 2, to character 2, inclusively).
    • barlop
      barlop almost 10 years
      @SuperUserMan I suppose not but then you can add the " on at the end. The funny thing with cut is it doesn't cut, in the sense that it doesn't remove characters. It shows characters. But notice also my one didn't 'cut' either, it showed just the first n characters, then added a quote character. I suppose it could go wrong if you had a very short line with a quote character. like abc" and wanted to trim the first 10 characters, you'd end up with two quotes. So whether with cut or my batch, one would have to say if line length < trim amount then don't add a quote. or don't trim.
    • SuperUserMan
      SuperUserMan almost 10 years
      "in my batch, one would have to say if line length < trim amount then don't add a quote. or don't trim". Yes can you tell that command too. That would be important as my text with so many lines could have small lines too. i plan to use 100-140 characters (like in twitter) in the batch you provided.so that could be an issue
  • SuperUserMan
    SuperUserMan almost 10 years
    plz plz plz barlop explain me this.... for /f "delims=" %%f in (a.txt) do ( set a=%%f echo !a:~0,%num%!^" .......... like you explain this to a 5th grade child learning batch, each letter at a time. I am just sick of not being able to understand this "FOR command (yes i am not from programming background) ...........AND one more thing i need the output in def.txt file not echo :)
  • barlop
    barlop almost 10 years
    to learn programming or scripting you have to break things down and build simple things grass up. So to understand it properly, the best way isn't so much to get a broken down explanation of every line.. But to write something simple yourself and to build it up. So you start by trying to understand something very simple. Like a for statement that dumps every line of a file (not trimming). And you look at how, given a variable e.g. a=abcdefg how do you output just part of it. like just the first 3 characters.
  • SuperUserMan
    SuperUserMan almost 10 years
    ok i understood something. 1) So %f prints 1 line why you used a=%%f i.e. extra "%" ?. 2)what does ! in start and end mean? and what does ^ and " mean in command --- !a:~0,%num%!^" 3) What does "delims=" mean? Thanks. For 3 characters in your example i would need %a:~0,3% right?
  • barlop
    barlop almost 10 years
    @SuperUserMan some of these are things that are unique to batch.. If you try something from cmd, you use %f, if you try something in a batch you see it doesn't work, and that you should use %%f in batch. You can try it with any simple line, from the command line vs from batch file. And it can be %a or whatever for %a in (a.txt) do echo %a. In batch you use %%a.
  • barlop
    barlop almost 10 years
    The exclamation marks are something completely ridiculous about batch that is a surprise to programmers. Normally you'd do %var% as in your variable surrounded by percentages. But in -some- cases, that doesn't work like using %var% within a for or if can be funny, and when that happens you have to A)have this line setlocal enableextensions enabledelayedexpansion and B)use !var! instead of %var% And if you are running things just in cmd.exe and you want !var! to work you need to do cmd /v:on then you can do echo !var!
  • barlop
    barlop almost 10 years
    The ^" well, you could always try ". But " has a special meaning, and to just give a plain " without any special meaning, you do what is called 'escaping' you escape it. And to escape it, you use the escape character, which in cmd.exe is ^. So, ^" is what I did there. I did ^" instinctively but generally you'd know you need to when you get errors not doing so. Getting batch files to do what you want is in some ways more difficult than programming. 'cos with programming you have a compiler that gives error messages and also you don't have the sillyness that i've described like needing !var!
  • barlop
    barlop almost 10 years
    escaping can also be something you do to a literal character to get its special meaning.. As an example, open cygwin and do $ echo -e 'abc\ndef'<ENTER> and putting the \ before the n, makes \n, which is read as a new line, not just a regular 'n'.
  • SuperUserMan
    SuperUserMan almost 10 years
    Thanks barlop. I understood everything regarding "for" in your answer except what does "delims=" mean? in context to my example? Thanks a lot again for your time :)
  • barlop
    barlop almost 10 years