Solved

Use awk with if

Posted on 2008-10-27
8
775 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
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.:
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.

803 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