issue with nawk while using variable assignment


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting issue with nawk while using variable assignment
# 1  
Old 10-05-2012
issue with nawk while using variable assignment

Code:
 
#ifconfig -a | nawk '/1.1.1.1/{print}'
inet 1.1.1.1 netmask xxxxxxxxx broadcast 0.0.0.0

If i assign the ip to a variable and search for the variable nothing gets printed!!

Code:
 
# ifconfig -a | nawk -v ip=1.1.1.1 '/ip/{print}'

I am not able to understand why this is happening!
# 2  
Old 10-05-2012
You cannot use a variable name like that in a regex. Try:

Code:
ifconfig -a | nawk -v ip=1.1.1.1  ' match( $0, ip )'

# 3  
Old 10-05-2012
Thanks agama.. it works..

My requirement here would be to search for 1.1.1.1 and get the value of mac-address from the next line.

Code:
 
bge0: flags=123243432<UP,BROADCAST,RUNNING,MULTICAST,IPv4,FIXEDMTU> mtu 1500 index 2
        inet 1.1.1.1 netmask xxxxxxxx broadcast 0.0.0.0
        ether 0:11:11:23:w2:62

can you please tell me how to get this?
# 4  
Old 10-05-2012
Have a go with this:

Code:
ifconfig -a |awk -v ip=1.1.1.1  ' match( $0, ip ) {getline; print $2; exit}'

# 5  
Old 10-05-2012
It works. Thanks. i am trying to understand this oneliner. does it mean the match function searches for the match and holds something like pointer to the line thats matched and whatever processing is done would start from the matched line onwards ?
# 6  
Old 10-05-2012
this should work too..

Code:
ifconfig -a | awk -v ip=1.1.1.1  '$0 ~ ip'

# 7  
Old 10-05-2012
I'll add some comments to explain:

Code:
ifconfig -a |awk -v ip=1.1.1.1  ' 
match( $0, ip ) {      # if the ip address is on this line
     getline;             # get the next line from the input file
     print $2;            # print the second field 
     exit;                 # stop processing input
}'

Remember that awk reads each line of the input file (output from ifconfig in this case) and applys the condition to the line. In this case the condition is the match command which returns true when the desired string is somewhere in the input line.

It could also be written like this, which I think is a bit easier to understand, but less clear as a one liner:

Code:
ifconfig -a |awk -v ip=1.1.1.1  ' 
{
  if( match( $0, ip ) )
  {
     getline;
     print $2;
     exit;
  }
}'

Hope this helps.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Variable Assignment

Hi I am facing a problem. export local_folder=/opt/app/ cd /opt/app/abc/ abcversion="abc*" (abcga5 is inside /opt/app/abc/) echo $abcversion (it echoes the correct version as abcga5 ) Now when I reuse the value of abcversion for a below path: export... (6 Replies)
Discussion started by: ankur328
6 Replies

2. Shell Programming and Scripting

Same Variable Assignment

Hi I have a strange problem: In my shell script I am performing a copy task: . prop.txt cp -r $dir/ $dir/archive $dir is fetched from a property file (prop.txt) which stores its value dir=/opt/data Now the problem is another dir1 comes into picture. I only want to add... (1 Reply)
Discussion started by: ankur328
1 Replies

3. Shell Programming and Scripting

Mutilple variable assignment

i have file that contains data as follows 1 2 3 now i need to assign them into three variables a1=1 a2=2 a3=3 Everything should come under a loop. In reality i may have 10 values which has to be assigned to 10 variable. if i have 25 values ( lines) , each should be assigned into... (3 Replies)
Discussion started by: Rajesh_us
3 Replies

4. Shell Programming and Scripting

AWK Variable assignment issue Ksh script

Hi There, I am writing a ksh script which assigns variable values from file "A" and passes that variables to file "B". While passing the parameters an additional "$" sign is being assigned to awk -v option. Could any one help me with this please. #!/bin/ksh head -1... (3 Replies)
Discussion started by: Jeevanm
3 Replies

5. Shell Programming and Scripting

Help with variable assignment

Hi, In AIX I have a variable with , (coma) separated values assigned to it like shown below var1=apple,boy,chris i want to convert this to var1='apple','boy','chris' the number of values assigned to var1 might change and it could be from 1 to n any suggestions please? (3 Replies)
Discussion started by: rahul9909
3 Replies

6. Shell Programming and Scripting

eval and variable assignment

Hi, i have an issue with eval and variable assignment. 1) i have a date value in a variable and that date is part of a filename, var1=20100331 file1=${var1}-D1-0092.xml.zip file2=${var2}-D2-0092.xml.zip file3=${var3}-D3-0092.xml.zip i am passing the above variables to a script via... (11 Replies)
Discussion started by: mohanpadamata
11 Replies

7. Shell Programming and Scripting

Inconsistent variable assignment issue

I am using a piece of code which is working fine in some cases and is not working in some other instances. All I am doing is grep'ing and concatenating. Below is a working scenario. $ echo $var1 REPLYFILENAME=Options_NSE1.txt.enc $ FILE_NM=`echo ${var1##*=}`.gz (Take everything after... (1 Reply)
Discussion started by: jamjam10k
1 Replies

8. Shell Programming and Scripting

help needed in variable assignment

Hi all, I am having some value in feedrow in my script. And want to assign first three character in to DTYPE. I am using following syntax. DTYPE=`$FEEDROW|cut -c 1-3` But it's not assigning this valuse into DTYPE. Please suggest. Its really uegent. Regards, Gander_ss (2 Replies)
Discussion started by: gander_ss
2 Replies

9. Shell Programming and Scripting

variable assignment in new shell

Hi, I'm writing a KSH script, and at one point, I have to call a new shell and perform some variable assignments. I noticed that the assignment is not working. Please see two samples below: Command 1: #>ksh "i=2;echo I is $i" Output: #>I is Command 2: #>ksh <<EOF > i=2 > echo I... (7 Replies)
Discussion started by: maverick80
7 Replies

10. UNIX for Dummies Questions & Answers

@ in a variable assignment

Hello Everybody, Does anyone know what the @ symbol means in a csh script, if used with a variable assignment as below @ line = 1 why not just use.... set line=1 Many thanks rkap (1 Reply)
Discussion started by: rkap
1 Replies
Login or Register to Ask a Question