gpt4 book ai didi

linux - 从文件中提取模式并减去收到的值

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:04:32 25 4
gpt4 key购买 nike

这是我目前的代码

grep -i "start" logs.txt | awk '{print $3}'
grep -i "end" logs.txt | awk '{print $3}'

因此,如果开始的值为 10,结束的值为 30。我如何减去它并得到 20 作为输出。

示例文件:logs.txt

start_value : 10
end_value : 30
start_value : 20
end_value : 50

期望的输出:

differance1 : 20
differance2 : 30

最佳答案

遵循 awk 可能会对您有所帮助。

awk '/start_value/{start=$NF} /end_value/{print "difference"++i" : " $NF-start;start=""}' Input_file

上面代码的解释:

awk '
/start_value/{ start=$NF } ##Searching for a string start_value in current line if found creating variable named start with value $NF.
/end_value/ { print "difference"++i" : " $NF-start; ##Searching for a string end_value and then printing string difference with variable i increasing value and printing the different of $NF and start variable here.
start="" } ##Nullifying the variable start here so that in case any line is empty it should not take previous value of it.
' Input_file ##Mentioning Input_file name here.

关于linux - 从文件中提取模式并减去收到的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49767482/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com