?
Solved

Use awk with if

Posted on 2008-10-27
8
Medium Priority
?
781 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 5

Accepted Solution

by:
zmo earned 2000 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Introduction Regular patching is part of a system administrator's tasks. However, many patches require that the system be in single-user mode before they can be installed. A cluster patch in particular can take quite a while to apply if the machine…
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
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…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.
Suggested Courses
Course of the Month12 days, 13 hours left to enroll

777 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