1

The file which I am processing (param.txt) is tab delimited and the contents of the first column are _ delimited.

param.txt

ONE_one  two  three
FOUR_four  five  six
SEVEN_seven  eight  nine

I've created below script to process this file:

@echo off & setlocal enableextensions enabledelayedexpansion
for /f "tokens=1,2,3 delims=    " %%G in ('type param.txt') do (
  for /f "tokens=1 delims=_" %%a in ('echo %%G') do (
    echo %%a -- %%b -- %%H -- %%I
  )
)
endlocal & goto :EOF

This produces the below output:

ONE -- %b -- two -- three
FOUR -- %b -- five -- six
SEVEN -- %b -- eight -- nine

What needs to be rectified in the script to get it to print the "value" instead of %b?

2 Answers 2

2

Try replacing tokens=1 with tokens=1,2.

Note also that you can do the extraction with a single for loop, since you can specify multiple delimiters:

for /f "tokens=1,2,3,4 delims=_ " %%G in ('type param.txt') do (
  echo %%G -- %%H -- %%I -- %%J
)
Sign up to request clarification or add additional context in comments.

Comments

0

JRL got the correct answer given the scenario you presented.

If a value in your 1st column ever might contain a value like "seven_seven_seven", then you probably only want to break on the first _. In that case you want to use "tokens=1*" in your 2nd FOR statement. That way %%b will contain "seven_seven".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.