Andrew
Andrew

Reputation: 293

How do I use a batch script to read a file and extract a subset of a line based on a character within that line?

I am working with ClearCase and I would like to use a batch script to run through a text file generated through cleartool commands and only extract lines that contain the @@ string, extracting from the beginning of that line up until the @@ string and putting quotes before and after.

An example text file is:

"Created automatically as a result of 'Work On' action in ClearQuest"
owner: <owner>
group: <group>
stream: <stream>
title: <title>
change set versions:
  M:\LEVEL1\PROJECT\src\ROOT@@\main\LEVEL1\6
  M:\LEVEL1\PROJECT\src\ROOT\file 7.txt@@\main\LEVEL1\1
  M:\LEVEL1\PROJECT\src\ROOT\folder 2@@\main\LEVEL1\2
  M:\LEVEL1\PROJECT\src\ROOT\folder 2\file 6.txt@@\main\LEVEL1\1
  M:\LEVEL1\PROJECT\src\ROOT\folder 2\file 8.txt@@\main\LEVEL1\1
  M:\LEVEL1\PROJECT\src\ROOT\folder 2@@\main\LEVEL1\1
  M:\LEVEL1\PROJECT\src\ROOT\file 4.txt@@\main\LEVEL1\2
  M:\LEVEL1\PROJECT\src\ROOT@@\main\LEVEL1\5
  M:\LEVEL1\PROJECT\src\ROOT\file 2.txt@@\main\LEVEL1\4
  M:\LEVEL1\PROJECT\src\ROOT\file 1.txt@@\main\LEVEL1\4
  M:\LEVEL1\PROJECT\src\ROOT@@\main\LEVEL1\3
  M:\LEVEL1\PROJECT\src\ROOT@@\main\LEVEL1\3\file 3.txt\main\LEVEL1\1
clearquest record id: <id>
clearquest record State: <state>

What I would like the output to be is:

"M:\LEVEL1\PROJECT\src\ROOT"
"M:\LEVEL1\PROJECT\src\ROOT\file 7.txt"
"M:\LEVEL1\PROJECT\src\ROOT\folder 2"
"M:\LEVEL1\PROJECT\src\ROOT\folder 2\file 6.txt"
"M:\LEVEL1\PROJECT\src\ROOT\folder 2\file 8.txt"
"M:\LEVEL1\PROJECT\src\ROOT\folder 2"
"M:\LEVEL1\PROJECT\src\ROOT\file 4.txt"
"M:\LEVEL1\PROJECT\src\ROOT"
"M:\LEVEL1\PROJECT\src\ROOT\file 2.txt"
"M:\LEVEL1\PROJECT\src\ROOT\file 1.txt"
"M:\LEVEL1\PROJECT\src\ROOT"
"M:\LEVEL1\PROJECT\src\ROOT"

Is this possible?

Best regards,

Andrew

Upvotes: 2

Views: 1308

Answers (1)

Bali C
Bali C

Reputation: 31221

Here you go

for /f "tokens=*" %%a in ('findstr "@@" log.txt') do (
for /f "tokens=1 delims=@" %%b in ("%%a") do (
echo "%%b" >>new.txt
)
)

Extracted strings will be in new.txt, just remember to change log.txt to your real text file :)

Upvotes: 3

Related Questions