1

I try to display two statistics of my nginx-server side-by-side. My first approach for this is column and process substitution.

column <(
        echo "sorted by busiest access times:"
        egrep -v " /hs/|GET /favicon|GET / " $file |
                sed -e 's/^[^[]\+\[\|:..:.. .*//g; s/:/ /; s/\//-/g' |
                date -f - +'%Y-%m-%d %H:xx h' |
                uniq -c |
                sort -nk 1 |
                tail -25
) <(
        echo "recent access times:"
        egrep -v " /hs/|GET /favicon|GET / " $file |
                sed -e 's/^[^[]\+\[\|:..:.. .*//g; s/:/ /; s/\//-/g' |
                date -f - +'%Y-%m-%d %H:xx h' |
                uniq -c |
                tail -25
)

Becomes:

sorted by busiest access times: recent access times:
80 2018-03-28 20:xx h           6 2018-03-29 23:xx h
81 2018-03-31 15:xx h           1 2018-03-30 01:xx h
82 2018-02-25 16:xx h           23 2018-03-30 08:xx h
83 2018-03-15 19:xx h           15 2018-03-30 09:xx h
84 2018-03-25 18:xx h           17 2018-03-30 11:xx h

Yikes, ugly. Where did the leading whitespaces go? OK, workaround with paste:

paste -d '#' <(
        echo "sorted by busiest access times:"
        egrep -v " /hs/|GET /favicon|GET / " $file |
                sed -e 's/^[^[]\+\[\|:..:.. .*//g; s/:/ /; s/\//-/g' |
                date -f - +'%Y-%m-%d %H:xx h' |
                uniq -c |
                sort -nk 1 |
                tail -25
) <(
        echo "recent access times:"
        egrep -v " /hs/|GET /favicon|GET / " $file |
                sed -e 's/^[^[]\+\[\|:..:.. .*//g; s/:/ /; s/\//-/g' |
                date -f - +'%Y-%m-%d %H:xx h' |
                uniq -c |
                tail -25
) | column -ts '#'

Better. However, the leading whitespaces are still missing:

sorted by busiest access times:  recent access times:
80 2018-03-28 20:xx h                  6 2018-03-29 23:xx h
81 2018-03-31 15:xx h                  1 2018-03-30 01:xx h
82 2018-02-25 16:xx h                 23 2018-03-30 08:xx h
83 2018-03-15 19:xx h                 15 2018-03-30 09:xx h
84 2018-03-25 18:xx h                 17 2018-03-30 11:xx h
...

Now I add a <(echo) as the first process substitution:

paste -d '#' <(echo) <(
        echo "sorted by busiest access times:"
        egrep -v " /hs/|GET /favicon|GET / " $file |
                sed -e 's/^[^[]\+\[\|:..:.. .*//g; s/:/ /; s/\//-/g' |
                date -f - +'%Y-%m-%d %H:xx h' |
                uniq -c |
                sort -nk 1 |
                tail -25
) <(
        echo "recent access times:"
        egrep -v " /hs/|GET /favicon|GET / " $file |
                sed -e 's/^[^[]\+\[\|:..:.. .*//g; s/:/ /; s/\//-/g' |
                date -f - +'%Y-%m-%d %H:xx h' |
                uniq -c |
                tail -25
) | column -ts '#'

The output becomes:

  sorted by busiest access times:  recent access times:
       80 2018-03-28 20:xx h             6 2018-03-29 23:xx h
       81 2018-03-31 15:xx h             1 2018-03-30 01:xx h
       82 2018-02-25 16:xx h            23 2018-03-30 08:xx h
       83 2018-03-15 19:xx h            15 2018-03-30 09:xx h
       84 2018-03-25 18:xx h            17 2018-03-30 11:xx h
...

Even better, but now I have two spaces at the beginning of each line, which shouldn't be there. Well, I could remove those two with an additional | sed -e 's/^..//' after column, but I'm starting to think that I'm missing the actual problem here.

What's a better approach or am I actually doing it right? Or can I make column keep the leading whitespaces?

I'm using Manjaro if that's of significance.

Your Answer

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

Browse other questions tagged or ask your own question.