首页 > 解决方案 > 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

标签: linuxawk

解决方案


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 {} +

推荐阅读