Solved

Use awk with if

Posted on 2008-10-27
8
778 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 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Let's say you need to move the data of a file system from one partition to another. This generally involves dismounting the file system, backing it up to tapes, and restoring it to a new partition. You may also copy the file system from one place to…
Utilizing an array to gracefully append to a list of EmailAddresses
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 get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…

726 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