Presenting data in column format

I have written a script that that takes file names and breaks them apart.

File names are :

aal_mock.war.deployed
AuroralMockService-1.0.war.deployed
consumerProfile.war.deployed
EbillMock.war.deployed
IAMAOGServices.war.deployed
IAMPermission.war.deployed
notificationservice.war.deployed
tibco_mock.war.deployed
tmo-iam-aa.war.deployed
tmo-iam-pm.war.deployed
tmo-iam-privacyvault.war.deployed
tmo-iam-provisioning.war.deployed
tmo-iam-provisioning.war.undeployed

I want to present the data like

aal_mock                      deployed
consumerProfile              deployed
EbillMock                       deployed
IAMAOGServices             deployed
IAMPermission               deployed
notificationservice           deployed
tibco_mock                    deployed
tmo-iam-aa                   deployed
tmo-iam-pm                  deployed
tmo-iam-privacyvault      deployed
tmo-iam-provisioning      deployed
tmo-iam-provisioning      undeployed

But the result I am getting is like :

aal_mock      deployed
consumerProfile      deployed
EbillMock      deployed
IAMAOGServices      deployed
IAMPermission      deployed
notificationservice      deployed
tibco_mock      deployed
tmo-iam-aa      deployed
tmo-iam-pm      deployed
tmo-iam-privacyvault      deployed
tmo-iam-provisioning      deployed
tmo-iam-provisioning      undeployed

I basically want to format it like 2 columns.

The script is :

#!/bin/bash


for file in /tmp/test/*war.* ; do
   NAME=`ls -ltrh ${file} | cut -d/ -f4 | cut -d. -f1`
   STATE=`ls -ltrh ${file} | cut -d. -f4`

echo "${NAME}      ${STATE}"
done

I can I indent it properly ?

Run as ./script.sh /tmp/test or perhaps ./script.sh if you are in /tmp/test and scritp.sh lives there.

#!/bin/bash

path_base="${@:-.}"
for f in "${path_base}"/*war.*; do
  name="${f##*/}" # strip path
  name="${name%.war.*}" # clean name
  name="${name%-[0-9]*}" # strip versioning is any
  state="${f##*.}" # obtain state
  printf "%-25s %s\n" "$name" "$state"
done
1 Like
printf "%-25s %s\n" "$name" "$state"

I believe the above line is what was missing. Thank you :slight_smile:

You can pick whatever you would like to use. I just shown you a more efficient way of extracting the fields you want.

1 Like

You might also try:

#!/bin/bash
cd /tmp/test
for file in *war.*
do	printf '%-25s %s\n' "${file%%.*}" "${file##*.}"
done

which, with the files you said were in that directory, produces the output:

AuroralMockService-1      deployed
EbillMock                 deployed
IAMAOGServices            deployed
IAMPermission             deployed
aal_mock                  deployed
consumerProfile           deployed
notificationservice       deployed
tibco_mock                deployed
tmo-iam-aa                deployed
tmo-iam-pm                deployed
tmo-iam-privacyvault      deployed
tmo-iam-provisioning      deployed
tmo-iam-provisioning      undeployed

This produces output in alphabetic order (instead of reverse modification time order) and the line shown in red does not appear in the output you said you wanted. Since most of the names you provided only contain two decimal points, I don't see how your code produced the output you said it did, but maybe this will give you some other ideas for extracting the data you want. If reverse time order is important, you could also try:

#!/bin/bash
cd /tmp/test
ls -tr *war.* | while IFS= read -r file
do	printf '%-25s %s\n' "${file%%.*}" "${file##*.}"
done

Hi.

A kind of automatic alignment can be done with align :

$ align data1 
aal_mock             deployed
consumerProfile      deployed
EbillMock            deployed
IAMAOGServices       deployed
IAMPermission        deployed
notificationservice  deployed
tibco_mock           deployed
tmo-iam-aa           deployed
tmo-iam-pm           deployed
tmo-iam-privacyvault deployed
tmo-iam-provisioning deployed
tmo-iam-provisioning undeployed

or, if more space is desired, increase the gutter:

$ align -g 10 data1 
aal_mock                      deployed
consumerProfile               deployed
EbillMock                     deployed
IAMAOGServices                deployed
IAMPermission                 deployed
notificationservice           deployed
tibco_mock                    deployed
tmo-iam-aa                    deployed
tmo-iam-pm                    deployed
tmo-iam-privacyvault          deployed
tmo-iam-provisioning          deployed
tmo-iam-provisioning          undeployed

On my system:

OS, ker|rel, machine: Linux, 3.16.0-4-amd64, x86_64
Distribution        : Debian 8.6 (jessie) 
align 1.7.0

Characteristics of align :

align   Align columns of text. (what)
Path    : ~/p/stm/common/scripts/align
Length  : 270 lines
Type    : Perl script, ASCII text executable
Shebang : #!/usr/bin/perl
Help    : probably available with --help

And align can be found at:
align: text column alignment filter

Best wishes ... cheers, drl