How to prefix a value of variable before the each line coming from the tail command using the awk command

I am coming from this question and I need to make the filename value to be stored in a variable and to use this variable in the print command instead of a static value

These were my trials but I am still unable to fix the line so that it works as expeted

filename=catalina.$(date +"%Y-%m-%d").log; tail -n+1 -F $filename | awk '{printf $filename"*"; print $0}'
Asked By: Shima Mahmoud


Use -v option of awk

filename=catalina.$(date +"%Y-%m-%d").log
tail -n+1 -F "$filename" | awk -v file="$filename" '{printf "%s* %sn",file,$0}'


  • I use a single printf for both filename (in file variable), * and line
  • I did not use FILENAME variable which is internal to awk.
Answered By: Archemar
Categories: Answers Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.