[英]How to compare variable dynamically inside awk in bash?
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: 其中find_thread
文件具有以下内容:
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. 使用-v
标志将$Timestamp2
放入变量ts
。 $2 ~ "^" ts
= the 2nd column (for example 19:16:35.493540
) should start with ts
按模式过滤: $2 ~ "^" ts
=第二列(例如19:16:35.493540
)应以ts
开头 Here's another variation of the same thing: 这是同一件事的另一个变种:
awk -F'[ .]' -v ts="$Timestamp2" '$2 == ts {print $1}' find_thread > ThreadID
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.