The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
insert a line after specific line namishtiwari Shell Programming and Scripting 8 05-21-2008 03:16 PM
Adding a columnfrom a specifit line number to a specific line number Ezy Shell Programming and Scripting 2 05-12-2008 09:29 AM
printing the next line too?? AndyA Shell Programming and Scripting 6 01-31-2008 06:35 AM
Printing Dots in specific Locations in the Console ? Max_Payne High Level Programming 0 01-13-2008 12:57 AM
Printing lines with specific awk NF jehrome_rando Shell Programming and Scripting 1 03-13-2007 04:23 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-17-2008
jake_won jake_won is offline
Registered User
  
 

Join Date: Jul 2008
Posts: 5
Printing a specific line using AWK

Hi,

I have a script that fetches only specific information from fcinfo command. Below is a portion of the script.

#!/usr/bin/ksh
set -x
HBA_COUNT=`sudo fcinfo hba-port | grep -i state | awk 'END{print NR}'`
echo "$HBA_COUNT HBAs exist"
echo '........'

INDEX=1
while [wiki] $INDEX -le $HBA_COUNT [/wiki] ; do
HBA_STAT[$INDEX]=`sudo fcinfo hba-port | grep -i 'Port WWN' | awk 'NR==$INDEX' | awk '{print $NF}'`
print 'WWN:'
print ${HBA_STAT[$INDEX]}
(( INDEX=$INDEX+1 ))
done

The part in red is where I'm having trouble with. When I type
sudo fcinfo hba-port | grep -i 'Port WWN' | awk 'NR==1' | awk '{print $NF}'
into the command line, I have no problem displaying the WWN info (where the value in blue can vary).

Also, I noticed something funny where if i replace (in the script)
HBA_STAT[$INDEX]=`sudo fcinfo hba-port | grep -i 'Port WWN' | awk 'NR==$INDEX' | awk '{print $NF}'`
with
HBA_STAT[$INDEX]=`sudo fcinfo hba-port | grep -i 'state' | awk 'NR==$INDEX' | awk '{print $NF}'`
it still doesn't work, but once I change 'NR==$INDEX' with '$NR==INDEX', it works...
Can someone explain why this is happening and maybe correct me on the syntax if I'm doing something wrong?

Thanks
  #2 (permalink)  
Old 07-17-2008
Franklin52 Franklin52 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,345
The shell don't expand shell variables within single quotes, try this:


Code:
HBA_STAT[$INDEX]=`sudo fcinfo hba-port | grep -i 'Port WWN' | awk 'NR=='$INDEX | awk '{print $NF}'`

or with a awk variable:


Code:
HBA_STAT[$INDEX]=`sudo fcinfo hba-port | grep -i 'Port WWN' | awk -v var=$INDEX 'NR==var' | awk '{print $NF}'`

  #3 (permalink)  
Old 07-17-2008
jake_won jake_won is offline
Registered User
  
 

Join Date: Jul 2008
Posts: 5
Thanks so much Franklin52 . The second method doesn't work but the first one does.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 11:42 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0