awk - how to pass varible


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk - how to pass varible
# 1  
Old 03-17-2015
IBM awk - how to pass varible

I want to pass variable to below awk statement
Code:
awk  '/abc123/{x=NR+1}(NR<=x){print}' sftp_log_20150317.log

I tried -v like below, but its not working. Please help!!!
Code:
awk -v var1="abc123" '/var1/{x=NR+1}(NR<=x){print}' sftp_log_20150317.log

Input file is:
sftp_log_20150317.log

Code:
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.

# 2  
Old 03-17-2015
You passed variable successfully, try like this

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

This User Gave Thanks to Akshay Hegde For This Post:
# 3  
Old 03-17-2015
Expected out put is as below:

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

Code:
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 Smilie
# 4  
Old 03-17-2015
.

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

Code:
awk -v dir="$path" '{print $4 " " $3 " " dir}'

HTML Code:
http://exceptiongeek.com/questions/question/1/How-to-use-shell-variables-in-awk-script

Last edited by Scrutinizer; 03-17-2015 at 02:38 PM.. Reason: code tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Aa_app varible in Linux

Hi, I am analyzing linux script, below is the following script line ${AA_APP:-/opt/ty/aa}/sbin/save_file_list ApplEnv.prererun ${AA_APP:-/opt/ty/aa}/sbin/ApplEnv.install_save_list I really dont understand what this line does, can someone explain me. AA_APP is variable... (4 Replies)
Discussion started by: stew
4 Replies

2. Shell Programming and Scripting

Pass awk field to a command line executed within awk

Hi, I am trying to pass awk field to a command line executed within awk (need to convert a timestamp into formatted date). All my attempts failed this far. Here's an example. It works fine with timestamp hard-codded into the command echo "1381653229 something" |awk 'BEGIN{cmd="date -d... (4 Replies)
Discussion started by: tuxer
4 Replies

3. Shell Programming and Scripting

Setting Varible with AWK in KSH

I am trying to set a variable from this AWK command in KSH but I keep getting an error that says my variable cannot be found. LOADNO = $(awk -F"|" 'NR==1{print $2}' file.txt) If I just run awk -F"|" 'NR==1{print $2}' file.txt I get the right value but as soon as I try to assign this... (1 Reply)
Discussion started by: cvigeant
1 Replies

4. UNIX for Dummies Questions & Answers

Is this a varible type?

Wondering what $@ does - is it a variable of some kind? (1 Reply)
Discussion started by: Jayden
1 Replies

5. Shell Programming and Scripting

Space in varible?

Hey guys I have here what I am sure amounts to a pretty dumb question.... how do I assign a value of say 999 to a variable called "random number" (note the space between random and number). What would the script look like using borne shell? Thanks! :) (4 Replies)
Discussion started by: pattingtonjbear
4 Replies

6. UNIX for Dummies Questions & Answers

How do I pass a variable to awk?

I have an awk statement where I Need to pass an environment variable but I cannot get it to work: My evironment varible examples below: $FILE1=/dev/fs/file.new $FILE2=/dev/fs/file.old Code below: awk -F"|" ' BEGIN { while( getline < "$FILE1" ) { arr=1 } } arr != 1 { print } '... (12 Replies)
Discussion started by: eja
12 Replies

7. Shell Programming and Scripting

How to pass a variable to Awk ?

I am trying to pass 2 shell variable's ("START" and "END") define earlier in the script to this awk statement, but i can't seem to pass it on. PLs help. set START = xxxx set END = yyyy set selected_file = `awk '/$START/,/$END/' filename` (24 Replies)
Discussion started by: Raynon
24 Replies

8. UNIX for Dummies Questions & Answers

PASS parameter to AWK

Hi, Can i pass a parameter(not a file name) as a parameter to a awk program? eg; $awk -f test 1 2 3 here test is the filename...and 1,2,3 are the i/p parameters? thank you:-) (2 Replies)
Discussion started by: unisam
2 Replies

9. UNIX for Dummies Questions & Answers

pass variable to awk

i would like to pass a variable to awk wherein the variable comes from external loop. i tried this... let x=0 until test $x -eq 32 do cat file | awk '{ print $1 , "Number" , $($x) }' >> output done thanks, (4 Replies)
Discussion started by: inquirer
4 Replies

10. Shell Programming and Scripting

Using varible/varible substitution in Perl/sed Search & Replace

Hi, I have a program that searches for a particular string patten. I am however having difficulty passing the varible $i (in a loop) as the string pattern to replace. Using either perl or sed search and replace statements, I get the same kinda result. For example, using the perl: for i in... (3 Replies)
Discussion started by: Breen
3 Replies
Login or Register to Ask a Question