0

In my zshrc I have an alias like this:

alias sl='screen -list'

It fits my need (seeing what screens I have running), but the output is rather ugly:

[pts/7]~% sl
There are screens on:
        32765.quotes-api        (04/26/2015 11:09:18 AM)        (Detached)
        5055.gitsync-test       (04/07/2015 09:24:04 PM)        (Detached)
        15074.gitsync-interceptor       (03/31/2015 10:39:45 AM)        (Detached)
        4662.eloquent-api    (03/29/2015 11:37:26 AM)        (Detached)
        16177.Dropbox   (03/17/2015 03:53:44 PM)        (Detached)
        18803.gitsync-todo-api-py       (03/06/2015 08:21:24 AM)        (Detached)
        796.website (01/31/2015 01:56:02 PM)        (Detached)
        7874.gitsync-optionals  (01/29/2015 02:27:24 PM)        (Detached)
        28474.linkbag   (12/16/2014 09:56:39 AM)        (Detached)
        10839.datapump  (10/13/2014 02:16:26 PM)        (Detached)
        5118.resr-api-python        (09/13/2014 12:28:33 PM)        (Detached)
        7619.dataglobbing    (09/03/2014 08:34:13 PM)        (Detached)
        10583.rest-api-dataglobbing  (09/03/2014 01:06:21 AM)        (Detached)
        11705.save-functions    (08/12/2014 01:00:58 PM)        (Detached)
14 Sockets in /var/run/screen/S-tuvokki.

So I started to format this and got the following to kinda work:

 screen -list|grep -v There|grep -v Sockets|awk 'BEGIN { format = " %-35s %-10s %s\n"
         printf format, "Name", "Active", "Status"
         printf format, "----", "------", "------" }
       { printf format, $1, $2, $5 }'

But how do I put this in an alias like the simple command I had before?

Just putting it all on one line does not work. I have tried to escape the quotes, use double quotes instead of single gave parse errors. I also tried to wrap it in a function, but it seems that the awk command relies on the newlines and doesn't like all the instructions to be on one line.

3

There are three ways you can solve this.

One: just use a function. aliases are for simple text macros, something your second example isn't.

sl() {
screen -list|grep -v There|grep -v Sockets|awk 'BEGIN { format = " %-35s %-10s %s\n"
         printf format, "Name", "Active", "Status"
         printf format, "----", "------", "------" }
       { printf format, $1, $2, $5 }'
}

Two: use the quote-line widget to properly escape the entire command

# type the entire command out like you would interactively.
% screen -list|grep -v There|grep -v Sockets|awk 'BEGIN { format = " %-35s %-10s %s\n"
     printf format, "Name", "Active", "Status"
     printf format, "----", "------", "------" }
   { printf format, $1, $2, $5 }'
# use quote-line which transform the line into:
% 'screen -list|grep -v There|grep -v Sockets|awk '\''BEGIN { format = " %-35s %-10s %s\n"
     printf format, "Name", "Active", "Status"
     printf format, "----", "------", "------" }
   { printf format, $1, $2, $5 }'\'''
# prepend alias sl= to the newly escaped line:
% alias sl='screen -list|grep -v There|grep -v Sockets|awk '\''BEGIN { format = " %-35s %-10s %s\n"
     printf format, "Name", "Active", "Status"
     printf format, "----", "------", "------" }
   { printf format, $1, $2, $5 }'\'''

And Three: just use a function. aliases are for simple text macros.

sl() {
screen -list|grep -v There|grep -v Sockets|awk 'BEGIN { format = " %-35s %-10s %s\n"
         printf format, "Name", "Active", "Status"
         printf format, "----", "------", "------" }
       { printf format, $1, $2, $5 }'
}

The awk example doesn't rely on newlines either, but you need ; to separate multiple statements on the same line.

screen -list|grep -v There|grep -v Sockets|awk 'BEGIN { format = " %-35s %-10s %s
"; printf format, "Name", "Active", "Status"; printf format, "----", "------", "------" } { printf format, $1, $2, $5 }'

Will work.

  • 1
    The function works (I don't know why I messed that up in the first place ...) For the record, what is the exact difference between option one and Three ;) – tuvokki May 7 '15 at 12:24

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.