Variable assignment inside awk

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Variable assignment inside awk
# 1  
Old 04-10-2013
Variable assignment inside awk


Was hoping someone could help with the following:

while read line; do pntadm -P $line | awk '{if (( $2 == 00 && $1 != 00 ) || ( $2 == 04 )) print $3,$5}'; done < /tmp/subnet_list

Anyone know if it is possible to assign $3 and $5 to separate variables within the {} brackets?

Thanks in advance
# 2  
Old 04-10-2013
This will go much faster if you explain what it is you are trying to accomplish. Perhaps demonstrate a failed attempt and explain why it was inadequate.

The answer to your question as asked is an obvious yes. The value of the field variables $3 and $5 can be assigned to separate variables (although they already are in separate variables). Perhaps you meant if those values could be pulled out of AWK and assigned to shell variables?

Again, this will go much faster if you explain the bigger picture.

# 3  
Old 04-10-2013
Hi Alister,

Essentially what I am trying to get here is pick out list of IP addresses from dhcp pool on a Solaris (dhcp) server where certain criteria are matched.

/tmp/subnet_list - contains a list of subnets

The pool for each subnet is listed and each line (containing an IP address among other details) is checked.

I am trying to match each line that has a MAC address present AND a reserved flag of 00 (awk variables $1 and $2 respectively) OR has a reserved flag of 04 (again awk variable $1).

For each of the lines that are matched, I want to pass the awk variables $3 and $5 (of the line in question) out to a shell variable e.g. ip_address=$3 and lease_date=$5. So I can then pass these through another check...

I hope this helps explain things a little better and unfortunately I can not provide code examples at this point in time, as I do not have access to the script from the system I am presently using.

If further details are required, let me know...

Thanks in advance
# 4  
Old 04-10-2013
Here's a possible way, in a shell script:
rm -f /tmp/awk_output
while read line; do
  pntadm -P $line | awk '{if (( $2 == 00 && $1 != 00 ) || ( $2 == 04 )) print $3,$5}' >> /tmp/awk_output
done < /tmp/subnet_list

while read ip_address lease_date; do
  echo $ip_address lease_date
  # further ops
done < /tmp/awk_output

# 5  
Old 04-11-2013
Thanks Hanson44 - I could do it that way, but I was really hoping to assign $3 and $5 to shell variables inside the {} brackets (assuming that it is actually possible?)...

Just to make the picture a little clearer:
root[my-box]# cat /tmp/final_list
root[my-box]# pntadm -P 111.222.333.0 | head

Client ID       Flags   Client IP       Server IP       Lease Expiration                Macro           Comment

00A0BCDE1FGHI1  00      111.222.333.001    111.222.333.253     04/06/2013                      macro1
00              04      111.222.333.002    111.222.333.253     Zero                            macro1
00              00      111.222.333.003    111.222.333.253     Zero                            macro1
00A0BCDE1FGHI2  00      111.222.333.004    111.222.333.253     05/06/2013                      macro1

Present code:
while read line; do pntadm -P $line | awk '{if (( $2 == 00 && $1 != 00 ) || ( $2 == 04 )) print $3,$5}'; done < /tmp/subnet_list

The code I have a present should pick out three lines from above (IP's 111.222.333.001 ...002 and ...004).

Can anyone tell me how to assign $3 and $5 to shell variables, within the {} brackets - please? Smilie


Last edited by CiCa; 04-11-2013 at 05:50 AM..
# 6  
Old 04-11-2013
Use array
while read line; do 
  val=( pntadm -P $line | awk '{if (( $2 == 00 && $1 != 00 ) || ( $2 == 04 )) print $3,$5}' )
  echo ${val[0]} ${val[1]}
done < /tmp/subnet_list

# 7  
Old 04-11-2013
Thanks Ahamed101 - unfortunately though, my knowledge around arrays and their usage is pretty limited e.g. how do I then read these values out of the array?

Alternatively, do you know of a way of doing this without the use of an array?

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Optimize multiple awk variable assignment

how can i optimize the following: TOTALRESULT="total1=4 total2=9 total3=89 TMEMORY=1999" TOTAL1=$(echo "${TOTALRESULT}" | egrep "total1=" | awk -F"=" '{print $NF}') TOTAL2=$(echo "${TOTALRESULT}" | egrep "total2=" | awk -F"=" '{print $NF}') TOTAL3=$(echo... (4 Replies)
Discussion started by: SkySmart
4 Replies

2. Shell Programming and Scripting

[awk] printing value of a variable assignment from a file

Heyas Me try to print only the value of a (specific) variable assignment from a file. What i get (1): :) tui $ bin/tui-conf-get ~/.tui_rc TUI_THEME dot-blue "" "$TUI_DIR_INSTALL_ROOT/usr" "$TUI_DIR_INSTALL_ROOT/etc/tui" "$TUI_PREFIX/share/doc/tui" "$TUI_PREFIX/share/tui"... (2 Replies)
Discussion started by: sea
2 Replies

3. Shell Programming and Scripting

Passing awk variable argument to a script which is being called inside awk

consider the script below sh /opt/hqe/hqapi1-client-5.0.0/bin/ alert list --host=localhost --port=7443 --user=hqadmin --password=hqadmin --secure=true >/tmp/alerts.xml awk -F'' '{for(i=1;i<=NF;i++){ if($i=="Alert id") { if(id!="") if(dt!=""){ cmd="sh (2 Replies)
Discussion started by: vivek d r
2 Replies

4. Shell Programming and Scripting

HELP with AWK one-liner. Need to employ an If condition inside AWK to check for array variable ?

Hello experts, I'm stuck with this script for three days now. Here's what i need. I need to split a large delimited (,) file into 2 files based on the value present in the last field. Samp: Something.csv bca,adc,asdf,123,12C bca,adc,asdf,123,13C def,adc,asdf,123,12A I need this split... (6 Replies)
Discussion started by: shell_boy23
6 Replies

5. Shell Programming and Scripting

awk variable assignment-- inside braces or not?

So, in awk, I've always put my variable assignments inside of the curly braces, just like dad, and grandpa, and the 26 generations before them. But today I came upon an awk statement that had them outside the braces-- blasphemy! Seriously, though, is there any practical difference? I was... (3 Replies)
Discussion started by: treesloth
3 Replies

6. 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

7. Shell Programming and Scripting

Automatic variable assignment inside a for loop

cs1=`echo "scale=8;($css1/$css0)*100"|bc` cs2=`echo "scale=8;($css2/$css0)*100"|bc` cs3=`echo "scale=8;($css3/$css0)*100"|bc` cs4=`echo "scale=8;($css4/$css0)*100"|bc` cs5=`echo "scale=8;($css5/$css0)*100"|bc` cs6=`echo "scale=8;($css6/$css0)*100"|bc` cs7=`echo "scale=8;($css7/$css0)*100"|bc`... (3 Replies)
Discussion started by: thulasidharan2k
3 Replies

8. Shell Programming and Scripting

variable assignment using awk

Guys, Could you please help me out. I need two values in two variables using awk from the o/p of grep. example:- grep sdosanjh <filename> sdosanjh myhostname myfilename NOW WHAT I WANT IS :- sdosanjh should be in variable (say NAME) myhostname should be in variable (say... (8 Replies)
Discussion started by: sdosanjh
8 Replies

9. Shell Programming and Scripting

getting variable inside awk

Hi All, I have awk script for replacing the nth ocurance of a string in an xml file... My code is like this FILETYPE=xml TAGNAME=type OCCURANCE=$1 TAGVALUE=valueur echo OCCURANCE:$OCCURANCE echo TAGNAME:$TAGNAME echo TAGVALUE:$TAGVALUE awk -v n=$OCCURANCE -v... (1 Reply)
Discussion started by: subin_bala
1 Replies

10. Shell Programming and Scripting

awk variable assignment in a file

Hi All, I have a little awk script which uses a variable (x): awk -v x=0 'NF != 6 { ++x } END { print "This batch had " x " errors out of ", NR" records"}' But when I've tried to put the command in a file I can't seem to declare the variable. I've managed to simplify the code so that I... (4 Replies)
Discussion started by: pondlife
4 Replies
Login or Register to Ask a Question

Featured Tech Videos