Solved

Use awk with if

Posted on 2008-10-27
8
772 Views
Last Modified: 2013-12-21
When I run:
sudo opcdeploy -cmd winosspi_windiag -par "/drives /nonwdrives" -node dmr-fs-01 | grep ':\\\\'
in my script I get the following output:
c:\                   FIXED        NTFS           17249.45 MB    6792.10 MB
e:\      Data         FIXED        NTFS           60416.29 MB    1682.69 MB
f:\      Mirror       FIXED        NTFS           60416.29 MB    5360.89 MB
What I would like to do is get column 5 and 7, but the problem is if the drive isn't named it becomes 4 and 6. So I was going to do a awk if statement but I am clearly doing something wrong. The problem occurs on 2 fronts.

1) In the for loop it returns every individual word as a seperate loop.
2a) If I put quotations around the command then all 3 lines come in at one time
2b) When this happens, I would expect the if statement to return the correct output for c:\ because that one would still line up properly, but it does not which tells me I have an error in my if statement too.

What I would like to be able to end up with from the output above is:
DriveLetter="c:\" DriveTotal=17249.45 DriveRemaining=6792.10 (do my output based on these 3, then loop for the next 2 and repeat the process)
DriveLetter="e:\" DriveTotal=60416.29 DriveRemaining=1682.69
DriveLetter="f:\" DriveTotal=60416.29 DriveRemaining=5360.89
for test in `sudo opcdeploy -cmd winosspi_windiag -par "/drives /nonwdrives" -node dmr-fs-01 | grep ':\\\\'|awk -F\n '{ print $1 }'`

do

        echo $test

        DriveLetter=`echo $test | awk {' print $1 '}`

        DriveTotal=`echo $test | awk '{ if ( $5 == "MB" ) { print "No name" } else print "Named" }'`

        echo $DriverLetter

        echo $DriveTotal

done

Open in new window

0
Comment
Question by:Aludaan
  • 4
  • 3
8 Comments
 
LVL 5

Accepted Solution

by:
zmo earned 500 total points
ID: 22814375
1) In the for loop it returns every individual word as a seperate loop.
2a) If I put quotations around the command then all 3 lines come in at one time

the following code will correct this, it will iterate through the command output line by line
sudo opcdeploy -cmd winosspi_windiag -par "/drives /nonwdrives" -node dmr-fs-01 | grep ':\\\\'|awk -F\n '{ print $1 }' | while read test

do

        echo $test

        DriveLetter=`echo $test | awk {' print $1 '}`

        DriveTotal=`echo $test | awk '{ if ( $5 == "MB" ) { print "No name" } else print "Named" }'`

        echo $DriverLetter

        echo $DriveTotal

done

Open in new window

0
 
LVL 5

Expert Comment

by:zmo
ID: 22814457
and otherwise your code is working perfectly well, except for a little glitch line 6, where you wrote "$DriverLetter" instead of $DriveLetter ;)

2b) When this happens, I would expect the if statement to return the correct output for c:\ because that one would still line up properly, but it does not which tells me I have an error in my if statement too.

from what I've tested, your if statement works well, both with quotes around the for action (only for c:) and with what I've given you in my previous post...

I'm using GNU bash, version 3.1.17 and GNU Awk 3.1.5 my tests.
0
 
LVL 5

Expert Comment

by:zmo
ID: 22814496
just a possibility, you may have non printable characters that gets in the way and messes awk...
try outputing your command to a file, and check with vim if it shows them...
0
 
LVL 48

Expert Comment

by:Tintin
ID: 22817535
You can do the whole lot with awk, ie:
sudo opcdeploy -cmd winosspi_windiag -par "/drives /nonwdrives" -node dmr-fs-01 | grep ':\\\\' | awk '{if (NF == 7) printf "DriveLetter=%s DriveTotal=%s DriveRemaining=%s\n",$1,$4,$6; else printf "DriveLetter=%s DriveTotal=%s DriveRemaining=%s\n",$1,$5,$7}'

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 48

Expert Comment

by:Tintin
ID: 22817545
BTW

grep ':\\\\'

is better written as

fgrep ':\'

it's much easier to read.
0
 

Author Closing Comment

by:Aludaan
ID: 31510353
Thanks zmo, that was perfect.
0
 
LVL 48

Expert Comment

by:Tintin
ID: 22818653
You didn't like my solution?
0
 
LVL 5

Expert Comment

by:zmo
ID: 22820712
well, I never been a deep user of awk (besides the traditional '{print $42}' :p), and your solution learned me a bit more about awk and its possibilities, so your solution enlighted me up.

imho, I think you deserves that the points be split, but that's the author's decision... not mine.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tech tip describes how to install the Solaris Operating System from a tape backup that was created using the Solaris flash archive utility. I have used this procedure on the Solaris 8 and 9 OS, and it shoudl also work well on the Solaris 10 rel…
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now