for loop returns more output with awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting for loop returns more output with awk
# 1  
Old 09-03-2010
for loop returns more output with awk

I need to get total number of hdisk not assigned to any VGs.
Code:
PDC # lspv |grep None |awk '{print $1}' |wc  
     131     131    1099

So, it shows 131 hdisks.

I need to look at the individual hdisk fget_config info like below:
Code:
PDC # fget_config -Av |grep hdisk230   
hdisk230 dac1  229 db4epic7

If I put these lines in for loop,
Code:
for diskname in $(lspv |grep None |awk '{print $1}')
do
fget_config -Av |grep $diskname 
done

the output gives me total number of hdisk as 362 with some duplicate hdisks info.

Please advise why for loop returns more outputs and the fix.

Thank you so much.

Last edited by Daniel Gate; 09-03-2010 at 12:24 PM.. Reason: code tags please
# 2  
Old 09-03-2010
Code:
fget_config -Av |grep $diskname

Check this and make sure that , $diskname is unique. I guess , the above statement returning more than one row for each $diskname.
# 3  
Old 09-03-2010
Yes, $diskname is unique (131 disks are unique with no duplicate).
Code:
for diskname in $(lspv |grep None |awk '{print $1}')
do
fget_config -Av |grep $diskname
done | sort -u

I added | sort -u to see what happens. " | sort -u " suppressed quite a bit, still the number of output is 219, which 88 extra outputs are returned.

Please advise.

Thank you so much.
# 4  
Old 09-03-2010
Code:
 
sort -u " suppressed quite a bit

that does mean , the output is returning more than one row for each diskname.

So, replace the "grep $diskname" with,

Code:
awk '/'$diskname'/ { print ; exit }'

so the query should be:

Code:
 
for diskname in $(lspv |grep None |awk '{print $1}')
do
fget_config -Av | awk '/'$diskname'/ { print ; exit }'
done

# 5  
Old 09-03-2010
YES. Now, I am getting the correct outputs. Smilie
I see 'grep' was causing the issue.
Thank you so much Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove carriage returns from awk output

I'm on Linux version 2.6.32-696.3.1.el6.x86_64, using the Ksh shell. I'm working with the input file: John Daggett, 341 King Road, Plymouth MA Alice Ford, 22 East Broadway, Richmond VA Orville Thomas, 11345 Oak Bridge Road, Tulsa OK Terry Kalkas, 402 Lans Road, Beaver Falls PA Eric Adams,... (2 Replies)
Discussion started by: prooney
2 Replies

2. Shell Programming and Scripting

Print awk output in same line ,For loop

My code is something like below. #/bin/bash for i in `ps -ef | grep pmon | grep -v bash | grep -v grep | grep -v perl | grep -v asm | grep -v MGMT|awk '{print $1" "$8}'` do echo $i ORACLE_SID=`echo $line | awk '{print $2}'` USERNAME=`echo $line | awk '{print $1}'` done ============= But... (3 Replies)
Discussion started by: tapia
3 Replies

3. UNIX for Dummies Questions & Answers

Print each output of loop in new column using awk or shell

I have this output from a loop a11 1,2 3,4 5,6 7,8 12,8 5,4 3,6 a12 10,11 12,13 15,18 20,22 a13 ... (3 Replies)
Discussion started by: maryre89
3 Replies

4. Shell Programming and Scripting

awk: too many output files created from while loop

I am using awk to read lines from a CSV file then put data into other files. These other files are named using the value of a certain column. Column 7 is a name such as "att" or "charter" . I want to end up with file names with the value of column 7 appended to them, like this: ... (5 Replies)
Discussion started by: dodgerfan78
5 Replies

5. Shell Programming and Scripting

awk output error while loop through array

Have built this script, the output is what I needed, but NR 6 is omitted. Why? Is it an error? I am using Gawk. '{nr=$2;f = $1} END{for (i=1;i<=f;i++) if (nr != i) print i, nr }' input1.csv >output1.csvinput1.csv 1 9 3 5 4 1 7 6 8 5 10 6 output1.csv > with the missing line number 6. 6 is... (5 Replies)
Discussion started by: sdf
5 Replies

6. UNIX and Linux Applications

display which line returns specific output

Hi, I'm trying to figure out a way to find which line in my file.txt with IP addresses: 192.168.0.1 192.178.0.2 etc... returns specific result when I execute command affecting all lines. For example when I run: for line in `cat file.txt`; do snmpget $line done it displays the... (5 Replies)
Discussion started by: svetoslav_sj
5 Replies

7. Shell Programming and Scripting

unexpected output within a for loop using awk

Hi all, after hours of playing around with this and scouring the web I decided to ask my fellow UNIX operators as I can't wrap my head around this. First off, I want to parse an input file with tabs (I could pull this off easily with different delimiters) but I was trying to make nicer... (2 Replies)
Discussion started by: Keepcase
2 Replies

8. Shell Programming and Scripting

Trouble with a file path as awk output in for loop

When I run the following command in the shell it works fine. It prints a city name and then a path for a file. ~$ for i in `awk -F':' '{print $0}' /home/knoppix/Desktop/data/subs | grep -m 1 $ city | sed "s/:/ /"` >do >echo $i >done Now, when I place it in this shell script (sh) it prints... (6 Replies)
Discussion started by: afroCluster
6 Replies

9. Shell Programming and Scripting

awk returns an integer?

I need to extract a value from a text file into a ksh script, and change the last two letters to "00". awk gets the right value (2500 in this example), but the variable has no value. I use the following command: StartTime=expr nawk 'NR==20 {print $11;exit}' $New_FILE echo 1 $StartTime... (4 Replies)
Discussion started by: Iliklev
4 Replies

10. UNIX for Dummies Questions & Answers

While loop in a nested if returns 'do unmatched'

I have a file called /ACCT/users that holds information on accounts to be created line by line. I'm trying to create accounts and change the password to a generic password. I want to probe the /etc/passwd file to check if there is already a user with the name i'm trying to create and if... (7 Replies)
Discussion started by: switchkill
7 Replies
Login or Register to Ask a Question