I want to dynamically compare the value of two variable and write the answer to a file:
#!bin/bash
Timestamp2="19:16:35"
cat find_thread |awk -F'.' '{print $1}'|awk '{for (i=1; i<=NF;i++) {if ( $i == $(Timestamp2) ) {print (i-1)}}}'>ThreadID
where find_thread
file has the following content:
8361 19:16:35.493540
8361 19:16:35.493594
8360 19:16:41.242314
8360 19:16:41.242343
8278 19:16:39.179931
8278 19:16:39.179973
If I understood correctly, here you go:
awk -v ts="$Timestamp2" '$2 ~ "^" ts {print $1}' find_thread > ThreadID
Explanation:
$Timestamp2
in the variable ts
using the -v
flag. $2 ~ "^" ts
= the 2nd column (for example 19:16:35.493540
) should start with ts
Here's another variation of the same thing:
awk -F'[ .]' -v ts="$Timestamp2" '$2 == ts {print $1}' find_thread > ThreadID
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.