Glycoversi
Glycoversi

Reputation: 77

Grab tokens only from lines containing a specific substring at unknown position within the first token

I have a log file that contains lines similar to:

1 3 2 4 5 6 4 3 2 4 6 6 53 54 5 5 7 4 35 52 234 234 423 26 6 2465 3

asdfj:C:kkl 4 5 6 5 4 3 2 3 4 5 6 7 6 5 45 6

1 3 2 4 5 6 4 3 2 4 6 6 53 54 5 5 7 4 35 52 234 234 423 26 6 2465 3

1 3 2 4 5 6 4 3 2 4 6 6 xdfj:C:asdfj 53 54 5 5 7 4 35 52 234 234 423 26 6 2465 3

jdfj:C:asdfj 4 5 6 5 4 3 2 3 4 5 6 7 6 5 789 6

asfgfj:C:asdfj 4 5 6 5 4 3 2 3 4 5 6 7 6 5 23 6

I need to grab the 1st and 16th tokens from all lines that BEGIN with strings containing the substring ":C:" (lines 2, 5, 6 in the example) and return these to an output file.

I'm using "FINDSTR" to grab these tokens, but I only know how to grab from all lines. How can I filter to grab from only lines beginning with the string/substring I want?

*Note: The substring ":C:" varies in it's position within the string, or else I would just try to match this ":C:" if it's position was constant.

Current commands I'm using:

@echo off

setlocal enabledelayedexpansion

for /F "tokens=1,16" %%a in (print.log) do (
echo %%a    %%b >> value.txt)

Upvotes: 0

Views: 55

Answers (1)

user6811411
user6811411

Reputation:

This batch:

@Echo off
SetLocal EnableDelayedExpansion
for /F "tokens=1,16" %%a in (
   ' findstr /R /C:"^[^ ]*:C:" print.log'
) do echo %%a    %%b >> value.txt

returns this output in value.txt:

> type value.txt
asdfj:C:kkl    45
jdfj:C:asdfj    789
asfgfj:C:asdfj    23

Upvotes: 2

Related Questions