With an array of elements, and an input-file I want to loop through the array and match each element with the input-file. I then want to get the line in the input-file where this element is occurring and the 3 lines that follows directly beneath. I have tried doing it this way:
for variable in $array
do
awk -v var="$variable" '/var/{x=NR+3}(NR<=x){print}' inputfile.txt
done
But this did not return the desired out put. I then tried grep:
for variable in $array
do
grep -A 3 "$variable" inputfile.txt
done
And this works. I guess I solved my problem, but as I would like to get a better understanding of awk, I decided to post this question, as I am curious to know what I am doing wrong when I try and feed awk a variable. I think I might be confused because the awk piece I have picked up, is without a BEGIN part? I am guessing a BEGIN part is assumed , and not necessary? Can my problem be solved with awk? Is awk the best tool for this task? And can my awk example be rewritten to have a BEGIN and END section, so it matches the examples and tutorials in awk I have looked at elsewhere on the internet? Thanks