-1

Bash command output is shown as a table but not able to extract column as they are in the output instead of that each word is acting as a field, if in the shown output column two words are there both of these words i am getting as two fields even though these two are one of the cell value of column.

$disk status
Disk States   tiers
In Use        10
Spare         2
Available     13
TOTAL DISKS   25

here TOTAL DISKS is in Ist column but I am using awk and taking $1(first field) it is giving TOTAL, but I want TOTAL DISKS. how to do this, I am not getting it?

1
  • 1
    To get the last field on the line in awk, use $NF. Commented Aug 8, 2019 at 6:29

2 Answers 2

0

if you are interesed in the last column, here are 2 ways to achieve it:

  1. instruct awk to return the last column

    $ disk status | awk '{print $NF}'

  2. revese the column order (last column will become the first), return the first column and then reverse it back

    $ disk status | rev | awk '{print $1}' | rev

Sign up to request clarification or add additional context in comments.

Comments

0

The reason AWK only shows the first word is that it by default separates fields/columns by any white space including the space in "TOTAL DISKS" making some of your rows 3 columns and some 2.

Either use FIELDWIDTHS to set (GNU) AWK to use fixed column width input, for your example :

$ disk status | awk -v FIELDWIDTHS="14 6" '{print $1}'

or force 2 columns by prepending NF==3{$1=$1" "$2; $2=$3} to your AWK (merges first 2 columns if there are 3) :

$ disk status | awk 'NF==3{$1=$1" "$2; $2=$3} {print $1}'

You can also try simply changing your field separator (-F) to a tab -F $'\t' or more than one single white space (in this case 2 spaces) :

$ disk status | awk -F'  ' '{print $1}'

Either way, $1 and $2 are now your 2 columns.

Your title suggest you also want HTML table output. This is described in Creating an HTML table with BASH & AWK . Based on that you could use :

$ disk status | awk -v FIELDWIDTHS="14 6" 'BEGIN { print "<table>" } {print "<tr><td>" $1 "</td><td>" $2 "</td></tr>" } END { print "</table>" }'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.