Given this format:
DISKGROUP NAME GB ALLOCATED GB USED GB FREE USABLE GB PCT USED STATUS
-------------- --------------- --------------- --------------- --------------- -------- --------------------
DISK_1 1,117.40 390.48 726.92 223.78 34.95 MOUNTED
DISK_2 1,117.40 65.97 1,051.43 386.04 5.90 MOUNTED
With a single bash command how can I reliably get the value from GB FREE? The number of spaces between fields can very based on the field size, so I can’t specify a static number of spaces with cut. Is there a way to use spaces as delimiter, but have the number of spaces be a variable size?
Thanks for any ideas
You can use
awkfor this:Explanation:
awk's BEGINstatement to print the heading which isGB FREE and -------. This is because the heading in your input file has spaces in the titles. This can throw theawkscript off.awk'sbuilt-in variableNRwhich stores the line number. Since we don’t want line 1 and 2 to be printed, we keep a condition ofNR>2. This way awk will skip the first two lines and start printing from line 3.awkby default, the delimiter or Field separator is spaces (one or more). Since your file has that we don’t need to set theFSbuilt-in variable. Each field separated by a space becomes a column and can be accessed using$andcolumn number. Since you want theGB FREEcolumn which is 4, we doprint $4.column 1hasDISK_2we add another pattern statement. UsingNR>2 && $1~/DISK_2/ensures we don’t print lines 1 and 2 and look for lines wherecolumn 1hasDISK_2in them and print thecolumn 4of those lines.Note: If you don’t really care about the heading part, you can shorten this one-liner to the following (depending on your requirement):
or
Test:
To add filters as stated in the comments: