awk - how to pass varible

I want to pass variable to below awk statement

awk  '/abc123/{x=NR+1}(NR<=x){print}' sftp_log_20150317.log

I tried -v like below, but its not working. Please help!!!

awk -v var1="abc123" '/var1/{x=NR+1}(NR<=x){print}' sftp_log_20150317.log

Input file is:
sftp_log_20150317.log

Interactive mode on.
Passive mode on.
mput abc123? 227 Entering Passive Mode (10,87,148,57,136,63)
150 Opening data connection for abc123.
226 Transfer complete.
248 bytes sent in 0.007882 seconds (30.73 Kbytes/s)
local: abc123 remote: abc123
221 Goodbye.

You passed variable successfully, try like this

awk -v var1="abc123" '$0 ~ var1{x=NR+1}(NR<=x){print}' sftp_log_20150317.log
1 Like

Expected out put is as below:

awk '/abc123/{x=NR+1}(NR<=x){print}'  sftp_log_INDB_20150317.log
mput abc123? 227 Entering Passive Mode (10,87,148,57,136,63)
150 Opening data connection for abc123.
226 Transfer complete.
local: abc123 remote: abc123
221 Goodbye.

It is searching for abc123 string and printing lines which has abc123 and also one more extra line.

---------- Post updated at 07:17 AM ---------- Previous update was at 07:15 AM ----------

Thanks!! It worked :slight_smile:

We can pass variable to Awk, refer link
path="xyzabc"

awk -v dir="$path" '{print $4 " " $3 " " dir}'
http://exceptiongeek.com/questions/question/1/How-to-use-shell-variables-in-awk-script