6

I have a script which check the mounted file system against the entry listed under fstab, the issue what I am facing here is to keep the output align.

Below is the script output:

/  is mounted OK
/boot  is mounted OK
/was8  is mounted OK
/was8/slogs  is mounted OK
/was8/cluster  is mounted OK
/was8/working  is mounted OK
/was8/app  is mounted OK
/was8/tools  is mounted OK
/was8/plugin  is mounted OK
/was8/coreproduct  is mounted OK
...

I want to keep these line aligned so it should look like this:

/  is mounted                       OK
/boot  is mounted                   OK
/was8  is mounted                   OK
/was8/slogs  is mounted             OK
/was8/cluster  is mounted           OK
/was8/working  is mounted           OK
/was8/app  is mounted               OK
/was8/tools  is mounted             OK
/was8/plugin  is mounted            OK
/was8/coreproduct  is mounted       OK
...

I have tried column and xargs unable to get the desire result. Can someone help me with this.

  • hi friend, Perl suggestion works for me, Except the / file system rest all gets aligned perfectly, There is extra tab for / file system Ok status and other file systems – Kamal Asdeo Jun 1 '18 at 18:17
12

In general, when you're doing the printing, you can set the width in the format string to printf. %-20s would print a string on a field 20 characters(*) wide, unless it overflows. %-20.20s would make it 20 characters and drop any overflowing part.

(* Though e.g. Bash's printf actually counts bytes. The difference can be seen with characters like ä in UTF-8.)

So, e.g.

printf "%-40s %s\n" "$mountpoint  is mounted" "$status"

would make the first part (at least) 40 characters wide:

/was8/coreproduct  is mounted            OK
...

Or, if you need to post-process an input like that, you could use Perl or awk:

perl -pe 's/(.*) +(\S+)$/ sprintf "%-40s %s", $1, $2 /e'  < file

awk '{s=$NF; sub(/ *[^ ]+ *$/, "", $0); printf "%-40s %s\n", $0, s}'  < file

Both basically separate the last non-whitespace string, and then print the two parts with the first on a fixed-width field.


Or, if you don't care about keeping the separation between the fields exactly as they were, a simpler solution commented by @JJoao would be:

awk '{s=$NF; NF-- ; printf "%-40s %s\n", $0, s}' < file

That produces the below output. Note that the two-space blank before is mounted is collapsed to one. This happens since awk rebuilds the whole $0 when NF or any of the fields are modified.

/was8/coreproduct is mounted             OK
  • cool +1; or .. awk '{s=$NF; NF-- ; printf "%-40s %s\n", $0, s}' – JJoao Jun 1 '18 at 10:37
  • 1
    @JJoao, yeah, that's the easy way, but it collapses the two-space separation before is mounted :) Probably a good idea to include that in any case. – ilkkachu Jun 1 '18 at 10:48
  • 3
    Note that printf "%.20s" only prints a 20 character wide string with zsh or fish printf. With other implementations, it's 20 byte wide as POSIX (unreasonably) requires. With ksh93's printf builtin, see also printf "%20Ls" for the width to be the display width (accounting for zero-width or double-width characters). For awk, YMMV depending on the implementation, with perl, use -Mopen=locale to work with characters in the locale's charset. – Stéphane Chazelas Jun 1 '18 at 11:02
6

Just a bit of obfuscating Perl:

perl -ne 'printf "%-40s %s", /(.*) (.*)/s'
  • 1
    That's not even too bad, as far as obfuscated Perl goes. – ilkkachu Jun 1 '18 at 10:49
  • ... any way, we are getting sneaky ☺ – JJoao Jun 1 '18 at 10:52
4

One way is to introduce a tab before the status code, e.g.:

<input rev | sed 's/ /\t/' | rev | column -s $'\t' -t

Output:

/  is mounted                        OK
/boot  is mounted                    OK
/was8  is mounted                    OK
/was8/slogs  is mounted              OK
/was8/cluster  is mounted            OK
/was8/working  is mounted            OK
/was8/app  is mounted                OK
/was8/tools  is mounted              OK
/was8/plugin  is mounted             OK
/was8/coreproduct  is mounted        OK
/was8/ihs  is mounted                OK
/was8/backup  is mounted             OK
/was8/ihs/logs  is mounted           OK
/was8/wsdm  is mounted               OK
/was8/ws  is mounted                 OK
/was8/ihs/logs/analysis  is mounted  OK
  • 1
    I think you could also something like sed -Ee 's/(.*) +/\1\t/' to replace the last spaces with the tab. (Stolen from here: unix.stackexchange.com/q/187889/170373 ) – ilkkachu Jun 1 '18 at 9:39
  • @ikkachu, that's the same as sed 's/\(.*\) /\1\t/' as the " +" will only ever match one space as the preceding .* will have eaten all the other ones. – Stéphane Chazelas Jun 1 '18 at 9:42
  • @StéphaneChazelas, good point. And of course the input here didn't even have multiple spaces before the OK. The main point was in trying to avoid the revs. – ilkkachu Jun 1 '18 at 10:33
  • Linux_FS_CHECK () { mount | egrep "ext|vxfs|lofs|xfs" | awk '{print $3}' > /tmp/Postcheck while read line do cat /tmp/Postcheck | grep $line > /dev/null if [ $? -eq 0 ]; then echo -e "$line filesystem is mounted $green(OK)";$reset else echo -e "!!!Warning!!!$line filesystem is not mounted $red(Failed)";$reset fi done < /tmp/Precheck } mount | egrep "ext|vxfs|lofs|xfs" | awk '{print $3}' >/tmp/Precheck Linux_FS_CHECK this is script which I pulled from my pre & post che – Kamal Asdeo Jun 1 '18 at 17:58
  • @KamalAsdeo: I don't understand – Thor Jun 1 '18 at 21:52
1

This can also be done very easily with the column utility. The key is to include some kind of differentiating marker between your 'columns'. For example, if space is the differentiating marker then column -s ' ' -o ' ' -t will perfectly align on spaces.

I use this frequently and have even made some nice vim macros that utilize it to align code. The general syntax you want is:

column -s <Separator String> -o <Separator String in Output> -t

This is a generalization of the tab based answer, but doesn't require the insertion of tabs necessarily.

  • Which version of column is this? – Thor Jun 1 '18 at 21:54
  • I'm guessing GNU. I'm on mobile atm and its trickier to check, but it works on most linuxes I use. – LambdaBeta Jun 1 '18 at 22:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.