阅读在shell脚本文件,并选择该行的部分 [英] Reading a file in a shell script and selecting a section of the line
问题描述
这大概是pretty基础,我想在发生读取文件。</ P>
然后程序应该找到CallTilEdb
出现的所有文件 Hendelse.logg
中:
CallTilEdb 8
CallCustomer 9
CallTilEdb 4
CustomerChk 10
CustomerChk 15
CallTilEdb 16
和总结,然后右列。对于这种情况下,它会是8 + 4 + 16,所以输出我希望会是 28
。
我不知道如何做到这一点,这是据我曾与得到 vistid.sh
:
#!/斌/庆典
声明-t文件名= hendelse.logg
声明-t次数=$ 1
声明-i sumTime = 0而读-r线
做
如果[$次数= $(切-f1线)10的#line
然后
sumTime + = $(切-f2线)
科幻
完成&LT; $文件名
所以在终端的执行将是
vistid.sh CallTilEdb
但现在我得到的错误是:
/home/user/bin/vistid.sh:第10行:[:一元运算符预期
您有一个不错的办法,但也许你可以使用 AWK
做同样的事情...相当快!
$ AWK -v面值=CallTilEdb'$ 1 == {面值总和+ = $ 2} END {打印总和+ 0}hendelse.logg
28
这可能看起来有点怪,如果你还没有使用的awk,到目前为止,但这里是它做什么:
-
-v面值=CallTilEdb
提供一个参数AWK
,这样我们就可以使用参数
作为脚本变量。你也可以做-v面值=$ 1
如果你想使用提供给脚本参数变量。 -
$ 1 ==面值{总和+ = $ 2}
这意味着:如果第一个字段是一样的变量的内容参数
,然后添加第二列的值到计数器之
。 -
END {打印总和+ 0}
这意味着:一旦你从处理文件完成后,打印之$ C的含量$ C>。在
+ 0
,使AWK
打印0
的情况下,之
未设置...也就是说,如果没有被发现。
如果你真的想使用bash做它,你可以使用读
有两个参数,让你不必使用<$ C $的C>剪切来处理这些值,连同一些算术运算来综上所述值:
#!/斌/庆典
声明-t文件名= hendelse.logg
声明-t次数=$ 1
声明-i sumTime = 0而读-r name的值与#-r安全读出两个值
做
如果[$次数==$名];然后#字符串比较
((sumTime + = $值))#总和
科幻
完成&LT; $文件名回声总和:$ sumTime
因此,它的工作原理是这样的:
$ ./vistid.sh CallTilEdb
和:28
$ ./vistid.sh CustomerChk
和:25
This is probably pretty basic, I want to read in a occurrence file.
Then the program should find all occurrences of "CallTilEdb"
in the file Hendelse.logg
:
CallTilEdb 8 CallCustomer 9 CallTilEdb 4 CustomerChk 10 CustomerChk 15 CallTilEdb 16
and sum up then right column. For this case it would be 8 + 4 + 16, so the output I would want would be 28
.
I'm not sure how to do this, and this is as far as I have gotten with vistid.sh
:
#!/bin/bash
declare -t filename=hendelse.logg
declare -t occurance="$1"
declare -i sumTime=0
while read -r line
do
if [ "$occurance" = $(cut -f1 line) ] #line 10
then
sumTime+=$(cut -f2 line)
fi
done < "$filename"
so the execution in terminal would be
vistid.sh CallTilEdb
but the error I get now is:
/home/user/bin/vistid.sh: line 10: [: unary operator expected
You have a nice approach, but maybe you could use awk
to do the same thing... quite faster!
$ awk -v par="CallTilEdb" '$1==par {sum+=$2} END {print sum+0}' hendelse.logg
28
It may look a bit weird if you haven't used awk so far, but here is what it does:
-v par="CallTilEdb"
provide an argument toawk
, so that we can usepar
as a variable in the script. You could also do-v par="$1"
if you want to use a variable provided to the script as parameter.$1==par {sum+=$2}
this means: if the first field is the same as the content of the variablepar
, then add the second column's value into the countersum
.END {print sum+0}
this means: once you are done from processing the file, print the content ofsum
. The+0
makesawk
print0
in casesum
was not set... that is, if nothing was found.
In case you really want to make it with bash, you can use read
with two parameters, so that you don't have to make use of cut
to handle the values, together with some arithmetic operations to sum the values:
#!/bin/bash
declare -t filename=hendelse.logg
declare -t occurance="$1"
declare -i sumTime=0
while read -r name value # read both values with -r for safety
do
if [ "$occurance" == "$name" ]; then # string comparison
((sumTime+=$value)) # sum
fi
done < "$filename"
echo "sum: $sumTime"
So that it works like this:
$ ./vistid.sh CallTilEdb
sum: 28
$ ./vistid.sh CustomerChk
sum: 25
这篇关于阅读在shell脚本文件,并选择该行的部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!