substring windows command line

11,927

This may work for you:

(For /F %A In (inputFile.txt) Do @Set "_=%A"&Call Echo ^%_:~-2^%)>outputFile.txt
Share:
11,927
G. Jonathan
Author by

G. Jonathan

IT, geek, bearded guy. Looking to deep into the realm of C++, Advanced VBA and java.

Updated on June 04, 2022

Comments

  • G. Jonathan
    G. Jonathan almost 2 years

    I tried to get the substring from each line of a text file based on fixed position in the file. I would like to achieve that using command lines.

    My file look like this:

    45testing45
    46testing46
    47testing47
    48testing48
    49testing49
    50testing50
    51testing51
    52testing52
    53testing53
    

    I tried the following command line:

    For -F %i in (inputFile.txt) do (SET _line=%i SET _chars=%_line:~10,2% @echo _chars > outputFile.txt)
    

    But the result is that the outputFile.txt is created but empty and when I hit SET in the command prompt, I see the _line variable as such _line=53testing53 SET _chars=%_line:~10,2% @echo _chars > outputFile.txt) I do not get why, I suppose I miss a delimiter in the DO part so that I can separate the different SET command.

    Could you please help me get the last 2 digits of my file in a new output file? I have downloaded Cygwin so am open to Unix command as well but I do not understand how to extract a fixed length substring of each line of a file using sed or awk.

    Thanks in advance.

    • aschipfl
      aschipfl almost 6 years
      You mean for /F but not for -F...
    • G. Jonathan
      G. Jonathan almost 6 years
      Yes sorry typo I meant /F
    • aschipfl
      aschipfl almost 6 years
      You need to put & between multiple commands in a single line...
  • Compo
    Compo almost 6 years
    Mike, the question example and tags are for the command line in cmd.exe and you've provided Windows batch script answer.
  • Compo
    Compo almost 6 years
    @aschipfl, it reads exactly as it should, I have escaped each of the percent characters, %, enclosing the variable named _ with carets, ^. Unlike most of my code, this one was actually tested, on Windows Vista!
  • aschipfl
    aschipfl almost 6 years
    I can confirm it works also on Windows 7; anyway, this is actually not escaping, because % signs are recognised before ^; seems that you are lucky, because the ~-2^ is invalid sub-string expansion syntax and is therefore simply not expanded (but is not a fatal error that aborts execution); so the second parsing phase receives the string without ^, which is valid syntax...
  • Compo
    Compo almost 6 years
    @aschipfl, I used the term escaping, simply for easy understanding, I really didn't want to explain further. I'm intrigued however as to why %_:~-2% is invalid syntax though, it is specifically expanding to the last two characters, a similar example %_test:~-7% is given here. e.g. SET _test=123456789abcdef0 followed by ::Extract only the last 7 characters and SET _result=%_test:~-7% with ECHO %_result% =abcdef0.
  • aschipfl
    aschipfl almost 6 years
    %_:~-2^% is not valid syntax (note the caret), that's why it's not expanded in the first phase; the second phase receives the string without the ^, so it's valid then... anyway, it works...