1

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

sku2003
  • 177
  • 1
  • 2
  • 8
  • You definitely need it to say BEGIN; it won't work without it. – Alex Aug 22 '17 at 20:18
  • @Alex . Well. You might be right that awk makes more sense when BEGIN is included. But looking at the answer provided by RomanPerekhrest below, it looks to me like a BEGIN part is just assumed, and not written? As a beginner in unix and especially the world of awk I was searching for a rewrite of RomanPerekhrest answer so it would include a BEGIN-part and an END part, to make it more pedagogical to understand how things in awk are assumed or not. The duplicate answer link provided halfway answers my question, but is hard to translate in to an answer for my question, as I am a beginner. – sku2003 Aug 23 '17 at 08:02
  • I thought that the BEGIN part would be assumed too. But then I had a script, and it was just not running as it should, and eventually I figured out to include BEGIN, and it started working normally. I guess it depends on the system? – Alex Aug 23 '17 at 13:19
  • I don’t understand this focus on BEGIN and END. If you look at the awk man page, you'll see that it supports several statements (e.g., if, for, print, and while) and many functions (e.g., gsub, index, length, and match). You don’t need to use them all; you just use the ones you need. It’s true that, if BEGIN and END are not specified, they are assumed — to be null. It seems to me that you're making this more complicated than it is.  P.S. You (sku2003) might get better support if you posted example data for which RomanPerekhrest’s answer doesn’t work. – Scott - Слава Україні Aug 26 '17 at 03:50

1 Answers1

3

The problem is that you can not paste a variable var into regex pattern /var/.
Instead, match the whole line $0 against that variable (in that case it will be treated as regex pattern)

awk -v var="$variable" '$0~var{x=NR+3}(NR<=x){print}' inputfile.txt
  • I think this is the answer I am looking for. However, I cannot make this piece of code work with $0~var. When I try it on my input-file and my array. It does not return anything, which I interpret as awk is finding no matches. Is this because awk looks for lines that has exact matches ? And grep just looks for the occurrence of my element? My input file would contain lines that has '[ABC]' and '[ABD]', but my array of elements only has 'ABC' and 'ABD' - i.e. no hard brackets. Can I make my variable contain wildcards using asterixis on both ends of my ABC element ? – sku2003 Aug 23 '17 at 07:53
  • @sku2003, $0~"ABC" will match [ABC] line without any wildcards – RomanPerekhrest Aug 23 '17 at 08:00
  • Alright then. awk is not requiring exact matches. In that case if I ask awk to find lines with 'ABC' occurring in my input file it should return the lines that has '[ABC]'. However, this still does not work. I am still not sure what I am doing wrong. I checked the kind of awk I have available with 'man awk'. It looks like it is gawk. Does this make a difference? – sku2003 Aug 23 '17 at 08:22