Home > Back-end >  Create a dynamic header based from the output on the file (BASH)
Create a dynamic header based from the output on the file (BASH)

Time:05-28

i have a file that is quite dynamic. Dynamic in a sense that Host and iSCSI_Name may come short or long. It works well for some host with long iqn name like the sample below but it doesn't work for host with shorter name.

Would appreciate if someone had done this or maybe had a previous script that creates a dynamic header that follows the length on the given data or vice versa.

With long iqn name

MAPPING
==================================================================================
Host           Status                 iSCSI_Name                    State
==================================================================================
irefr-esz-011  online  iqn.2000-01.com.vmware:irefr-esz-011-312901  active
==================================================================================

with short iqn and host name output looks like this. Data is not in place.

MAPPING
==================================================================================
Host           Status                 iSCSI_Name                    State
==================================================================================
esz1  online  irefr-esz-011-312901  active
==================================================================================

Currently, i had this on my script.

echo -e "\e[96m MAPPING\e[0m"
echo "=================================================================================="
printf '%-12s %-30s %-21s %-30s %-20s\n' Host Status iSCSI_Name State
echo "=================================================================================="

cat outputfile | column -t

echo "=================================================================================="
echo

output file:

irefr-esz-011  online  iqn.2000-01.com.vmware:irefr-esz-011-312901  active
esz1  online  irefr-esz-011-312901  active
irefr-esz-011  online  iqn.2000-01.com.vmware:irefr-esz-011-312901  active
esz1  online  irefr-esz-011-312901  active
irefr-esz-011  online  iqn.2000-01.com.vmware:irefr-esz-011-312901  active
esz1  online  irefr-esz-011-312901  active

CodePudding user response:

What's about

SEP="===================================================================================="
(echo Host Status iSCSI_Name State; cat outputfile) | column -t | \
sed "1 s/\(.*\)/$SEP\n\1\n$SEP/";echo $SEP

sed picks the first line and decorates it with a leading and a trailing separator.

  • Related