linux - Running find, grep and awk script
问题描述
I am using find to search for file. then I pass this file as argument for grep. I want to run awk script on each result? How can I do this ?
so lets say, find returns 5 files, grep on each file return 15 lines and on these 15 lines, i want to run awk script.
I tried following but it error out
find . -type f -name test.log | xargs grep -A 15 "Data starts now" | xargs awk -f postprocess.awk
can anybody suggest anything wrong with syntax ?
Lets say file test1.log is
num1 104
num2 434
Num3 572
Data starts now
num1 04
num2 34
Num3 72
Lets say file test2.log is
num1 203
num2 135
Num3 098
Data starts now
num1 17
num2 33
Num3 89
Lets say file test3.log is
num1 924
num2 834
Num3 532
Data starts now
num1 34
num2 63
Num3 89
postprocess.awk is
{
if($1=="num1")
{
num1_value =$2;
}
if($1=="num2")
{
num2_value =$2;
}
if($1=="num3")
{
num3_value =$2;
}
}
END {
mult=num1_value*num2_value;
print "Multiplication is " mult;
}
if I run
find . -type f -name "test*.log" -exec grep -A15 "Data starts now" {} + | awk -f postprocess.awk
I should have got 3 output but get only 1 line with wrong result
Multiplication is 0
解决方案
To run grep
and awk
on each individual file found by find
, you can use the -exec
action and start a small shell script:
find . -type f -name "test*.log" -exec sh -c '
for file; do
grep -A15 "Data starts now" "$file" | awk -f postprocess.awk
done
' sh {} +
推荐阅读
- scala - 在 org.scalatest.FunSuite 上定义超时
- r - 通过连接多个列来更新列
- javascript - 通过 reduce 键创建新的 json 对象
- python - Dask.distributed 每个节点只使用一个核心
- python - Python:For 循环和列表
- c# - Regex.IsMatch returning True which is not correct
- angular - Insert some element according to Average number
- firebase - NoSQL 'like' 模型结构
- c - 为什么加密函数 'crypt()' 不接受我的 nonce,尽管 nonce 是一个有效的、两个字符长的字符串?
- javascript - 我可以从子组件获取计算数据到父组件吗?