How to extract, using cmd, string between quotes from a particular line

Home / Uncategorized / How to extract, using cmd, string between quotes from a particular line

Hope I will be clear with my problem and what I search for. I have a long script in a .sjs file (basically txt with different extension), part of which are the following lines
var Want_spring_aid_file_update = 0;
var Front_spring_aid_file = "name_1.pspck";
var Rear_spring_aid_file = "name_2.pspck";

I have been looking around for a .bat script that can probe certain lines, 2 and 3 in the case above, in this .sjs file and write the string between quotes in a text file.

I have found on this marvelous website a script that outputs all the strings between quotes in that file. I want just on particular lines. The script I am talking about is below.
>"output.txt" (
for /f usebackq^ tokens^=2^ delims^=^" %%A in ("input.txt") do echo "%%A"

Also It would be helpful is someone could help me understand the role of certain characters in the above script. These are ^, 2^, ^=^.

Thanks guys !

The command FOR with option /F is used to process lines of a file, or a single string specified in double quotes, or output of a command line executed by FOR in a separate command process in background captured from handle STDOUT of this background command process.

The first option usebackq results in interpreting the file name specified in double quotes not as string to process, but as double quoted name of a file of which lines should be processed by FOR.

The option delims= is used to specify the delimiters for splitting each non empty line not starting with default end of line character ; up into one or more substrings called tokens. The default delimiters are space and horizontal tab character. delims= without any character means do not split up the line at all. Only the double quote character " should be used for this task as delimiter.

The option tokens= defines which substring(s) should be assigned to the loop variable(s). By default only the first substring (token) is assigned to the specified loop variable. By specifying tokens=2 the command FOR is informed that of interest is the second substring which should be assigned to the specified loop variable (first). This means also if a line does not have a string after 1 or more of the specified delimiters, the line is also ignored. This is true for the first line of input.txt which does not contain a double quote character at all.

Run in a command prompt window for /? for more information about the options of command FOR output on several pages.

Those options of FOR are specified usually enclosed in double quotes.
>"output.txt" (
for /F "usebackq tokens=2 delims="" %%A in ("input.txt") do echo "%%A"

But the problem here is specifying " as delimiter. For Windows command interpreter the option string is "usebackq tokens=2 delims=" which would not make much sense because that means the lines should not be split up into tokens and nevertheless only the second token is of interest. However, the next " is invalid because expected next is the loop variable, a single character with a percent sign before. The percent sign must be escaped in a batch file with an additional percent sign.

For that reason it is necessary to specify the options of FOR without enclosing them in double quotes. So it must be made sure during the preprocessing phase of Windows command interpreter before executing the FOR command line that the 3 options are interpreted as a single argument string.

The escape character to use in batch files is the caret character ^ which instructs Windows command interpreter to interpret the next character as literal character belonging to an argument string. Only % must be escaped with % instead of ^ to be interpreted as literal character.

It can be read on running in a command prompt window cmd /? on last paragraph on last output help page which characters in directory/file names (or other argument strings) require enclosing the string in double quotes: the space character, the characters &()[]{}^=;!’+,`~ and not mentioned by help the characters "|<> which can’t be used in a directory/file name.

So it is specified with usebackq^ tokens^=2^ delims^=^" for Windows command interpreter that an argument string not enclosed in double quotes with the unusual text usebackq tokens=2 delims=" should be passed to command FOR as second argument after /F.

See How does the Windows Command Interpreter (CMD.EXE) parse scripts? for more detailed information about command line parsing by Windows command interpreter.
Read more

Leave a Reply

Your email address will not be published. Required fields are marked *