Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
495 views
in Technique[技术] by (71.8m points)

windows - How can I get unique records from in the below case

I am trying to run a script where I am trying to find a value from a file. File is :

8009 [main] INFO  com.utilities.task.ICSTask  - Submitted run of the task: taskId=0015FL0Z0000000000R6, taskRunId=20789
https://use4.dm-us.com/saas/api/v2/activity/activityLog?taskId=0015FL0Z0000000000R6&runId=20789

Code I wrote is :

SetLocal EnableDelayedExpansion
@for /f "tokens=2 delims=:," %%i in ('type abc.txt') do ( 
    @set%%i 
    REM To remove Space into Variable
    Set "taskID=!taskID: =!"
    echo !taskID!  >> def.txt
)

Result I am getting is :

0015FL0Z0000000000R6
0015FL0Z0000000000R6

I want to get unique value of 0015FL0Z0000000000R6 in def.txt file as a result. How can I achieve this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There will be only one taskId but I am getting this is multiple lines. I want to retrive the taskId which is same for a flow.

you have only one taskId, but you have two lines in the text file, which means, your loop is executed two times. And as taskid isn't overwritten the second time, it still holds the value from the first time.

Change ... in ('type abc.txt') do ... to ... in ('type abc.txt^|find "taskId="') do ... to filter abc.txt to the relevant line only.

Also, you do echo !taskID! >> def.txt, which adds two trailing spaces. I slightly changed the redirection syntax to avoid that.

That changes your complete code to:

@echo off
SetLocal EnableDelayedExpansion
for /f "tokens=2 delims=:," %%i in ('type abc.txt^|find "taskId=') do ( 
    @set%%i 
    REM To remove Space into Variable
    Set "taskID=!taskID: =!"
    >>def.txt echo !taskID!
)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...