Printing a specific line using AWK


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Printing a specific line using AWK
# 1  
Old 07-17-2008
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
This User Gave Thanks to jake_won For This Post:
# 2  
Old 07-17-2008
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  
Old 07-17-2008
Thanks so much Franklin52 Smilie. The second method doesn't work but the first one does.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Printing string from last field of the nth line of file to start (or end) of each line (awk I think)

My file (the output of an experiment) starts off looking like this, _____________________________________________________________ Subjects incorporated to date: 001 Data file started on machine PKSHS260-05CP ********************************************************************** Subject 1,... (9 Replies)
Discussion started by: samonl
9 Replies

2. Shell Programming and Scripting

Printing multiple lines on the same line between specific text

This is an extract from a large file. The lines that start with fc are ports on a fabric switch. In between each fc port there is information about the port. fc2/12 is up Port description is SEIEDISCOVER-3 Speed is 4 Gbps fc2/13 is down (Administratively down) fc2/14 is up Port... (1 Reply)
Discussion started by: kieranfoley
1 Replies

3. Shell Programming and Scripting

Matching and printing line with awk

Hi there, I'm trying to use awk to print out the entire line that contains a match to a certain regex and then append some text,plus the match to the end of the line. So far I have: awk -F: '{print "RG:Z:" $2}' file Which prints out the match I want plus the additional text, but I'm stuck... (3 Replies)
Discussion started by: jim_lad
3 Replies

4. Shell Programming and Scripting

Printing characters at specific position in line

Hi, I am trying to get an output like : +----------------------------------+ ----------- + + some variable substitution + some text + Is there a way I can specify in printf (in ksh) the particular position I want to print a character, and also repeat a character from... (1 Reply)
Discussion started by: neil.k
1 Replies

5. Shell Programming and Scripting

Replace specific field on specific line sed or awk

I'm trying to update a text file via sed/awk, after a lot of searching I still can't find a code snippet that I can get to work. Brief overview: I have user input a line to a variable, I then find a specific value in this line 10th field in this case. After asking for new input and doing some... (14 Replies)
Discussion started by: crownedzero
14 Replies

6. Shell Programming and Scripting

Using awk to read a specific line and a specific field on that line.

Say the input was as follows: Brat 20 x 1000 32rf Pour 15 p 1621 05pr Dart 10 z 1111 22xx My program prompts for an input, what I want is to use the input to locate a specific field. Like if I type in, "Pou" then it would return "Pour" and just "Pour" I currently have this line but it is... (6 Replies)
Discussion started by: Bungkai
6 Replies

7. Shell Programming and Scripting

Counting rows line by line from a specific column using Awk

Dear UNIX community, I would like to to count characters from a specific row and have them displayed line-by-line. I have a file called testAwk2.csv which contain the following data: rabbit penguin goat giraffe emu ostrich I would like to count in the middle row individually... (4 Replies)
Discussion started by: vnayak
4 Replies

8. UNIX for Advanced & Expert Users

printing specific line from a file.

The below line gives the perfect output when I mention the record number and file name as hardcoded. awk 'NR==3{print}' samp2.txt But when I pass the record num and file name as variable, it doesn't give any output. row_num=3;file2=samp2.txt;awk 'NR==$row_num {print}' $file2 Can you... (2 Replies)
Discussion started by: siba.s.nayak
2 Replies

9. Shell Programming and Scripting

Printing lines with specific awk NF

I have this files: ./frm/lf_mt1_cd.Ic_cell_template.attr ./die/addgen_tb_pumd.Ic_cell_template.attr ./min_m1_n.Ic_cell_template.attr When I use: awk -F\/ '{print NF}' Would result to: 3 3 2 I would like to list the files with 3 fields on it. Any Suggestions? (1 Reply)
Discussion started by: jehrome_rando
1 Replies

10. Shell Programming and Scripting

printing a line number using awk

Hi Chaps, I'm trying to print the line number of a comma delimited file where the second field in the line is blank using AWK. Here is the code I have so far where am I going wrong. It is the last column in the file. nawk -v x==0 'BEGIN {FS=",";OFS=","} x++ if ($2 == " ") print $x' bob.tst ... (3 Replies)
Discussion started by: rjsha1
3 Replies
Login or Register to Ask a Question