Paulius Mickus Paulius Mickus - 6 months ago 3x
Bash Question

awk script works on pure file, but dont work with tail+2

I have my script which gets maximum value of 3 columns of selected file. If file is pure it works good, but if i try to pipe file with tail+2 it just doesnt work anymore. There is code:

BEGIN {max1 = 0; max2 = 0; max3 = 0}
if(max1 < $1)
max1 = $1
if(max2 < $2)
max2 = $2
if(max3 < $3)
max3 = $3
END {print max1, max2, max3;}

i execute code like this : awk -f [codefilename] [targetfile]

works 100% good

i execute code like this (want to remove first line before counting):

awk -f [codefilename] [targetfile] | tail+2

it fails

Thank you for your help and time.


Consider your example:

awk -f codefilename targetfile | tail+2

First of all you need a space between tail and +2. Secondly what is happening is that the output from the awk command is piped to tail so its basically like writing:

awk -f codefilename targetfile > tmp1
tail +2 tmp1

What I guess you want is to take all but the first line from targetfile and run your awk code on it, if that's the case you need:

tail +2 targetfile | awk -f codefilename

Afaik tail +N is not supported by all distros, and you would need something like sed -n '2,$p' instead. If anyone can clarify, please do.