Vibhav MS
Vibhav MS

Reputation: 33

Reading a specific column from a row using a batch file

I have a notepad which looks like this.

Job Name                                                         Last Start           Last End             ST Run/Ntry Pri/Xit
________________________________________________________________ ____________________ ____________________ __ ________ _______
DEV_xxx_xxx_xxx_xxxx_b                                           11/20/2012  22:05:00 -----                RU 9229277/1

This value "22:05:00" is present on the 77th column of row 3. Is there a way using a batch script that i can extract only this value and assign it to a variable. The above notepad is the redicrected output of an autosys command (if it helps)

I've been breaking my head for the past 3 days but to no avail.

Upvotes: 1

Views: 5054

Answers (3)

Endoro
Endoro

Reputation: 37589

search it with GNU sed (very fast!)

for /f %%i in ('sed -n "/DEV/ {s/.*\([0-2][0-4]:[0-5][0-9]:[0-5][0-9]\).*/\1/p;q}" file.txt') do set "var=%%i"
echo %var%

Upvotes: 1

dbenham
dbenham

Reputation: 130929

Another way (faster) to read the 3rd line.

@echo off
<file.txt (
  set/p=
  set/p=
  set /p var=
)
set "var=%var:~76,8%"

Use a FOR /L loop if you want to skip a larger number of lines

@echo off
<file.txt (
  for /l %%N in (1 1 2) do set/p=
  set /p var=
)
set "var=%var:~76,8%"

If the job name never contains spaces, then it looks like you could also use FOR /F in the traditional way.

@echo off
for /f "skip=2 tokens=3" %%A in (file.txt) do set "var=%%A" & goto :break
:break

Upvotes: 2

foxidrive
foxidrive

Reputation: 41307

Untested

@echo off
for /f "skip=2 delims=" %%a in (file.txt) do set "var=%%a"&goto :done
:done
set "var=%var:~76,8%"
echo "%var%"
pause

Upvotes: 2

Related Questions