Solved

Use awk with if

Posted on 2008-10-27
8
771 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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Installing FreeBSD… FreeBSD is a darling of an operating system. The stability and usability make it a clear choice for servers and desktops (for the cunning). Savvy?  The Ports collection makes available every popular FOSS application and packag…
Utilizing an array to gracefully append to a list of EmailAddresses
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.:
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

705 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

15 Experts available now in Live!

Get 1:1 Help Now